What Does java.lang.NumberFormatException: For input string: 'z' Mean in Java?

  • Context: Java 
  • Thread starter Thread starter kllr7
  • Start date Start date
  • Tags Tags
    Error Java
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 3K views
kllr7
Messages
1
Reaction score
0
i need to know what the error messege below means and how to fix it.

Exception in thread "main" java.lang.NumberFormatException: For input string: "z"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:447)
at java.lang.Integer.parseInt(Integer.java:497)
at chap56.main(chap56.java:23)

it came from the program below which was created and compiled with jGrasp.
it should read any number of integers from a file and output the sum of the evens and the odds to a file. sorry about the attachment i don't know how to put code in the body.
 

Attachments

Last edited:
Physics news on Phys.org
A quick answer is that the program encounters a non-numeric character "z" when it expects a numeric digit.
The best way is to display every character the program reads before you do the command Integer.parseInt(b).
An even better way is to implement a try-catch construct so that when the exception occurs, it will be caught, and you can print the offending character.

If you would like further help, it would certainly be easier if you post the data file (chap56input.txt), or part of it so we can see if the programme code is compatible with what it reads. To post the data file, just open it with an editor or Word, and copy part of the text and paste it onto the message screen of this forum.


For the benefit of other readers, here's the code:
Code:
import java.io.*;
import java.util.*;
public class chap56
{
	public static void main (String[] args) throws IOException,
														FileNotFoundException
	{
	BufferedReader inFile = new
			BufferedReader(new FileReader("C:\\Documents and Settings\\Ryan\\My Documents\\Travis\\Travis school\\comp programing\\java\\programs\\chap. 5\\chap56input.txt"));  
	PrintWriter outFile =  new
				PrintWriter(new FileWriter("C:\\Documents and Settings\\Ryan\\My Documents\\Travis\\Travis school\\comp programing\\java\\programs\\chap. 5\\chap56output.out"));
		String a,b;
		StringTokenizer c;
		int x,y,z;
		y=0;
		z=0;
		a=inFile.readLine();
		a=a+" z";
		c=new StringTokenizer(a);
		b=c.nextToken();
		while (b!="z")
		{
			x=Integer.parseInt(b);
			if (x%2==0)
				y=y+x;
			else z=z+x;
			b=c.nextToken();
		}
		outFile.println(" sum of even's "+y+"/n sum of odds "+z);
		outFile.close();
	}
}