#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Beginner. Useless program for training. User input not producing desired effect.


    I'm sorry to bother you all with this thing, because in reality, it's a completely useless program, but completing it will get me that much closer to a grasp of Java. I tried searching for the answer first, but I couldn't find the information I need.

    To be honest, I'm not sure if what I'm trying to do is possible, but here goes; I'm trying to make it so that a specific user input (in this case, "Life is like a") causes a random number between 0 and 3 to be generated, and that number to correlate with some Strings in an array, so it will randomly print one of the messages included in the array. Eclipse doesn't show any actual errors in my code, but even if the code is sound, it's still not working the way I intended. When you run the program and input "Life is like a", it doesn't output anything.

    I know my code is probably sloppy and weird...I'm just starting out...

    Code:
    import java.util.Random;
    import java.util.Scanner;
    class Gumpy {
    	public static void main(String args[]){
    		Random rand = new Random();
    		Scanner input = new Scanner(System.in);
    		String gump = "Life is like a";
    		
    		String life[]={" box of chocolates."," beanstalk, isn't it?"," pig's ***; salty and full of wonder."," sick game where nobody wins."};
    		int option = rand.nextInt(4);
    		
    		if(input.nextLine() == gump){ 
    		System.out.println(life[option]);
    		}
    	}
    }
  2. #2
  3. Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    May 2004
    Location
    Superior, CO, USA
    Posts
    2,418
    Rep Power
    1713
    In Java, the "==" operator for objects doesn't quite do what you want. It basically compares the address of the left hand and right hand objects, not the contents of the objects. The somewhat frustrating thing is that for primitives (int, float, etc.) the "==" operator does work the way you want.

    For String and other Objects you want to use the equals() method. So something more like:

    java Code:
    import java.util.Random;
    import java.util.Scanner;
    class Gumpy {
    	public static void main(String args[]){
    		Random rand = new Random();
    		Scanner input = new Scanner(System.in);
    		String gump = "Life is like a";
     
    		String life[]={" box of chocolates."," beanstalk, isn't it?"," pig's ***; salty and full of wonder."," sick game where nobody wins."};
    		int option = rand.nextInt(4);
     
    		if(input.nextLine().equals(gump)){  // <font color="Red">notice the difference here</font>
    		System.out.println(life[option]);
    		}
    	}
    }
    Last edited by stdunbar; May 10th, 2013 at 03:50 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0
    Beautiful! It totally worked! I did not know there was such a thing as "equals()". The first things I tried were things like:

    if(input = gump)
    ~~~
    if(input.next = gump)
    ~~~
    if(input.nextLine(gump))

    Bah. Thank you so much for the help! And it works exactly how I intended.

IMN logo majestic logo threadwatch logo seochat tools logo