0

I'm trying to format the String sequence "a b c" where a b and c are all whole number integers.I need to add a b and c to 3 separate arrays.Here is what I have. When prompted for the str, the error of NumberFormatException keeps showing up.

I have tried changing up some variables and their declarations and looking at other explanations online.

import java.util.*;
import javax.swing.*; 
public static void main(String[] args){
     Scanner s = new Scanner(System.in);
     System.out.println("How Many times do you want to loop it?"); //Scanner takes in input
     int loop = s.nextInt();

     //if someone can help me format this better or have a 3d array that would be helpful.

     ArrayList<Integer> a1 = new ArrayList<Integer>(); //3 arrays
     ArrayList<Integer> b1 = new ArrayList<Integer>();
     ArrayList<Integer> c1 = new ArrayList<Integer>();

     String a = ""; //Int #1
     String b = ""; //Int #2
     String c = ""; //Int #3
     String strSpc = " "; //String Spaces to compare with actual String
     int x = 0;
     String str = " ";

     //Start of the error

     for(int i=0; i<loop;i++) {
         str = JOptionPane.showInputDialog(i+1 + ": ");

         //checks format

         while(str.charAt(x) !=(strSpc.charAt(0))){
             a = a + str.charAt(x);
             x++;
         }
         int a2 = Integer.parseInt(a);
         a1.add(a2);

         while(str.charAt(x+1) !=(strSpc.charAt(0))){
             b = b+str.charAt(x);
             x++;
         }
         int b2 = Integer.parseInt(b);
         b1.add(b2);

         while(str.charAt(x+1) !=(strSpc.charAt(0))){
             c = c+str.charAt(x);
             x++;
         }
         int c2 = Integer.parseInt(c);
         c1.add(c2);

         System.out.print('\n');
     }
}
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • [How can I prevent java.lang.NumberFormatException: For input string: “N/A”?](https://stackoverflow.com/questions/18711896/how-can-i-prevent-java-lang-numberformatexception-for-input-string-n-a) – Sudhir Ojha Sep 13 '19 at 05:37
  • That exception is straight forward: you try to parse a string " " that is empty as number. Empty strings aren't numbers. Honestly, your whole code ... doesn't make much sense. For example: you keep ever increasing x. But when you fetch that `str` variable in a loop. Meaning: the length of str might be different each time. I think you should step back and re-think what you try do here. And maybe take to your peers. This is just extremely complicated code, doing almost nothing useful. Honestly: step back, and consider starting from scratch again. – GhostCat Sep 13 '19 at 05:39

1 Answers1

0

You seem to understand that when the first while loop completes, str.charAt(x) would be a space, so in the second while loop, you started by checking str.charAt(x+1), skipping the space. However, in the body, you are still adding str.charAt(x) to b, which would have been a space.

 while(str.charAt(x+1) !=(strSpc.charAt(0))){ // x + 1 here
     b = b+str.charAt(x); // x here
     x++;
 }

You should change it to:

 while(str.charAt(x+1) !=(strSpc.charAt(0))){ // x + 1 here
     b = b+str.charAt(x+1); // x + 1 here as well
     x++;
 }

The same goes for the third for loop. This time, it needs to check charAt(x+2) and put append charAt(x+2) to c, because charAt(x+1) by that time would have been a space.

Last but not least, you need to reset x = 0; at the start of each iteration of the outer for loop.

 int x = 0; <---- move this....
 String str = " ";

 for(int i=0; i<loop;i++) {
    <---- here
     str = JOptionPane.showInputDialog(i+1 + ": ");

In fact, what you are doing is just splitting up str, which can be done by the split method:

String[] splits = str.split(" ");
int a = Integer.parseInt(splits[0]);
int b = Integer.parseInt(splits[1]);
int c = Integer.parseInt(splits[2]);
a1.add(a);
b1.add(b);
c1.add(c); 
Sweeper
  • 213,210
  • 22
  • 193
  • 313