0

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?

Yash
  • 6,123
  • 4
  • 15
  • 32
  • 2
    https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo – achAmháin Nov 15 '17 at 08:50

1 Answers1

0

First, change your assignments as you are reassigning the same static variables.

public static void setCInv(int CInv) {Problem.cInv = CInv;}
public static void setVInv(int VInv) {Problem.vInv = CInv;}

Second, you need to move across all tokens in the file to identify the numbers and update the respective variable.

//Gathers the language values from the file.
public static void readFile() throws FileNotFoundException {
    takeFile();
    scanFile = new Scanner(language);
    int num = 0;
    scanFile.nextLine(); //Skip ---Phonemes---
    setCInv(getInt(scanFile.nextLine()));
    setVInv(getInt(scanFile.nextLine()));
}

public static int getInt(String str){
    System.out.println(str);
    int num =0;
    Scanner line = new Scanner(str);
    //Splits the scanned line into tokens (accessed via next()) and search for numbers.
    //Similar thing could have been done using String.split(token);
    while(line.hasNext()){
        try{
            num = Integer.parseInt(line.next());
            return num;
        }catch(NumberFormatException e){}
    }
    return num;
} 
DeludedPsyche
  • 131
  • 1
  • 1
  • 11