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

Thread: A little help with looping my array

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

    Default A little help with looping my array

    Nearing the end of this assignment and am having a little trouble figuring out a way to get my loop to return to the proper place each time it repeats. Here's the section of code I'm working on:
    public static void main(String[] args) {
     
    		Scanner input = new Scanner(System.in);
    		final int size = 100; //max array size
    		int numEmployee = 0; //number of array entries
    		int i;
    		int choice=0;
    		Employee [] employeeData = new Employee[size]; //an array of objects
    		employeeData[numEmployee] = new Employee();
     
     
    		System.out.println("1. Enter new employee data");
    		System.out.println("2. Display employee data");
    		System.out.print("3. Quit \n=> ");
    		choice = input.nextInt();
     
    		while(choice!=3){
    			if(choice == 1){
    				employeeData[numEmployee].setFirstName();
    				employeeData[numEmployee].setLastName();
    				employeeData[numEmployee].setHoursWorked();
    				employeeData[numEmployee].setPayRate();
    				numEmployee++;
    			}else if(choice == 2){
    				for(i = 0; i<numEmployee; i++);
    				employeeData[i].getFirstName();
    				employeeData[i].getFirstName();
    				employeeData[i].getHoursWorked();
    				employeeData[i].getPayRate();
    			}else{
    				System.out.println("Invalid entry. Please try again.");
    			} //close if
    		} //close while
    		System.out.println("You have quit.");
    	} //close main

    After each run through the while loop, until the user enters 3, I need the code too loop back to the menu section and ask the user to choose between 1, 2, and 3 again. Usually I would just call the method name at the end of the loop, but there is no method name in this case...or is there a way to create a type of method inside main? I tried but was only getting errors. Any help is appreciated.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: A little help with looping my array

    A basic rule to follow: Anything that needs to be repeated needs to be inside of the loop. So if you need to re-display a menu, then that code should be inside of the while loop, not before the while loop.

    And no, you cannot directly nest methods inside of another method in Java (although you can indirectly by nesting classes, but let's not go there just yet).

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

    tyneframe (November 24th, 2012)

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

    Default Re: A little help with looping my array

    Hey, thanks. That did the trick... but also brought up a new problem it seems. After I select 1 for the first time and enter some information it loops me back to the menu and asks me to select again. But this time, I get a 'Null Pointer Exception' error when I try to enter another set of information. How can a value be null the second time through, but not the first?

    This seems to be an array problem now. Should I move my question to the proper board?

  5. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: A little help with looping my array

    Please post your latest edition of the code, please post the error message, and please indicate by comment in the code which line is causing the NPE, something like

    public class Foo {
     
       public Foo() {
         bar();
         baz(); //  ******** NPE occurs here *******
      }
    }

    so we can easily find it.

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

    Default Re: A little help with looping my array

    Here's the updated code, and the error message is:
    "Exception in thread "main" java.lang.NullPointerException at Employee.main(Employee.java:109)"

    public static void main(String[] args) {
     
    		//declare an object
    		Scanner input = new Scanner(System.in);
    		final int size = 100; //max array size
    		int numEmployee=0; //number of array entries
    		int i;
    		int choice=0;
    		Employee [] employeeData = new Employee[size]; //an array of objects
    		employeeData[numEmployee] = new Employee();
     
    		while(choice!=3){
    			System.out.println("1. Enter new employee data");
    			System.out.println("2. Display employee data");
    			System.out.print("3. Quit \n=> ");
    			choice = input.nextInt();
     
    			if(choice == 1){
    				employeeData[numEmployee].getUserEntry(); //******** NPE occurs here *******
    				numEmployee++;
    			}else if(choice == 2){
    				for(i = 0; i<numEmployee; i++);
    				System.out.println(employeeData[i].getFirstName()+"\t"+employeeData[i].getLastName());
    			}else if(choice == 3){
    				System.out.println("You have quit.");
    			}else{
    				System.out.println("Invalid entry. Please try again.");
    			} //close if
    		} //close while
    	} //close main

Similar Threads

  1. Looping Over and Over Again?
    By avalanche72 in forum Loops & Control Statements
    Replies: 1
    Last Post: February 1st, 2012, 05:11 AM
  2. Need help with looping!
    By crsoccerplayer6 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 29th, 2011, 05:09 PM
  3. Looping through an Array.
    By kl2eativ in forum Loops & Control Statements
    Replies: 4
    Last Post: June 10th, 2011, 08:15 PM
  4. Help in looping
    By endframe in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 28th, 2010, 03:24 PM
  5. Looping through a multidimensional array
    By MysticDeath in forum Loops & Control Statements
    Replies: 2
    Last Post: October 11th, 2009, 06:41 PM