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 9 of 9

Thread: Help please!!!

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Help please!!!

    Hello guys,
    I am taking a summer java 1 course and I have no idea how to do one of the parts of my first project for the course. The assignment is to create a program that determines if a 5 digit number is a palindrome. I am not sure how to create a code that keeps asking the user to reenter a 5 digit number if they entered the wrong number. I am assuming that somehow I have to include a while loop in the code. I was testing the loop for one of the conditions and I was unable to have the loop ask the user for a new number. I could not find any information online on this specific problem. This is what I came up with. Thank you very much for the help.

    ( I tried using smaller numbers to test it)

     
    mport java.util.Scanner;
     
     
    public class Pelindrone {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Scanner input= new Scanner ( System.in);
    int num;
    System.out.println( "Please enter five digit number");
    num = input.nextInt();
    while (10 < num )  { System.out.println(" Enter new number number");}
     
    	}
     
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,973
    Thanks
    51
    Thanked 2,269 Times in 2,241 Posts

    Default Re: Help please!!!

    code that keeps asking the user to reenter a 5 digit number if they entered the wrong number.
    That can be done with a while() loop. Inside the loop: ask the question, get the response, exit the loop if the response is good, otherwise output a message and loop back to ask the question again.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    toddy (May 31st, 2014)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,718
    My Mood
    Mellow
    Thanks
    231
    Thanked 706 Times in 692 Posts

    Default Re: Help please!!!

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    Please give your threads better titles. Moving to Loops and Control Statements.

  5. #4
    Junior Member
    Join Date
    May 2014
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help please!!!

    Thank you very much for your response. How would I go about doing this?

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,973
    Thanks
    51
    Thanked 2,269 Times in 2,241 Posts

    Default Re: Help please!!!

    Write the code as I suggested in post#2:
    while() loop
    ask the question,
    get the response,
    exit the loop if the response is good,
    otherwise output a message and loop back to ask the question again.
    end loop
    If you don't understand my answer, don't ignore it, ask a question.

  7. #6
    Junior Member
    Join Date
    May 2014
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help please!!!

    ok thank you once again. This is what I come up with:
     
    import java.util.Scanner;
     
     
    public class Pelindrone {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Scanner input= new Scanner ( System.in);
    int num;
    System.out.println(" Enter 2 digit number");
    num = input.nextInt();
    while ( num <= 10) { System.out.println("Enter 2 digit number");
    				num=input.nextInt();
    while (num>99) { System.out.println("Enter 2 digit number");
    				num=input.nextInt();
    }
    }
    }
     
    	}

    for some reason it does not want to work. any ideas?

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,973
    Thanks
    51
    Thanked 2,269 Times in 2,241 Posts

    Default Re: Help please!!!

    Please edit the post and fix the formatting of the code so it is readable.
    Logically nested statements within {}s should be indented.
    There should not be one } above another }.
    There should not be any code after a {

    it does not want to work
    Please explain. Copy any output here that shows what you are talking about.

    I see two while() statements. Why are there two?

    Did you see post#5 where I tried to show you the logic? It used ONE while() statement.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    GregBrannon (May 31st, 2014)

  10. #8
    Junior Member
    Join Date
    May 2014
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help please!!!

    Ok thanks a lot and sorry about the format. I was able to make it work by indenting the while loops.

  11. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,973
    Thanks
    51
    Thanked 2,269 Times in 2,241 Posts

    Default Re: Help please!!!

    I was able to make it work by indenting the while loops.
    That is very unusual. The formatting doesn't make code work. Code is formatted so humans can read and understand it more easily. Computers don't care.
    It's the logic that make programs work.
    If you don't understand my answer, don't ignore it, ask a question.