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

Thread: I cannot get this loop to stop running. What am I doing wrong??

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    15
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default I cannot get this loop to stop running. What am I doing wrong??

    This loop is supposed to take a user inputted string, compare it to a string variable of another class that is stored in an array and if they're equal it is supposed to have the user edit the information and set the new variables. I can get it to exit, but after all of the new info is entered it just prompts the user for the information again. Any help would be greatly appreciated. Thanks

    public static void changeData(Student [] x){
    		String sName="";
    		String newFirst;
    		String newLast;
    		String newPhone;
    		double newGPA;
    		String test="";
     
    		boolean same=false;
    		Scanner input=new Scanner(System.in);
     
    		System.out.println("Enter name of student you wish to edit: ");
    		sName=input.nextLine();
     
    		for(int i=0;i<x.length;i++)
    		{	
    			test=x[i].getName();
     
    		if(sName.equalsIgnoreCase(test))	
    		{			    		
     
    		     System.out.println("New data for student: ");
    		     System.out.println("Enter first name: ");
    			 newFirst=input.nextLine(); 
    			 System.out.println("Enter last name: ");
    			 newLast=input.nextLine();
    			 System.out.println("Enter new phone number: ");
    			 newPhone=input.nextLine();
    			 System.out.println("Enter new GPA: ");
    			 newGPA=input.nextDouble();
     
    			 x[i]=new Student(newFirst,newLast,newPhone,newGPA);
     
    			break;	
    		}
    		else
    		{	
    		   System.out.println("That name doesn't exit in our records"); 
    		   break;
    		}   	
     
     
     
    		}
    	}


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: I cannot get this loop to stop running. What am I doing wrong??

    What happens after the loop? Could you post an SSCCE demonstrating what's going on?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: I cannot get this loop to stop running. What am I doing wrong??

    Yea it seems like your running through all of that code more than once because of code outside of the loop. Maybe you should try putting a marker like a print statement at the top of all that code in the changeData method to see if its running through changeData more than once, or just the for loop.
    Unless somehow x.length; is never ending. once you add another thing/Student to x is the length increased?
    Actually thats kind of what it looks like whats happening. It looks like your adding a new Student, so x.length() is bigger, then i is smaller than then length so it has to run again.

Similar Threads

  1. Button Won't Stop Animation.
    By SyntheticD in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 24th, 2011, 02:27 PM
  2. Cant get my code to stop jumping
    By Mob31 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 2nd, 2011, 07:03 AM
  3. Stop a while loop with an awt button, problem
    By frx08 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 20th, 2011, 08:24 AM
  4. Replies: 4
    Last Post: April 27th, 2010, 01:18 AM
  5. radio buttons stop working
    By tabutcher in forum AWT / Java Swing
    Replies: 2
    Last Post: March 5th, 2010, 09:28 AM