저는 Java에서 매우 새롭기 때문에 연습을 위해 일부 값 (주로 문자)을 사용하여 새로운 임의의 단어를 생성하는 프로그램을 만들려고합니다. 이 프로그램은 텍스트 파일에서이 값을 가져옵니다. 다음 단계는 해당 종류의 각 문자가 들어있는 (int
) 각 array
의 길이를 정의한 다음 각 배열에 적절한 문자 (String
)를 채워서 인벤토리 (Arrays
)를 정의하는 것입니다. 내 진행 상황을 확인하는 동안 내 코드가 인벤토리 길이 (cInv 및 vInv)를 업데이트하지 않는다는 것을 알고 있습니다.scanner.hasNextInt()는 예기치 않은 값을 반환합니다.
이 코드의 관련 부분입니다 :
static File language;
static Scanner scanFile;
static Scanner scanInput = new Scanner(System.in);
static int cInv;
static int vInv;
//Getters go here
public static void setLanguage(File language) {Problem.language = language;}
public static void setCInv(int CInv) {Problem.cInv = cInv;}
public static void setVInv(int VInv) {Problem.vInv = vInv;}
//Asks for the file with the language values.
public static void takeFile() throws FileNotFoundException {
String route = scanInput.nextLine();
setLanguage(new File(route));
BufferedReader br;
br = new BufferedReader(new FileReader(language));
}
//Gathers the language values from the file.
public static void readFile() throws FileNotFoundException {
takeFile();
scanFile = new Scanner(language);
//Defines the inventory sizes. It seems the failure is here.
if (scanFile.hasNextInt()) {setCInv(scanFile.nextInt());}
if (scanFile.hasNextInt()) {setVInv(scanFile.nextInt());}
}
public static void main(String[] args) throws FileNotFoundException {
readFile();
//The following line is for checking the progress of my coding.
System.out.println(cInv);
}
이 관련이 (읽기 또는이어야한다) 읽는 텍스트 파일의 일부입니다
---Phonemes---
Consonants: 43
Vowels: 9
그리고 출력이 있어요 0
필자는 파일의 맨 처음에 43을 입력 해 보았습니다. 입력에 숫자를 입력하려고 시도했지만 0이 계속 나타납니다. 누군가 내가 누락되거나 잘못한 것을 알고 있습니까?
https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo – notyou