0

I am trying to read data from a text document, formatted in this way:

123 Fluffy      12/04/2017  34  2   
124 Domino      11/23/2018  12  1
125 Rover       9/8/2018    45  10

and it should output like this:

ID No. 123 : Fluffy : 2 years old : 34lbs 
Registered on 12/04/2017
ID No. 124 : Domino : 1 years old : 12lbs 
Registered on 11/23/2018
ID No. 125 : Rover : 10 years old : 45lbs 
Registered on 9/8/2018

When doing this, however, it outputs this error:

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.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at DogRunner1.main(DogRunner1.java:56)

The code for the object looks like this:

public class Dog1
{

String name;

int age;
// regDate -- registration date (in the format: "YYYY/MM/DD")
String regDate;
int weight;
int idNumber;


public String bark() {
    return "bark";
}

public  void setAge(int a) {
    age = a;
}

public void setRegDate(String b) {
    regDate = b;
}
public void setId(int c) {
    idNumber = c;
}
public void setWeight(int d) {
    weight = d;
}

public String toString()
   {
       return "ID No. " + idNumber + " : " + name + " : " + age + " years old : " + weight +"lbs \nRegistered on " + regDate;
   }
}

and the code for the program that runs it looks like this:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class DogRunner1
{


   public static void main(String[] args) throws FileNotFoundException
   {


    Dog1 d3;
    d3 = new Dog1();

    File text = new File ("dogdata.txt");
    Scanner inFile = new Scanner(text);
    while(inFile.hasNext()) {
        d3.setId(inFile.nextInt());
        d3.name = inFile.nextLine();
        inFile.nextLine();
        d3.setRegDate(inFile.nextLine());
        d3.setWeight(inFile.nextInt());
        d3.setAge(inFile.nextInt());
        System.out.println(d3);
        inFile.nextLine();
    }
        inFile.close();
   }
}

I know that you have to skip a line between two Strings, or at least I think that's what I am thinking of, and I have tried many combinations of the placement of nextLine(), to no avail. I have no idea what is wrong, please help.

0

1 Answer 1

1

You are using nextLine() to read the name and the regDate fields. That method reads the rest of the line. You don't want that, you want to read a single word, so you should be using next():

    d3.setId(inFile.nextInt());
    d3.name = inFile.next();
    d3.setRegDate(inFile.next());
    d3.setWeight(inFile.nextInt());
    d3.setAge(inFile.nextInt());
    System.out.println(d3);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.