Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Need help with error message in java.

  1. Oct 27, 2008 #1
    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 dont know how to put code in the body.

    Attached Files:

    Last edited: Oct 27, 2008
  2. jcsd
  3. Oct 27, 2008 #2
    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 (Text):

    import java.io.*;
    import java.util.*;
    public class chap56
        public static void main (String[] args) throws IOException,
        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;
            a=a+" z";
            c=new StringTokenizer(a);
            while (b!="z")
                if (x%2==0)
                else z=z+x;
            outFile.println(" sum of even's "+y+"/n sum of odds "+z);
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook