2

I know there are a lot of topics with this error but I cannot find any solution to my problem. I am trying to implement a program which reads a file and part of the data is placed into objects, at the same time these are stuck into one array. Each line of the file is one object which I call Client or VIPClient depending on if the value is true or not. This is the error:

Exception in thread "main" java.util.InputMismatchException: For input string: "1234567890123456"
at java.util.Scanner.nextInt(Scanner.java:2166)
at java.util.Scanner.nextInt(Scanner.java:2119)
at javaapplication1.readfile.readFile(readfile.java:31)
at javaapplication1.main.main(main.java:16)

and this is the code:

public static Client[] readClients(Client[] arraycl){
//arraycl is the empty array where the objects will be placed
        do{
        String name=xclientes.next();
        String dni=xclientes.next(); //dni is id
        int ccnumber=xclientes.nextInt();//the error is here
        int drivingyear=xclientes.nextInt();
        boolean vip=xclientes.nextBoolean(); 
        int vipcard=xclientes.nextInt();
        for (int i=0; i<=arraycl.length ;i++){
            arraycl[i] = new VIPClient(name,dni,ccnumber,drivingyear,0,null,true,vipcard);
            arraycl[i]=new Client(name,dni,ccnumber,drivingyear,0,null,false);
            arraycl[i]=new Client(name,dni,ccnumber,drivingyear,0,null,false);
            arraycl[i]=new Client(name,dni,ccnumber,drivingyear,0,null,false);   
            arraycl[i]= new VIPClient(name,dni,ccnumber,drivingyear,0,null,true,vipcard);
            arraycl[i]= new VIPClient(name,dni,ccnumber,drivingyear,0,null,true,vipcard);
            arraycl[i]=new Client(name,dni,ccnumber,drivingyear,0,null,false);
            arraycl[i]= new Client(name,dni,ccnumber,drivingyear,0,null,false);
            arraycl[i]=new Client(name,dni,ccnumber,drivingyear,0,null,false);
            arraycl[i]= new Client(name,dni,ccnumber,drivingyear,0,null,false);
            closeFile(xclientes);
        }  //end for
        return arraycl;
        } while (xclientes.hasNext());

}//end readClientes 

The 0 and null values will be set afterwards, so here is the text file:

Turing 55550000A 1234567890123456 5 true 1
Knuth 55550001B 0001000000000000 13 false
Wirth 55550010C 0010000000000000 1 false
Dijkstra 55550100D 1100000000000000 2 false
Allen 55551000E 1000000000000000 6 true 2
Curie 55550101M 0101000000000000 3 true 3
Pearl 55550110K 0110000000000000 4 false
Liskov 55551111K 1111000000000000 15 false
Pearl 55550110K 0110000000000000 8 false
Codd 55551110P 1110000000000000 7 false

The structure of the file is:

name(String) id(String) ccnumber(int) drivinyear(int) vip(boolean) vipcard (int)

I think the problem is that netbeans thinks the ccnumber is string which obviously isn't (1234567890123456), or maybe the number is too large (probably not) but I have no idea what to do. I tried to make ccnumber as a String and then parse it but it only gave me more problems. I'd be so grateful if you could give me some advice on how to solve the error.

4
  • MaxValue for int in Java is - 2147483647, clearly your number is too big. Commented Feb 23, 2015 at 17:52
  • But again, I don't think you should be using int or long for ccnumber because you have some entries like 0010000000000000. If you use long then 0010000000000000 will become same as 010000000000000 and 10000000000000. Commented Feb 23, 2015 at 17:57
  • What format should i use then? String? Commented Feb 23, 2015 at 18:09
  • Yes... Strings should work just fine. Commented Feb 23, 2015 at 18:32

2 Answers 2

1

Do not use nextInt. Use nextLong, because 1234567890123456 is out of Integer range (see Integer.MIN_VALUE and Integer.MAX_VALUE here).

Sign up to request clarification or add additional context in comments.

Comments

0

An int uses 32 bits thus ranges from -231 to 231-1, so the maximum is:

maximum bound    2 147 483 648
value    1 234 567 890 123 456

So it's clear you can't parse this in an int. A long has 64 bits so the maximum value is 263-1 or:

maximum bound 9 223 372 036 854 775 807
value             1 234 567 890 123 456

This is thus withing the bounds and can be represented.


Another thing you will have trouble with is that you call:

closeFile(xclientes);

If this does what I think it does, you are closing the stream before going for a new loop. That will probably cause errors.

In fact you're loop doesn't make much sense at all because it will overwrite all data that was originally stored there. Furthermore once you've read a record, you're gonna return. So you will never fetch a second person anyway.

2 Comments

I've just deleted the closeFile(xclientes); and like you said the program only recognizes the first line of the file. How could i fix the array loop to read the second line in the file and fetch the second person/object?
Well first of all: why do you make that many ...Client instances for each person?

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.