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

  • Thread starter Thread starter kllr7
  • Start date Start date
  • Tags Tags
    Error Java
AI Thread Summary
The error message "Exception in thread 'main' java.lang.NumberFormatException: For input string: 'z'" indicates that the program is attempting to convert a non-numeric string, specifically "z", into an integer using Integer.parseInt(). This occurs in the context of reading integers from a file, where the program expects only numeric values. To resolve this issue, it is recommended to display each character read before parsing it to identify non-numeric inputs. Implementing a try-catch block can also help catch the exception when it occurs, allowing the program to handle the error gracefully and print the offending character. Additionally, sharing the contents of the input file (chap56input.txt) would assist in diagnosing compatibility issues between the program's code and the data being read.
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:
Technology 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();
	}
}
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
3
Views
3K
Replies
12
Views
2K
Replies
5
Views
3K
Replies
2
Views
2K
Replies
7
Views
3K
Replies
3
Views
11K
Replies
2
Views
9K
Back
Top