Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Question about IF Statement

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Question about IF Statement

    Hey guys,

    Just have a question here about IF Statement. Basically when I type in the word "end" I need the program to terminate.

    However, with this code, the program terminates no matter what string/characters I enter.

    Just wondering would anyone know how I would solve this ?

    Thanks

    import java.util.Scanner;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class Doorknob 
     
    {
    	public static void main (String args[])
    	{	
     
    		int x=0, answer=0;
    		int number;
     
    		Scanner input=new Scanner (System.in);	
     
     
    	do{
    		ProjectGenerator c = new ProjectGenerator();
     
    		System.out.println("");
    		System.out.println("What is " + c.getRandomInt() + " * " + c.getRandomInt2() + "?");
     
     
    		while(!input.hasNextInt())
    				{
     
    				if (input.nextLine().equals("end"));
    					{
    					System.exit(0); 
    				}
     
     
    					System.out.println("Incorrect. Please enter only digits and try again.");//www.stackoverflow.com
    					System.out.println("");
    					System.out.println("What is " + c.getRandomInt() + " * " + c.getRandomInt2() + "?");
    					input.next();
    				}
     
    		int result = input.nextInt();
    		answer = (c.getRandomInt()*c.getRandomInt2());
     
    			if (result == answer)
    				{
    					System.out.println("Well done");
    				}
     
    			else
     
    				while (result != answer)
    				{
    					System.out.println("Unfortunately, that's not correct. Please try again.");
    					System.out.println("");
    					System.out.println("What is " + c.getRandomInt() + " * " + c.getRandomInt2() + "?");
     
    				while(!input.hasNextInt())
    				{
    					System.out.println("Incorrect. Please enter only digits and try again.");//www.stackoverflow.com
    					System.out.println("");
    					System.out.println("What is " + c.getRandomInt() + " * " + c.getRandomInt2() + "?");
    					input.next();
    				}
     
    					result = input.nextInt();
     
    					if (result == answer)
    						{
    							System.out.println("Well done");
    						}					
    				}
     
    		x++;
    		}
    	while(x <= 5);
    	}	
     
    	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Question about IF Statement

    the program terminates no matter what
    Where and how does the program terminate? System.exit() or by returning?
    Add some println statements to print out what is entered and saved in variables
    and also to show program execution flow.

    You need to add the -Xlint option to the javac command to see what problems there are:
    javac.exe -Xlint Doorknob.java
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question about IF Statement

    Thanks for the reply.

    It terminates by returning.

    All the user types in is just the answer to the multiplication question asked.

    I've never came across the -Xlint option before ..

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Question about IF Statement

    I've never came across the -Xlint option before .
    Did you try it? It will give a message telling you want is wrong with your code.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question about IF Statement

    I'm not getting any errors when I compile my code.

    Is that what you mean ?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Question about IF Statement

    Did you use the -Xlint option with the javac command? It should show you a problem when you use it.


    I always use it and I get a message from the compiler with your code. You need to change how you use the compiler so that you also get the message.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. I need more if-statement help
    By DarkPrince in forum Loops & Control Statements
    Replies: 1
    Last Post: November 7th, 2012, 10:41 PM
  2. Question about else statement
    By jean28 in forum Java Theory & Questions
    Replies: 1
    Last Post: September 23rd, 2012, 11:52 PM
  3. Need help with if statement.
    By dreamer in forum Loops & Control Statements
    Replies: 2
    Last Post: May 30th, 2012, 02:27 PM
  4. If statement help
    By Legion of Daughters in forum Loops & Control Statements
    Replies: 12
    Last Post: September 6th, 2011, 08:25 AM
  5. [SOLVED] Switch statement question
    By shikh_albelad in forum Loops & Control Statements
    Replies: 5
    Last Post: May 31st, 2009, 05:13 AM