Reading integers from a text file.

G

guitarromantic

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
 
B

Bruce Lee

salesTextFile looks like an Enumeration

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

guitarromantic

Bruce said:
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?
 
M

Matt Humphrey

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?

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 (e-mail address removed) http://www.iviz.com/
 
G

guitarromantic

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);
 
M

Michele Damian

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
}
}
 
G

guitarromantic

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,127
Latest member
CyberDefense
Top