Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

error while reading a file

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!I want to read some values from a file which i do not know how many lines it has each time!My file is something like that
160 0.23
156 1.2 ...
I make an arraylist to store these values but when I run my project i have this error "Exception in thread "main" java.util.InputMismatchException" in double y=sc.nextDouble();
my code is that:
       File file = new File("");

       
       Scanner sc = new Scanner(file);
       ArrayList<Cores> c=new ArrayList<Cores>();
       

       while(sc.hasNext()){
          int x=sc.nextInt();
          double y=sc.nextDouble();
          Cores c1=new Cores(x,y);
          c.add(c1);
           
       }
       System.out.println(c);






       sc.close();

   }
Could you suggest me something for these problem??
 
Rancher
Posts: 3714
34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is in the file at the location where the exception happens?  Print out the value of x after  it is read to show where the program was trying to read.
Then look at the input file where that value for x is located to find the bad input value.
 
Mark Spen
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:What is in the file at the location where the exception happens?  Print out the value of x after  it is read to show where the program was trying to read.
Then look at the input file where that value for x is located to find the bad input value.


Thank you for your advice!I tried what you said me,  but still my code it reads only the variable x and always appears error when reads the variable y.
 
Rancher
Posts: 4545
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to see what's going on (since I guess this is happening on the first row?), change the nextInt and nextDouble to next() and print out the values.

Or just this:
 
Mark Spen
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:Just to see what's going on (since I guess this is happening on the first row?), change the nextInt and nextDouble to next() and print out the values.

Or just this:



With your advice,my code print all the data from the file. I change 0.03 to 0,03 and print me both my variable x and y.The only problem i have is that my code print my arraylist like that [Cores@5afa04c, Cores@6ea12c19]
 
Dave Tolls
Rancher
Posts: 4545
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The decimal format was going to be my next suggestion.
Well caught.

For the other issue, your Cores class has not overridden the toString method and so uses the default toString from Object.
You'll need to override toString if you want your println call to output something from your Cores objects.
 
Mark Spen
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:The decimal format was going to be my next suggestion.
Well caught.

For the other issue, your Cores class has not overridden the toString method and so uses the default toString from Object.
You'll need to override toString if you want your println call to output something from your Cores objects.



I fixed my code and my probleb was solved!Thank you all for you help and advice!!
 
A timing clock, fuse wire, high explosives and a tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic