Results 1 to 19 of 19
  1. #1
    srisar is offline Member
    Join Date
    Jul 2009
    Location
    batticaloa, Sri Lanka
    Posts
    30
    Rep Power
    0

    Default String is not accepting "*" letter, please help me to solve

    Java Code:
    public class SimpleCalc {
    
        public static void main(String[] args) {
    
            int a = 0, b = 0, c = 0;
            String sign = null;
            boolean err = false;
    
            try {
                a = Integer.parseInt(args[0]);
                sign = args[1];
                b = Integer.parseInt(args[2]);
            } catch (Exception e) {
                System.out.println("Error in arguments input, program terminated");
                err = true;
            }
    
            if (sign.equals("+")) {
                c = a + b;
            } else if (sign.equals("-")) {
                c = a - b;
            } else if (sign.equals("*")) {
                c = a * b;
            } else if (sign.equals("/")) {
                c = a / b;
            } else {
                err = true;
            }
    
            if (err) {
                System.out.println("Error in operation, program terminated...");
                System.exit(0);
            } else {
                System.out.println("Rersult of the calculation is " + c);
            }
    
        }
    }
    all the calculations are working exept multiply, i dont know why, please help me to solve this

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    run this app with 2 + 4 as the args and then 2 * 4, and note the difference:
    Java Code:
    public class ReadArgs {
      public static void main(String[] args) {
        for (String arg : args) {
          System.out.println("-" + arg + "-");
        }
      }
    }
    * has meaning on the command line you know.

  3. #3
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    c:\> java ReadArgs 2 "*" 4
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  4. #4
    corlettk is offline Member
    Join Date
    Apr 2009
    Location
    Brisbane
    Posts
    86
    Rep Power
    0

    Default

    As others have alluded to, but never quite said explicitly... on the command line, you need to quote the star (i.e: "*") to supress it's "special" meaning to the command shell (star means "all files" in both Winblows and *nix command shells).

    *SimpleCalculator.java*
    Java Code:
    package forums;
    
    public class SimpleCalculator
    {
      public static void main(String[] args) {
        try {
          int a = Integer.parseInt(args[0]);
          char sign = args[1].charAt(0);
          int b = Integer.parseInt(args[2]);
    
          int result = 0;
          switch (sign) {
            case '+': result = a + b; break;
            case '-': result = a - b; break;
            case '*': result = a * b; break;
            case '/': result = a / b; break;
            default: throw new IllegalArgumentException("unknown sign: "+sign);
          }
          System.out.println(a+" "+sign+" "+b+" = "+result);
        } catch (Exception e) {
          System.err.println(e.toString()+": "+e.getMessage());
        }
      }
    }
    *SimpleCalculatorTest.bat* (note that the star is doublequoted).
    Java Code:
    ---------- run bat ----------
    
    C:\Java\home\src\forums>cd /d c:\java\home\src\forums 
    
    C:\Java\home\src\forums>javac -d c:\java\home\classes SimpleCalculator.java 
    
    C:\Java\home\src\forums>java -cp c:\java\home\classes forums.SimpleCalculator 1 + 1 
    1 + 1 = 2
    
    C:\Java\home\src\forums>java -cp c:\java\home\classes forums.SimpleCalculator 5 - 2 
    5 - 2 = 3
    
    C:\Java\home\src\forums>java -cp c:\java\home\classes forums.SimpleCalculator 2 "*" 1 
    2 * 1 = 2
    
    C:\Java\home\src\forums>java -cp c:\java\home\classes forums.SimpleCalculator 4 / 2 
    4 / 2 = 2
    
    Output completed (1 sec consumed)
    Cheers. Keith.

  5. #5
    srisar is offline Member
    Join Date
    Jul 2009
    Location
    batticaloa, Sri Lanka
    Posts
    30
    Rep Power
    0

    Default

    thanks guys..
    im sorry for late reply, because i faced a power failure, so i couldnt reply..
    thank goodness there are somany good people out here to help a new comer...

    thanks a lot again....

  6. #6
    srisar is offline Member
    Join Date
    Jul 2009
    Location
    batticaloa, Sri Lanka
    Posts
    30
    Rep Power
    0

    Default

    i think i have a proble, i tried the same way, i dont know why, by the way im using netbeans, and im passing arguments through Project config - arguments, and it all worked for all other expreesions, but not for this * mark...i dont know

    corelttk, i tried ur method and it didnt seems to work.. i gave me the same result as before

    java.lang.NumberFormatException: For input string: "build.xml": For input string: "build.xml"

    i think maybe there is a problem in netbeans???

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    My advice: the command line is not a good place for a program like this as it forces too many kludges on you. How about doing it in a console window or (even better) a GUI.

  8. #8
    srisar is offline Member
    Join Date
    Jul 2009
    Location
    batticaloa, Sri Lanka
    Posts
    30
    Rep Power
    0

    Default

    yes you are right, it is not a big deal, using a bufferedreader to read from console, but it is my curiosity why it didnt work?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Did you run the program that I posted? If so, the results should suggest the why here.

  10. #10
    srisar is offline Member
    Join Date
    Jul 2009
    Location
    batticaloa, Sri Lanka
    Posts
    30
    Rep Power
    0

    Default

    -2-
    -build-
    -build.xml-
    -manifest.mf-
    -nbproject-
    -src-
    -test-
    -2-

    this is the result it gave me

  11. #11
    srisar is offline Member
    Join Date
    Jul 2009
    Location
    batticaloa, Sri Lanka
    Posts
    30
    Rep Power
    0

    Default

    cn u explain what is this
    String arg : args
    as a beginner, i could not figure this out

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    If you also include this line in my code:
    Java Code:
    System.out.println(System.getProperty("user.dir"));
    You'll see that it outputs a directory on your computer. Go to that directory and tell me the names of the files and folders that are present. Then you'll understand that * can be used as a wildcard for directorys and files on the command line and how this works.

    best of luck.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    cn u explain what is this
    String arg : args
    This is a fancy for loop, called a for-each loop
    Java Code:
        for (String arg : args) {
          System.out.println("-" + arg + "-");
        }
    It is equivalent to this
    Java Code:
        for (for int i = 0; i < args.length; i++) {
          System.out.println("-" + args[i] + "-");
        }
    One word of unasked for advice: Please avoid unnecessary abbreviations in your posts here. I recommend this for several reasons:

    1) Programming is an exercise in precision. When you communicate here (or anywhere) about programming issues and questions, you want this communication to be as clear as possible to avoid any chance for ambiguity. I'd say at least half the answers here are requests for clarification. Let's avoid that.

    2) For many here, English is not their first (or second or third) language. It's hard enough for them to understand what people are posting here much less if it's couched in obscure and non-standard abbreviations.

    3) Extra letters don't cost anything here, so you might as well use them.

    4) It makes the poster seem a bit immature and thus for some not worthy of help.

  14. #14
    srisar is offline Member
    Join Date
    Jul 2009
    Location
    batticaloa, Sri Lanka
    Posts
    30
    Rep Power
    0

    Default

    I got your advice. sorry i will not use any abbreviations anymore.., by the way thanks for the explanations on for each loop:

    yes i saw the directories and got the point, but sorry for disturbing you again and again, how can i use this "*" character as input argument
    Last edited by srisar; 07-12-2009 at 04:23 PM.

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    so if you go to the C:\Users\Sri Saravana\Desktop\Java Works\Sample_Studies directory using Windows Explorer (if you are using a Windows OS), I'll bet you'll find these directories and files: build, build.xml, manifest.mf, nbproject, src, test

  16. #16
    srisar is offline Member
    Join Date
    Jul 2009
    Location
    batticaloa, Sri Lanka
    Posts
    30
    Rep Power
    0

    Default

    yes i did. so is there is no other way to use this char '*', i did tried this : " * ", and it did worked, so it means i have to use space between this char.

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Is it your teacher's requirement that you do this on the command line? Again, the best recommendation is not to do it this way unless you are being forced.

  18. #18
    srisar is offline Member
    Join Date
    Jul 2009
    Location
    batticaloa, Sri Lanka
    Posts
    30
    Rep Power
    0

    Default

    yes, my teacher want me to take input like arguments, because he said input machanism is much complex than System.out.println(); so he said we will use arguments for now, but this calculator idea was mine. but im little bit worried that i could not finish it fully.
    as you said it is not a big deal i think..., i will try to use bufferedReader instead. i found it some where on this forum some samples.

  19. #19
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    you could also use 'X' and 'x' instead of '*'.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

Similar Threads

  1. Replies: 3
    Last Post: 05-21-2009, 03:48 PM
  2. Replies: 0
    Last Post: 04-02-2009, 07:02 PM
  3. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  5. How to solve "No compiler error"?
    By iceman in forum New To Java
    Replies: 5
    Last Post: 04-22-2008, 03:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •