Question

I am having difficulties trying to make an application I am coding to distinguish whether or not the args[] input of the user is a String or an int. If an int value is typed, it will be listed and added in common addition format. If a String value is typed into args[], a list of the ignored arguments is collected (Strin[] ignored) and will be printed for the user to see. I am trying to distinguish this through nested loops that parse the value inputted as an int value and checks to see if this args[] String value is indeed a int, but, being fairly new at programming, I have hit quite the roadblock. Any suggestions?

Here is my code:

public class Addition {


  public static void main(String args[]) {
    int sum = 0;
    String[] ignored = new String[args.length];
    if(args.length == 0) {
      System.out.println("No input values!");
    } else {
      for(int i = 0; i < args.length; i++) {
        if(Character.isDigit(Integer.parseInt(args[i]))) {
          sum += Integer.parseInt(args[i]);
          if(i == 0) {
            System.out.print(args[i]);
          } else if(Integer.parseInt(args[i]) >= 0) {
            System.out.print(" + " + args[i]);
          } else if(Integer.parseInt(args[i]) < 0) {
            System.out.print(" - " + (Integer.parseInt(args[i]) * -1));
          }
        } else if(!Character.isDigit(Integer.parseInt(args[i]))) {
            ignored[i] = args[i];
        }
      } System.out.print(" = " + sum);
    }
  }
}

I am getting this Interactions error:

   java.lang.NumberFormatException: For input string: "-"
   at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at Addition.main(Addition.java:17)  
Was it helpful?

Solution 2

Well, you can either A) determine it's a number, then parse it, or B) try to parse, and handle it gracefully if it fails. Since you're summing things, I'm assuming they SHOULD be a number, and so let's go with B.

Something as simple as this would work in your case:

int badArgs = 0;
for(int i = 0; i < args.length; i++) {

  try {
    int value = Integer.parseInt(args[i]);
    // Do your main logic with value here
  } catch (NumberFormatException e) {
    ignored[badArgs++] = args[i];
  }

Current problems with your code: checking .isDigit as you are makes no sense - it's meant to check a character from a string - you've already parsed it at that point, so if it's going to throw a NumberFormatException, it will have already, before you finish the check. Second: checking if it's a digit doesn't make sense anyhow, as -1 is a valid number, but would obviously fail whatever check you had in mind - and the rest of your code shows you intend to handle negaties.

OTHER TIPS

Ok I'm not that clear what you are trying to do with your numbers but you can actually use the NumberFormatException to achieve what you have to do (separate the numbers and non-numbers).

ArrayList<Integer> numbers = new ArrayList<Integer>();
ArrayList<String> nonNumbers = new ArrayList<String>();

for(String s : args)
{
    try
    {
        int number = Integer.parseInt(s);
        numbers.add(number);
    }
    catch (NumberFormatException nfe)
    {
        nonNumbers.add(s);
    }
}

// rest of your code can use numbers and nonNumbers lists
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top