5

I'm having some trouble trying to read a String and a Double from a txt file. Here is my txt file:

Mike 300.50
John 260
Lisa 425.33

And here is the code I am using to read them:

reader = new Scanner();
while(reader.hasNext()){
    name= reader.next();
    salary = reader.nextDouble();
    System.out.println(name + " " + salary + "\r\n");
}

Whenever I run this code, Exception in thread "main" java.util.InputMismatchException appears telling me the problem is in nextDouble().

Does anybody know how to solve this?

UrsinusTheStrong
  • 1,157
  • 1
  • 15
  • 29
Tom
  • 45
  • 3
  • The `nextDouble()` leaves the newline so your second call to `next()` consumes the newline and then you try and parse the next name as a double. I suggest you read an entire line at a time. – Elliott Frisch Apr 30 '16 at 04:33
  • By doing so I will not be able to save both things in different attributes. Or can I? – Tom Apr 30 '16 at 04:45

3 Answers3

3
reader = new Scanner();
while(reader.hasNext()){
    name= reader.next();       
    salary = reader.nextDouble();
    System.out.println(name + " " + salary + "\r\n");
    reader.nextLine();
}

Try this. What's happening is that the scanner is reading the empty line after every double, so name will be the empty line, and it will read (for example) John as the salary, and give you that exception. So what I did was add that little line of code: reader.nextLine(); so it could skip that empty line. Hope this helps.

tam
  • 53
  • 1
  • 8
0

You could try this way:

while(reader.hasNextLine()){
    String[] values = reader.nextLine().split("\\s+");
    name= values[0];
    salary = Double.valueOf(values[1]);
    System.out.println(name + " " + salary + "\r\n");
}
mmuzahid
  • 2,124
  • 20
  • 37
  • Sorry to bother, but what does those lines do? Thanks. – Tom Apr 30 '16 at 04:43
  • I wanted to know these lines do: name= values[0]; salary = Double.valueOf(values[1]); – Tom Apr 30 '16 at 04:46
  • ``reader.nextLine().split("\\s+");`` split single line at a time by space and stored it in a String array. So I'm getting String name by ``values[0]`` first element of array and ``Double.valueOf(values[1])`` parsing second element to double – mmuzahid Apr 30 '16 at 04:49
  • Thanks a lot! It worked perfectly and now I understand some new concepts :) – Tom Apr 30 '16 at 04:56
  • @Tom welcome and happy to know it helped you – mmuzahid Apr 30 '16 at 05:37
  • Scanner provides you with means to split into tokens, and using it to read lines just to tokenize manually later looks wrong. – Basilevs Apr 30 '16 at 06:08
-1

you can use java.io.StreamTokenizer to read String and double. StreamTokenizer split file into tokens with suitable datatype. and by using some constants you can identify the token type. like TT_NUMBER,TT_WORD

File f=new File("path of file");
StreamTokenizer st=new StreamTokenizer(new FileInputStream(file));
while(st.nextToken()!=StreamTokenizer.TT_EOF)   //getting contents of file{
    switch(st.ttype)
    {
        case StreamTokenizer.TT_EOF: break;
        case StreamTokenizer.TT_NUMBER://it will read number always in double data type
            no=st.nval;
        break;
        case StreamTokenizer.TT_WORD:
            name=st.nval;
        break;
    }}
Sharad Gautam
  • 85
  • 6
  • 11