Results 1 to 4 of 4
  1. #1
    LasseA is offline Member
    Join Date
    Oct 2012
    Posts
    29
    Rep Power
    0

    Default Scanner and if/else trouble

    I'm trying to make a program in where the user can type in the first letter of either green, red or blue, and then get a outprint saying "red" if he/she typed r or R etc.
    It seems like I'm doing something wrong with the line "String color = s.nextString();". Tried to look in both text book and what I could find by googling, but haven't found the right help. Hopefully I can get a clear hint here.

    Java Code:
    import java.util.Scanner;
    
    public class Chapter4_SelfCheck10 {
    	public static void main(String[] args) {
    		Scanner s = new Scanner(System.in);
    		String color = s.nextString();
    		
    		System.out.println("Choose a color, red, blue or green, type in first letter only");
    		
    		if (color == g || G) {
    			System.out.println("Green");
    		}
    		else if (color == r || R) {
    			System.out.println("Red");
    		}
    		else if (color == b || B) {
    			System.out.println("Blue");
    		} else {
    		System.out.println("You entered an incorrect color");
    		}
    		
    		
    	}
    }

  2. #2
    Yedionbir is offline Member
    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Default Re: Scanner and if/else trouble

    issue one : you must assignment scanner variable then system.out.println("your question/necessery sentence")

    issue two : you must use nextLine methods for string input

    ex i1 & i2:

    Java Code:
    Scanner input  = new Scanner(System.in);
    System.out.println("where do you live"); // suppose "London"
    String city = input.nextLine(); // now city varible is "London"

    issue three : you must use equals method not == significations for comparison two string variable

    ex i3:

    Java Code:
    if(city.equals("London") || color.equals("Paris")){
         System.out.println("well, you are living in Europa");
    so, your code should like this

    Java Code:
    import java.util.Scanner;
    
    public class Chapter4_SelfCheck10{
    
    	public static void main(String[] args){
    		Scanner s = new Scanner(System.in);
    		System.out.println("Choose a color, red, blue or green, type in first letter only");
    		
    		String color = s.nextLine();
    		
    		if(color.equals("g")|| color.equals("G")){
    			System.out.println("Green");
    		}
    		else if (color.equals("r")|| color.equals("R")) {
    			System.out.println("Red");
    		}
    		else if(color.equals("b")|| color.equals("B")){
    			System.out.println("Blue");
    		}
    		else{
    			System.out.println("You entered an incorrect color");
    		}
    	}
    
    }
    sorry for my English :( ... i hope, this reply help to you

  3. #3
    LasseA is offline Member
    Join Date
    Oct 2012
    Posts
    29
    Rep Power
    0

    Default Re: Scanner and if/else trouble

    Thanks a lot! Some of those corrections would had taken me hours to figure out.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,742
    Rep Power
    19

    Default Re: Scanner and if/else trouble

    Please don't spoonfeed full solutions like that.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 11-11-2012, 01:55 AM
  2. Replies: 1
    Last Post: 09-20-2012, 09:54 AM
  3. Scanner/Locale Trouble
    By plm-pusik in forum New To Java
    Replies: 1
    Last Post: 10-15-2010, 07:07 PM
  4. Scanner Trouble
    By Snoman in forum New To Java
    Replies: 3
    Last Post: 09-28-2010, 05:29 AM
  5. Replies: 0
    Last Post: 12-01-2008, 09:50 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
  •