Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

Reading integers from a text file.

Thread view: 
guitarromantic@gmail.com - 18 Mar 2006 02:42 GMT
Hey everyone.
I need to read the ints (specifically, to find the highest) from a text
file structured like this:

Surname 100
Foo 12
Bar 13 etc

My code to loop through the values gives an error despite compiling:

     int highestSales = Integer.MIN_VALUE;
     while (salesTextFile.hasNext())
     {
       int i = salesTextFile.nextInt();
       if (i > highestSales)
       {
         highestSales = i;
         System.out.println(highestSales);
       }
     }

(salesTextFile is a BufferedReader/FileReader input stream). This code
gives me an InputMismatchException error - any help?

Matt
Bruce Lee - 18 Mar 2006 03:14 GMT
salesTextFile looks like an Enumeration

reader readline
String a = String.split(" ")[1]
int i = Integer.parseInt(a);

> Hey everyone.
> I need to read the ints (specifically, to find the highest) from a text
[quoted text clipped - 21 lines]
>
> Matt
guitarromantic@gmail.com - 18 Mar 2006 15:41 GMT
> salesTextFile looks like an Enumeration
>
> reader readline
> String a = String.split(" ")[1]
> int i = Integer.parseInt(a);

So basically that's exploding the string into two arrays? That would
work, but my instructions are to use the String.next and nextInt
methods to obtain the number - is there a way I can do this using that
solution?
Matt Humphrey - 19 Mar 2006 00:00 GMT
> Hey everyone.
> I need to read the ints (specifically, to find the highest) from a text
[quoted text clipped - 19 lines]
> (salesTextFile is a BufferedReader/FileReader input stream). This code
> gives me an InputMismatchException error - any help?

I'm looking at the J2SE 5.0 API and I don't see that BufferedReader has a
hasNext() or nextInt () methods.    So what's the real story?  If the source
is a text file you are somehow going to have a parse the input lines to
separate the string and digit characters (also a string) and to convert the
digit characters into an int.

Matt Humphrey matth@ivizNOSPAM.com  http://www.iviz.com/
guitarromantic@gmail.com - 19 Mar 2006 01:01 GMT
Sorry, this is a new technique for me, using a FileReader to get data
from text files, and I probably worded that badly. My instructions were
these:

Hint: You can read the file using a Scanner, making use of Scanner's
next()
and nextInt() methods. Scanner has another useful method called
hasNext()
which will return false when the scanner can find no more tokens (i.e.
it has
reached the end of the text in the file)

Here's the code preceeding what I posted above.

   BufferedReader inputStream2 = null;
   try
   {
     inputStream2 = new BufferedReader(new FileReader(path + filename
+ ".txt"));
     Scanner salesTextFile = new Scanner(inputStream2);
opalpa@gmail.com opalinski from opalpaweb - 19 Mar 2006 05:05 GMT
> Surname 100
> Foo 12
> Bar 13 etc

> (salesTextFile is a BufferedReader/FileReader input stream). This code
> gives me an InputMismatchException error - any help

Surname, Foo, and Bar are not integers.  I did not run  your code, but
trying to parse those non-integer strings as integers, that fits with
InputMismatchException.

all the best,
Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
Michele Damian - 19 Mar 2006 11:31 GMT
> while (salesTextFile.hasNext())
>      {
>        int i = salesTextFile.nextInt();

When you check if there is token you have also to check if that token
is an int.
If this is an int you can read it with the nextInt() method, otherwise
you have to go to the next token with the next() method.

> This code gives me an InputMismatchException error

Because you try to read a token that isn't an int with the nextInt()
method.

You can try a code like this:

while(salesTextFile.hasNext()) {   //Check if there's another token

if(salesTextFile.hasNextInt()) {   //Check if the next token is an int
 temp = salesTextFile.nextInt();

 if(temp > highestSales) highestSales = temp;

}
else {
 salesTextFile.next();   //Go to the next token
}
}
guitarromantic@gmail.com - 19 Mar 2006 14:50 GMT
Thanks Michele, this is exactly what I was looking for. I need to work
on my logic skills, I knew the solution would be something
straightforward like that, but I've only been programming since October
so I'm not fully developed yet.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.