-1

I am trying to read a file with three different items. When I run it I get the first three items printed but I can't seem to get it to run back through the loop.

The file reads as below:

Tom Atto
6
3
Eaton Wright
5
5
Cary Oki
5
11

The file will read the first line three lines and prints:

The ideal weight for Tom Atto is: 185. 

Then it has an error:

Input has information to read? true
The ideal weight for Tom Atto is: 185.0
Exception in thread "main" java.util.InputMismatchException
    at java.base/java.util.Scanner.throwFor(Scanner.java:939)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
    at chapter3.chapterThreeProject12.main(chapterThreeProject12.java:38)

The code I have written:

```java
//import file
Scanner fileIn = null;
Scanner keyboard = new Scanner(System.in);

try {
    fileIn = new Scanner(new FileInputStream("/Users/nicola/Desktop/file2.txt"));
}
catch (FileNotFoundException e) {
    System.out.println("File not found");
    System.exit(0);
}
//end import

String name1;
double heightFeet1, heightInch1;
int count = 1;

System.out.println("Input has information to read? " + fileIn.hasNext());

while (fileIn.hasNext()) {
    name1 = fileIn.nextLine();
    heightFeet1 = fileIn.nextDouble();
    heightInch1 = fileIn.nextDouble();
    double height1 = (heightFeet1 * 12 )+ heightInch1;
    double calculatingHeight1 = (height1 - 60);
    double idealWeight1 = (calculatingHeight1  * 5) + 110;
    System.out.println("The ideal weight for " + name1 + " is: " + idealWeight1);
}
Nico
  • 17
  • 3
  • 1
    Can you share an example of the file you're trying to read? – Mureinik Dec 26 '19 at 20:48
  • Where is your code stuck? – shmosel Dec 26 '19 at 20:52
  • can you post an example of `file2.txt`? you set up `Scanner keyboard` but never use it, instead everything is read from `fileIn` including part of what is printed in `System.out.println`. – kaan Dec 26 '19 at 21:29
  • you need provide your file content – dung ta van Dec 27 '19 at 03:56
  • Sorry about that, the file content is: Tom Atto 6 3 Eaton Wright 5 5 Cary Oki 5 11 The file will read the first line three lines and print The ideal weight for Tom Atto is: 185. Then it as an error – Nico Dec 28 '19 at 19:26
  • 1
    @Nico Can you provide the error message you are getting? Thanks! – Nick Dec 28 '19 at 19:34
  • [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Abra Dec 28 '19 at 20:36
  • Hi Nick, I added the error message not sure it is that helpful though. thanks – Nico Dec 29 '19 at 20:24

2 Answers2

0

Thanks for the comments guys, although it is probably the hard way, I have come up with the following solution. By reading everything in as the same ie, as a string and then converting to an int this made the program run much more efficiently. See below my solution that worked:

    String name1, heightFeet1, heightInch1;



    System.out.println("Input has information to read? " + fileIn.hasNext());


    while (fileIn.hasNext()) {
        name1 = fileIn.nextLine();
        heightFeet1 = fileIn.nextLine();
        int heightF = Integer.parseInt(heightFeet1);
        heightInch1 = fileIn.nextLine();
        int heightI = Integer.parseInt(heightInch1);
        double height1 = (heightF * 12 )+ heightI;
        double calculatingHeight1 = (height1 - 60);
        double idealWeight1 = (calculatingHeight1  * 5) + 110;
        System.out.println("The ideal weight for " + name1 + " is: " + idealWeight1);

    }
    fileIn.close();
Nico
  • 17
  • 3
0

I found out what was going wrong, it is the old nextLine trick, if I have a next line after any type of number value I must read in the white space before reading the next line, example below:

String name1; double heightFeet1, heightInch1;

    System.out.println("Input has information to read? " + fileIn.hasNext());

    while (fileIn.hasNext()) {
        name1 = fileIn.nextLine();
        heightFeet1 = fileIn.nextDouble();
        heightInch1 = fileIn.nextDouble();
        fileIn.nextLine(); // reads the white space before starting the loop again. 
        double height1 = (heightFeet1 * 12 )+ heightInch1;
        double calculatingHeight1 = (height1 - 60);
        double idealWeight1 = (calculatingHeight1  * 5) + 110;
        System.out.println("The ideal weight for " + name1 + " is: " + idealWeight1);
    }
Nico
  • 17
  • 3