2 Replies Latest reply: Nov 4, 2009 4:27 AM by gimbal2 RSS

    Reading file and parseInt - PROBLEM

    843789
      Hi, I am trying read from .txt file but I have a problem when I use Integer.parseInt. Can you see what could be wrong in this source:

      public String loadPlayerFromTxt() {
      String result = "";
      try{
      FileInputStream fstream = new FileInputStream("Test.txt");
      DataInputStream in = new DataInputStream(fstream);
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
      int position = 0;

      while ((br.readLine()) != null) {
      Gamer gamer = new Gamer();

      gamer.setName(br.readLine());
      gamer.setRace(br.readLine());
      gamer.setRole(br.readLine());

      gamer.setLevel(Integer.parseInt(br.readLine()));

      String [] array = new String [6];

      String abilities = br.readLine();
      String str = abilities.trim();
      //System.out.println(abilities);
      array = str.split(",");
      gamer.setStrength(Integer.parseInt(array[0]));
      gamer.setEndurance(Integer.parseInt(array[1]));
      gamer.setDexterity(Integer.parseInt(array[2]));
      gamer.setIntelligence(Integer.parseInt(array[3]));
      gamer.setWisdom(Integer.parseInt(array[4]));
      gamer.setCharisma(Integer.parseInt(array[5]));
      this.gcollection.addGamer(gamer,position);

      position++;
      result = "succesful";
      }
      in.close();
      }
      catch (Exception e){//Catch exception if any
      System.err.println("Error: " + e.getMessage());
      e.printStackTrace();
      e.getLocalizedMessage();
      }
      return result;
      }

      Error message is:
      Error: For input string: "12, 15, 19, 9, 14, 7"
      java.lang.NumberFormatException: For input string: "12, 15, 19, 9, 14, 7"
           at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
           at java.lang.Integer.parseInt(Integer.java:456)
           at java.lang.Integer.parseInt(Integer.java:497)
           at ca5.GameControle.loadPlayerFromTxt(GameControle.java:56)
           at ca5.GameControle.main(GameControle.java:112)
        • 1. Re: Reading file and parseInt - PROBLEM
          800581
          Pavel.Prochy wrote:
          Hi, I am trying read from .txt file but I have a problem when I use Integer.parseInt. Can you see what could be wrong in this source:

          Error: For input string: "12, 15, 19, 9, 14, 7"

          java.lang.NumberFormatException: For input string: "12, 15, 19, 9, 14, 7"
          The Integer.parseInt() method expects a String that contains only numbers. You are passing it the following string:
          12, 15, 19, 9, 14, 7
          Which contains punctuation and whitespace (i.e. not just numbers). When the string you pass to Integer.parseInt() contains anything but numbers you get the "NumberFormatException" thrown. What you need to do is to do some pre-processing on the string you read from the file or (ideally) change the format of the file so that you have each of the numbers on their own line rather than having multiple numbers on the same line. If you HAVE to keep all the numbers on the same line then you will need to do the following (or something very similar to it):
          public class Tester3 {
              public static void main(String[] args) {
                 String input = "12, 15, 19, 9, 14, 7";
                 
                 String[] strings = input.split(",");
                 
                 for(String string : strings) {
                      int parameter = Integer.parseInt(string.trim());
                      
                      System.out.println("parameter: "+parameter);
                 }
              }
          }
          Program output from above code:
          parameter: 12
          parameter: 15
          parameter: 19
          parameter: 9
          parameter: 14
          parameter: 7
          When you use the Integer.parseInt() call you should consider the possibility that your input will cause the call to fail. You can pre-process the input to ensure it will not fail, catch the NumberFormatException by wrapping the call to Integer.parseInt() in a try-catch block or create a method in your class that throws a NumberFormatException to its caller and let the caller handle the error

          Edited by: amp88 on Nov 4, 2009 2:26 AM

          Edited by: amp88 on Nov 4, 2009 2:27 AM
          • 2. Re: Reading file and parseInt - PROBLEM
            gimbal2
            Welcome to the forum. If you want help, then make it possible for people to help you. This means investing time into asking a proper question, simply dumping your code here and saying "do all the work for me!" is not going to get you any constructive replies. Here are a few tips:

            1. use the code formatting tags to format your code. You can do this by selecting your code and then pressing the code button above the input area. This makes it far easier to read

            2. If you get an error, post the stacktrace

            3. Post what output you are getting and what output you are expecting.