I'm very new in Java, and for practicing I'm trying to make a program that generates new, random words using some values (mostly letters). The program is meant to take these values from a text file. The next step is to define inventories (Arrays
) that contain each letter of its kind (first by defining a variable (int
) for the length of each array
, and then filling each array with its proper letters (String
)). While checking my progress, I realize that my code isn't updating the inventories lengths (cInv and vInv).
This is the relevant part of the code:
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);
}
This is the relevant part of the text file that it reads (or should be reading):
---Phonemes---
Consonants: 43
Vowels: 9
And it's output is 0
I've tried typing 43 at the very beginning of the file, and I've also tried typing the number in the input, but I keep getting 0 nevertheless. Does someone know what I'm missing or doing wrong?