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

Thread: Simple loop problem

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Simple loop problem

    At least I think the solution to my problem will involve a loop. I'm new to java, so I'm sorry if I'm on the wrong board.
    Basically, my code asks the user to enter a number from 1 - 12 and it then converts their number to the corresponding month, and displays it. If the enter 13 then the programs quits. My problem is, if the enter any number other than 1-13, my code displays an 'invalid entry' message and prompts them to try again. How do I enter the code to make the program loop back to the beginning and let them enter a new number if their first try was invalid?

    Here's my code:

    import java.util.Scanner;
     
     
    public class Months {
     
    	//variables
    	private int numberEntered;
     
    	//constructor
    	public Months(){
    	}
     
    	//methods
    	public void getNumberFromUser(){
    		Scanner input = new Scanner(System.in);
    		System.out.print("Please enter the numeric value of a month (13 to quit): ");
    		numberEntered = input.nextInt();
    	}
     
    	public void displayMonth(){
    		if(numberEntered == 1)
    			System.out.println("You have chosen January");	
    		else if(numberEntered == 2)
    			System.out.println("You have chosen February");
    		else if(numberEntered == 3)
    			System.out.println("You have chosen March");
    		else if(numberEntered == 4)
    			System.out.println("You have chosen April");
    		else if(numberEntered == 5)
    			System.out.println("You have chosen May");
    		else if(numberEntered == 6)
    			System.out.println("You have chosen June");
    		else if(numberEntered == 7)
    			System.out.println("You have chosen July");
    		else if(numberEntered == 8)
    			System.out.println("You have chosen August");
    		else if(numberEntered == 9)
    			System.out.println("You have chosen September");
    		else if(numberEntered == 10)
    			System.out.println("You have chosen October");
    		else if(numberEntered == 11)
    			System.out.println("You have chosen November");
    		else if(numberEntered == 12)
    			System.out.println("You have chosen December");
    		else if(numberEntered == 13)
    			System.out.println("Thanks for playing. Goodbye!");
    		else
    			System.out.println("Your entry is invalid. Please try again.");
    	}
     
    	public static void main(String[] args) {
    		Months userEntry = new Months();
     
    		userEntry.getNumberFromUser();
    		userEntry.displayMonth();
    	}
     
    }

    Any help is really appreciated.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    975
    Thanks
    7
    Thanked 205 Times in 181 Posts

    Default Re: Simple loop problem

    You don't say what is supposed to happen if the user enters a number 1->12. Should it repeat or not?

    You could use a while or do loop:

    public static void main(String[] args) {
        Months userEntry = new Months();
     
        boolean finished = false;
        while(!finished) { 
            userEntry.getNumberFromUser();
            userEntry.displayMonth();
            finished = ...
        }
    }

    You can examine the value the user entered to determine what the value of finished should be set to at the end of the loop.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Simple loop problem

    Sorry, my mistake. It is supposed to keep repeating until the user enters 13. How do I go about setting a value to finished?
    Last edited by tyneframe; October 13th, 2012 at 08:40 PM.

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    975
    Thanks
    7
    Thanked 205 Times in 181 Posts

    Default Re: Simple loop problem

    As I suggested, you set finished by looking at the value of numberEntered. Almost certainly an if statement will be involved: if numberEntered has certain value you can set finished to true, and the program will end.

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Simple loop problem

    Hm, I'm still not sure what I'm doing wrong. I've tried entering the boolean and setting the result based on the numberEntered just like you said, but I keep getting an error that says "cannot make a static reference to the non-static field numberEntered'

    Am I placing the code in the wrong place?
    public static void main(String[] args) {
    		Months userEntry = new Months();
     
    		Boolean finished = false;
    		while (!finished){
    		userEntry.getNumberFromUser();
    		userEntry.displayMonth();
    			if(numberEntered = 13)
    				finished = true;
    		}
    	}
    }

  6. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    975
    Thanks
    7
    Thanked 205 Times in 181 Posts

    Default Re: Simple loop problem

    Just little things: (1) "boolean" and "Boolean" are different types (2) "=" and "==" are different operators and

    cannot make a static reference to the non-static field numberEntered
    (3) What the compiler is getting at is that numberEntered is variable that belongs to a particular instance of Months just like the methods getNumberFromUser() and displayMonth(). And, like those those methods, you must use userEntry when referring to the value:

    userEntry.numberEntered

  7. The Following User Says Thank You to pbrockway2 For This Useful Post:

    tyneframe (October 14th, 2012)

  8. #7
    Junior Member
    Join Date
    Sep 2012
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Simple loop problem

    Thank you so much for the help! It works perfectly now. Can't believe I forgot the == thing.
    Thanks again.

  9. #8
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    975
    Thanks
    7
    Thanked 205 Times in 181 Posts

    Default Re: Simple loop problem

    You're welcome.

Similar Threads

  1. hello! can i please get some help with a simple loop problem?
    By zerocool18 in forum Loops & Control Statements
    Replies: 6
    Last Post: October 13th, 2012, 11:31 AM
  2. Simple Do Loop Problem
    By inflames098 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 10th, 2012, 04:43 PM
  3. One of the most simple loop awnsers
    By Firestar912 in forum Loops & Control Statements
    Replies: 8
    Last Post: May 7th, 2012, 08:13 PM
  4. [SOLVED] WHILE LOOP USING || (or) SIMPLE PROBLEM
    By SPACE MONKEY in forum Loops & Control Statements
    Replies: 3
    Last Post: May 7th, 2012, 01:23 AM
  5. Replies: 4
    Last Post: May 15th, 2011, 07:03 AM