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
Click For Summary
SUMMARY

The error message "java.lang.NumberFormatException: For input string: 'z'" indicates that the Java program is attempting to convert a non-numeric string, specifically "z", into an integer using the Integer.parseInt() method. This occurs in the context of reading integers from a file, where the program encounters unexpected characters. To resolve this issue, implement a try-catch block to handle the exception gracefully and print the offending character. Additionally, ensure that the program checks for non-numeric input before attempting conversion.

PREREQUISITES
  • Understanding of Java exception handling
  • Familiarity with the Integer.parseInt() method
  • Knowledge of file I/O in Java
  • Experience with string manipulation and tokenization in Java
NEXT STEPS
  • Implement a try-catch block to handle NumberFormatException in Java
  • Learn about Java file input/output operations
  • Explore the use of StringTokenizer for parsing strings in Java
  • Research best practices for validating user input in Java applications
USEFUL FOR

Java developers, software engineers, and students learning about exception handling and file processing in Java.

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();
	}
}
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
4K
Replies
1
Views
8K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 22 ·
Replies
22
Views
6K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 3 ·
Replies
3
Views
11K
  • · Replies 2 ·
Replies
2
Views
9K