0

I've been given this question but I think there's something wrong with my code.

Write a Java Program to take in 3 command line arguments for calculation. The first and third argument must be an Integer. The second argument must be either + - x / The program will print out the result of the calculation if the 3 arguments are correct. Do not use switch, only use if else.

I input the following arguments:

1 '*' 2

And then get "You have entered an invalid operator" as a result. If I remove '*' and replace it in with just *:

1 * 2

My code cannot work at all and I get the following error:

Exception in thread "main" java.lang.NumberFormatException:

For input string: ".project"

at java.lang.NumberFormatException.forInputString(Unknown Source)

at java.lang.Integer.parseInt(Unknown Source)

at java.lang.Integer.parseInt(Unknown Source)

at Command1.main(Unknown Source)

Any idea how I can correct my code? I wondered if I should replace char with String instead but couldn't get the symbol to work either.

Code:

import java.util.Scanner;
public class Command1 {

public static void main(String[] args) {
    int num1 = Integer.parseInt(args[0]);
    char symbol = args[1].charAt(0);
    int num2 = Integer.parseInt(args[2]);
    System.out.println(args[0]+" "+args[1]+" "+args[2]+" = ");
    if(symbol=='+')
    {
        System.out.println(num1+num2);
    }
    else if(symbol=='-')
    {
        System.out.println(num1-num2);
    }
    else if(symbol=='*')
    {
        System.out.println(num1*num2);
    }
    else if(symbol=='/')
    {
        System.out.println(num1/num2);
    }
    else
    {
        System.out.println("You have entered an invalid operator.");
    }
    // TODO Auto-generated method stub

 }

}
andolsi zied
  • 3,553
  • 2
  • 32
  • 43
  • Exception in thread "main" java.lang.NumberFormatException: For input string: ".project" means you are trying to parse the String ".project" to a number. Seeing as ".project" isn't a number, that causes problems – Stultuske Nov 13 '17 at 09:27
  • @notyou primitive types don't have an equals implementation. his comparisons are correct. – Stultuske Nov 13 '17 at 09:27
  • @Stultuske I see it's a `char`, I had only scanned over the question initially! – achAmháin Nov 13 '17 at 09:29
  • what is the IDE you are using to run the project? – Ravindra Ranwala Nov 13 '17 at 09:35
  • @RavindraRanwala eclipse –  Nov 13 '17 at 09:37
  • `*` might be a special character in your console/terminal. Using `'*'`might be a way to escape it, but `"*"` is another way to escape it. Have you tried `1 "*" 2` as input? – luke Nov 13 '17 at 10:11
  • 1
    Do some debugging. Or put `System.out.printf("\"%s\"%n", args[1];` in your `main()` and see exactly what you are passing to Java. `'*'` would work on a Linux command line, but not on a Windows command line. It depends where you are putting the command line. – DodgyCodeException Nov 13 '17 at 10:42
  • The * is being interpreted as a wildcard and expanded to a list of files (which is why the second error message mentions `.project`). The proper way to pass the argument will depend on the console you are using. As for the first error, add some debugging info to see exactly which args are being passed to your `main()` method. – Grodriguez Nov 13 '17 at 10:44
  • 2
    Since * is used as a command for all files in command prompt, we cannot use it as its is. We can pass this as '*', but it will not fit into your current generic code. Hence as mentioned in your question (+ - x /) use 'X' instead. After all it is for to understand command line argument concept, Isn't it. – Tejendra Nov 13 '17 at 11:10

2 Answers2

0

Your problem is simply the use of the quotes you are passing as the arguments.

Using ' or no quote will result in the two errors you quote above.

Try using double quotes:

java Command1 5 "*" 2
achAmháin
  • 4,176
  • 4
  • 17
  • 40
  • double quotation marks gives the same error as without quotation marks at all –  Nov 14 '17 at 09:18
  • By any chance did you not put a space before and after the `"`? – achAmháin Nov 14 '17 at 09:26
  • Try this answer I posted yesterday: https://stackoverflow.com/a/47267093/6722100 except replace the last the last line with `java Command1 5 "*" 2` – achAmháin Nov 14 '17 at 09:27
0

Using double quotation mark for the Symbol you can get answer. Like 4 "*" 2

Joseph Charles
  • 656
  • 6
  • 17