Results 1 to 9 of 9
  1. #1
    jwdxx is offline Member
    Join Date
    Jan 2014
    Posts
    4
    Rep Power
    0

    Default [Solved] problem with simple else if statement

    Started java a couple days ago, and now im working on a calculator. But i'm having a problem with else-if statement. I've made a simple version of the problem here so its easier to identify. If i type in 17, it will give me the answer that i want it to. But if i type in 38, nothing happens.
    Java Code:
    import static java.lang.System.out;
    import java.util.Scanner;
    
    public class Firstclass {
    
    	public static void main(String[] args) {
    		Scanner answer = new Scanner(System.in);
    		out.println("Whats your name?");
    		if (answer.nextLine().equals("bob")) {
    			out.println("How old are you?:");
    			if (answer.nextLine().equals("17")) {
    				out.println("Nice");
    			} else if (answer.nextLine().equals("38")) {
    				out.println("good");
    			}
    		}
    	
    	}
    
    
    }
    Last edited by jwdxx; 01-12-2014 at 03:04 PM.

  2. #2
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: problem with simple else if statement

    what you mean by "nothing happens"? how the program behaves?

  3. #3
    jwdxx is offline Member
    Join Date
    Jan 2014
    Posts
    4
    Rep Power
    0

    Default Re: problem with simple else if statement

    Well as i said if i answer 17, i get the answer and it works fine, but if i answer 38 nothing happens, and once i press enter again, the program terminates. Seems to be a problem with my else if statement.

  4. #4
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: problem with simple else if statement

    ok try to run your program once more and again enter 38 for age question. The same will happen, i.e. your program will wait for input. Now try to enter 38 once more and hit enter, tell me what happens.

  5. #5
    jwdxx is offline Member
    Join Date
    Jan 2014
    Posts
    4
    Rep Power
    0

    Default Re: problem with simple else if statement

    It seems to work after type it twice, thanks milovan! but how do i fix this?

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

    Default Re: problem with simple else if statement

    Don't read another line if you didn't type 17, but store the read line in a variable and test that against 17 ad 38 ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    allan is offline Member
    Join Date
    Jan 2014
    Posts
    7
    Rep Power
    0

    Default Re: problem with simple else if statement

    the problem is that everytime you call next line it takes a new line input 17 hit enter then input 38 hit enter you will have both messages displayed

  8. #8
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: problem with simple else if statement

    well it might be tricky to understand in the beginning, but let me try explain it in simple way.

    You have created Scanner object and every time when you call some of its "next" methods (nextLine() for example), it will read your input from the console.

    Now, in your code, you call nextLine() method in if() loop and therefore it will read and try to compare what you entered with the condition (in your case it is: if("38".equals("17")).

    Since this is not "true" your code is going for else if() but there you have another call for nextLine() method, thus runtime is waiting for next input and enter to be hit.

    Instead of calling so many nextLine() methods, you should rather call it once and keep result in String object and then compare that String in your if-elseif loop.

  9. #9
    jwdxx is offline Member
    Join Date
    Jan 2014
    Posts
    4
    Rep Power
    0

    Default Re: problem with simple else if statement

    Thanks for your fast answers. Fixed the problem by assigning the answer to a string value and using the string for if statements as you suggested

Similar Threads

  1. Replies: 3
    Last Post: 11-17-2011, 04:17 PM
  2. Simple program, simple problem
    By taymilll in forum New To Java
    Replies: 12
    Last Post: 06-20-2011, 06:12 AM
  3. Help with simple control statement
    By Kevin BSU in forum New To Java
    Replies: 3
    Last Post: 11-15-2010, 05:27 PM
  4. a simple if statement
    By senca in forum New To Java
    Replies: 16
    Last Post: 02-25-2010, 10:17 PM
  5. Simple "if" statement problem....compiling error.
    By CYANiDE in forum New To Java
    Replies: 4
    Last Post: 10-14-2009, 10:56 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
  •