简体   繁体   中英

Unable to input “*” in terminal for Java

I am trying to write a simple calculator in java that takes in an input in the console.

 eg. java 30 - 7  (works)
     java 30 * 7  (doesn't work)

After a little debugging, it seems that * is parsed into the main method as the class name instead of String '*'.

 eg. public static void main(String[] args) {
          System.out.println(args[1]);
          // (returns Calculator.java and Calculator.class)
     }

Does anyone know why? And is there a way to resolve this?

it seems that * is parsed into the main method as the class name instead of String '*'.

That's not what is happening.

The shell you're using (probably bash) expands * to match all files in the current directory, before java is called. This is known as globbing and has nothing to do with Java. If you don't want the argument to be parsed by the shell, pass a string:

java some_app '30 * 7'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM