Results 1 to 5 of 5
  1. #1
    suroh is offline Member
    Join Date
    Jul 2015
    Posts
    4
    Rep Power
    0

    Default Issue with if else and String

    Okay let me start off by saying that I'm coming from c++ and ruby and I'm finding the differences hard to grasp in some ways, that being said please take it easy on me lol!

    So I'm attempting to learn java and one of the first things I do when trying out a new language is writing a simple calculator ( Very simple ) that would look some thing like this, and naturally evolve once I've grasped the basics.

    Java Code:
     public class Calculator { 
    
      public static void main(String args[]) { 
         
          String Greetings = "Hello Please choose one of the following \nAddition: \nSubtraction: \nMultiplication: \nDivision: ";
                         
            System.out.println(Greetings);    
                 Scanner menu = new Scanner(System.in);
                     String user_choice; 
                          user_choice = menu.next();
                          
                           /* 
                               So it's my understanding in order to process a String one of the ways you can do it is by using this Scanner in regards to a simple terminal based program I'm used to using cin >> ;  Instead of having to go through 
                               multiple extra steps, so this is one of the things I'm finding difficult to get used to keep in mind I've quite literally just started learning java less then two hours ago...
                               
                               The code up to this point works as intended ( relatively speaking anyway ) but it breaks down when I try to take information that the user input and use it with an if else statement.                 
                          */ 
    
                  
                    if(user_choice == " addition ") {             
              
                           String addition_message = "Please input the two numbers you wish to add"; 
                               System.out.println(addition_message); 
                                  Scanner read_int = new Scanner(System.in); 
                                     int Num1 = read_int.nextInt(); 
                                         Scanner read_int2 = new Scanner(System.in);
                                            int Num2 = read_int2.nextInt();
                                               int result; 
                                                  result = Num1 + Num2; 
                                            
                                                      System.out.println(Num1 + " + " + Num2 + " = " + result); 
                
                } else { 
                    String addition_error_string = " Sorry but that wasn't a choice"; 
                        System.out.println(addition_error_string);
       }
    }
    So the issue I'm having is simple I want the user to type in one of the above choices Ie, "Addition " , " Subtraction " , " Multiplication " , and or "Division ". Once they've done that it would execute the proper if else statement.

    I realize their are many different ways I can do this but as I'm still in my infancy of learning java I really wanted to keep it simple.

    I could set things up in a vector then iterate through it to find what the user is looking for and then do an if else, switch case, while loop etc.. but I don't see why the method above wont work I'm sure I overlooked some thing small and stupid lol... Any help would be greatly appreciated!
    Last edited by suroh; 07-27-2015 at 08:17 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Issue with if else and String

    Please edit your post and wrap the code in code tags:
    BB Code List - Java Programming Forum - Learn Java Programming

    Use the equals() method to compare the contents of two Strings or any objects. The == operator compares the contents of the reference variables, not the contents of the objects pointed to.
    Last edited by Norm; 07-27-2015 at 08:12 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    suroh is offline Member
    Join Date
    Jul 2015
    Posts
    4
    Rep Power
    0

    Default Re: Issue with if else and String

    Yeah sorry about that I wrote it out nice and neat and completely forgot to add the tags ;) Any way thank you very much I'll try your suggestion right now!

    Java Code:
    import java.util.Scanner;
    
    public class Calculator { 
        public static void main(String args[]) { 
            String Greetings = "Hello Please choose one of the following \nAddition: \nSubtraction: \nMultiplication: \nDivision: ";
             
                
            System.out.println(Greetings);    
                Scanner find_me = new Scanner(System.in);
                    String user_choice; 
                        user_choice = find_me.next();
                        
             if(user_choice.equals("Addition")) {              
                String addition_message = "Please input the two numbers you wish to add"; 
                   System.out.println(addition_message); 
                    Scanner read_int = new Scanner(System.in); 
                        int Num1 = read_int.nextInt(); 
                            Scanner read_int2 = new Scanner(System.in);
                                int Num2 = read_int2.nextInt();
                                    int result; 
                                        result = Num1 + Num2; 
                                            
                System.out.println(Num1 + " + " + Num2 + " = " + result); 
                
                } /* else { 
                    String error_string = " Sorry but that wasn't a choice"; 
                        System.out.println(error_string); */
            
        
            if(user_choice.equals("Subtraction")) {
              String subtraction_message = "Please input the two numbers you subtract";  
                System.out.println(subtraction_message);
                    Scanner read_int = new Scanner(System.in);
                        int Num1 = read_int.nextInt();
                            Scanner read_int2 = new Scanner(System.in); 
                                int Num2 = read_int2.nextInt(); 
                                    int result = Num1 - Num2; 
                                        System.out.println(Num1 + " - " + Num2 + " = " + result);     
            } 
                        
            
            if(user_choice.equals("Multiplication")) { 
              String multiplication_message = "Please enter the two numbers you wish to multiply";  
                System.out.println(multiplication_message); 
                    Scanner read_int = new Scanner(System.in); 
                        int Num1 = read_int.nextInt();
                            Scanner read_int2 = new Scanner(System.in); 
                                int Num2 = read_int2.nextInt(); 
                                    int result = Num1 * Num2; 
                                        System.out.println(Num1 + " * " + Num2 + " = " + result);
            } 
            
            if(user_choice.equals("Division")) { 
              String division_message = "Please enter the two numbers you wish to divide";
                System.out.println(division_message);
                    Scanner read_int = new Scanner(System.in); 
                        int Num1 = read_int.nextInt(); 
                            Scanner read_int2 = new Scanner(System.in); 
                                int Num2 = read_int2.nextInt(); 
                                    int result = Num1 / Num2; 
                                        System.out.println(Num1 + " / " + Num2 + " = " + result);
            
            
            }
        } 
    }
    So that fixed my issue right away thank you again for the help! I'm starting to actually get this now haha
    Last edited by suroh; 07-27-2015 at 08:45 PM.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Issue with if else and String

    Please learn how code is supposed to be indented: Code Conventions for the Java Programming Language: Contents

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Issue with if else and String

    Member suroh is blocked for a month for sending the following PM:
    Quote Originally Posted by suroh
    I know exactly how code is supposed to be indented, I also know that it's a matter of personal preference and any one who says otherwise is full of shit.

    Furthermore there was no need to be rude, my code was and is fine. It's clear to me now that not only is java utterly useless but you a moderator are also a piece of shit. So congratulations you've lost a member and some one that was interested in finally learning this shit language that requires thirty extra unnecessary steps that real languages have long since removed :) Good bye douche bag and don't bother responding I wont ever come here again, you'll just be wasting your own time!
    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Java String Issue
    By Skyraz in forum New To Java
    Replies: 2
    Last Post: 02-18-2014, 05:01 AM
  2. String divided issue
    By java1 in forum New To Java
    Replies: 4
    Last Post: 01-29-2012, 06:02 AM
  3. String object comparison issue
    By subith86 in forum New To Java
    Replies: 3
    Last Post: 01-19-2011, 06:18 AM
  4. Issue with String tokenizer..
    By jainanuj82 in forum Advanced Java
    Replies: 6
    Last Post: 06-24-2010, 01:19 PM
  5. [SOLVED] String Length issue.
    By ChrisMayhew in forum New To Java
    Replies: 7
    Last Post: 01-11-2009, 03:16 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
  •