Results 1 to 11 of 11
Like Tree2Likes
  • 1 Post By gimbal2
  • 1 Post By gimbal2

Thread: what am I doing wrong?

  1. #1
    javanoob3 is offline Member
    Join Date
    Sep 2014
    Posts
    3
    Rep Power
    0

    Default what am I doing wrong?

    import java.util.Scanner;


    public class FirstClass {
    public static void main (String[] args) {

    Scanner one = new Scanner(System.in);
    int x, y;
    int total = 0;
    String oper = "start";

    System.out.print("Would you like to add, subtract, divide or multiply?");
    oper = one.next();
    System.out.println("You chose to " + oper);

    System.out.print("Please enter a value for x: ");
    x = one.nextInt();
    System.out.println("Entered: " + x);

    System.out.print("Please enter a vale for y: ");
    y = one.nextInt();
    System.out.println("Entered: " + y);


    if (oper == "add") {
    total = x + y;
    } else if (oper == "subtract") {
    total = x - y;
    } else if (oper == "multiply") {
    total = x * y;
    } else if (oper == "divide") {
    total = x / y;
    }

    System.out.println(total);
    one.close();
    }
    }

  2. #2
    javanoob3 is offline Member
    Join Date
    Sep 2014
    Posts
    3
    Rep Power
    0

    Default Re: what am I doing wrong?

    Java Code:
    import java.util.Scanner;
    
    
    public class FirstClass {
    public static void main (String[] args) {
    
    Scanner one = new Scanner(System.in);
    int x, y;
    int total = 0;
    String oper = "start";
    
    System.out.print("Would you like to add, subtract, divide or multiply?");
    oper = one.next();
    System.out.println("You chose to " + oper);
    
    System.out.print("Please enter a value for x: ");
    x = one.nextInt();
    System.out.println("Entered: " + x);
    
    System.out.print("Please enter a vale for y: ");
    y = one.nextInt();
    System.out.println("Entered: " + y);
    
    
    if (oper == "add") {
    total = x + y;
    } else if (oper == "subtract") {
    total = x - y;
    } else if (oper == "multiply") {
    total = x * y;
    } else if (oper == "divide") {
    total = x / y;
    }
    
    System.out.println(total);
    one.close();
    }
    }

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: what am I doing wrong?

    Several things:

    First, you didn't tell us what the problem is?
    Second, you didn't place your code between code tags (however, since you're new, that is okay). Go to advanced
    editor, highlight your code and click the # on the menu.

    Regarding your program, regardless of what it is supposed to do, you must use equals and not == to compare
    strings.

    e.g
    for Strings and other objects
    use a.equals(b), not a == b.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    javanoob3 is offline Member
    Join Date
    Sep 2014
    Posts
    3
    Rep Power
    0

    Default Re: what am I doing wrong?

    Sorry about that. Thanks for the feedback. The problem was total was just printing 0 in the end. Now that I changed == to equals() the code works! Why wouldn't == work though?

    Java Code:
    import java.util.Scanner;
    
    
    public class FirstClass {
    	public static void main (String[] args) {
    		
    		Scanner one = new Scanner(System.in);
    		int x, y;
    		int total = 0;
    		String oper;
    		
    		System.out.print("Would you like to add, subtract, divide or multiply?");
    		oper = one.next();
    		System.out.println("You chose to " + oper);
    		
    		System.out.print("Please enter a value for x: ");
    		x = one.nextInt();
    		System.out.println("Entered: " + x);
    		
    		System.out.print("Please enter a vale for y: ");
    		y = one.nextInt();
    		System.out.println("Entered: " + y);
    		
    
    		if (oper.equals("add")) {
    			total = x + y;
    		} else if (oper.equals("subtract")) {
    			total = x - y;
    		} else if (oper.equals("multiply")) {
    			total = x * y;
    		} else if (oper.equals("divide")) {
    			total = x / y;
    		}
    		
    		System.out.println("The answer is " + total);
    		one.close();
    	}
    }

  5. #5
    Hamenopi is offline Nub
    Join Date
    Sep 2014
    Location
    Houston
    Posts
    8
    Rep Power
    0

    Default Re: what am I doing wrong?

    == tests for reference (pointer) equality.

    .equals() tests for value equality.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: what am I doing wrong?

    Man, everyone is making calculators right now. Even programming assignments are subject to fashion it seems!

    We've seen that == over equals() mistake so many times now I feel it is time for a Luscious Paint Example (LPE).
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: what am I doing wrong?

    Alright, here goes: Luscious Paint Example time! Hi, I'm Gimby and I suck at paint.

    Lets sketch a situation where we have two variables which we lazily call "var1" and "var2". These variables point to their own String objects - to make it interesting those two String objects have the exact same value.

    Java Code:
    String var1 = new String("Tea");
    String var2 = new String("Tea");
    In picture form:

    what am I doing wrong?-obj_ref_ex2.png

    As the picture indicates, var1 == var2 will result in FALSE because the two variables actually have a different value in them; a reference to a different object.

    Now we change the situation.

    Java Code:
    var2 = var1;
    Both var1 and var2 now reference the exact same object. In picture form:

    what am I doing wrong?-obj_ref_ex3.png

    And because we now made both variables point to the exact same object, we have created the one possible scenario where var1 == var2 will return true.

    As you can see in the second picture, we have now severed all ties to the other String object; it is just floating in space alone and forgotten. That is the moment where the ever hungry garbage collector of Java will fly by and eat it up.


    This story is true for any and all object types. However there is a secondary trap specifically to do with specific objects such as String and Integer. Take this example:

    Java Code:
    String var1 = "Tea";
    String var2 = "Tea";
    Here we have actually without really knowing it created the situation in the second picture; var1 and var2 point to the exact same String object. This is to do with the String literal pool; a great optimization built into Java that has confounded many a Java programmer both learning and experienced. Many rules of HowItWorks(tm) are broken by the introduction of object pooling.

    EDIT: grrrr, the forum does not let me get rid of those uncropped attachments :/
    Attached Thumbnails Attached Thumbnails what am I doing wrong?-obj_ref_ex2.png   what am I doing wrong?-obj_ref_ex3.png  
    Last edited by gimbal2; 09-17-2014 at 11:04 AM.
    Hamenopi likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: what am I doing wrong?

    Quote Originally Posted by gimbal2 View Post
    Alright, here goes: Luscious Paint Example time! Hi, I'm Gimby and I suck at paint.
    You can say that again; I've never seen Donald Duck painted that badly ...

    kindest regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: what am I doing wrong?

    Hey, as long as the story is told well, it doesn't matter if you print it on gold plated papyrus or toilet paper.
    milovan likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: what am I doing wrong?

    Story well told? Where are Huey, Dewey, and Louie then? This isn't much of a story; quack!

    kindest regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: what am I doing wrong?

    Peasant!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. What is wrong with this?
    By MW130 in forum New To Java
    Replies: 4
    Last Post: 03-08-2013, 09:59 AM
  3. I can't find anything wrong with this but somehow it's wrong.
    By Biscuit Tickler in forum New To Java
    Replies: 2
    Last Post: 09-12-2012, 09:28 PM
  4. What am I doing wrong?
    By DuncanHollows in forum New To Java
    Replies: 2
    Last Post: 04-08-2012, 05:33 PM
  5. What am I doing wrong??
    By NoNickName in forum New To Java
    Replies: 3
    Last Post: 04-23-2009, 11:04 PM

Posting Permissions

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