Results 1 to 12 of 12
  1. #1
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default break statement is ignored.

    in this "program" there is two break statements, the first one is ignored, for what reson?

    What am I trying to do is enter info for x number of students(firstname, lastname and civic creg number), and if that student already exists, you will reenter the info.

    Java Code:
    import pjjava.nbie.*;
    import java.util.*;
    import java.lang.*;
    import java.io.*;
    
    class Pojahns
    {
    	public static void main (String[] args) throws Exception
    	{
    		Scanner	in = new Scanner (System.in);
    
    		System.out.println ("How many studens are there in your class?");
    		int students = in.nextInt ();
    
    		String[] info = new String[students];
    		System.out.println ("Please specify the student information by FIRSTNAME LASTNAME CIVI-CREG-NUMBER");
    		for (int i = 0; i < info.length; i++)
    		{
    			while (true)
    			{
    				info[i] = in.nextLine ();
    
    				[B]if (i == 0)[/B]	//The first time you enter student info is there nothing to compare with, so we break the loop.
    					[B]break;[/B]
    
    				for (int j = 0; j != i; j++)
    				{
    					boolean comparing = info[i].equals (info[j]);
    
    					if (comparing == false)
    					{
    						System.out.println ("That person already exists!!");
    						continue;
    					}
    
    					else
    						break;
    				}
    			}
    		}
    	}
    }
    Also tried this method:

    http://download.oracle.com/javase/tu...LabelDemo.java

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    That isn't so much your problem, while it doesn't LOOK like it is calling break, it is. You can test this by adding print statements.

    Java Code:
    System.out.println(i);
    			while (true)
    			{
    				info[i] = in.nextLine ();
    				if (i == 0)	{//The first time you enter student info is there nothing to compare with, so we break the loop.
    					System.out.println("In if");
    					break;
    				}
    Replacing the correct lines with this will help point out that it is in fact breaking out of the loop(even if it doesn't look like it).

    Some other errors you are getting: One has to do with scanners and not swallowing an end of line.

    Another is, if comparing == false, that means info i is not equal to info j, so why are you saying the person exists? Shouldn't you be saying that person exists if they are the same?

    There are some other small changes you will have to fix. Try using print statements to print what you think values should be at certain points, if they aren't what you expect, try to figure out why.

    This can probably be done more elegantly as well. It was a challenge for me to spot and correct the flaws.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    Never compare a boolean to true or false. It's redundant and also bad coding practice.
    Java Code:
    // if (comparing == false)
    if (!comparing)
    // or, in the context of your code, still better:
    if (!info[i].equals (info[j]))=
    db

  4. #4
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    ok the problem is definitely(but not 100%) that the input buffer(right word?) not being cleaned after I press enter. I added code to clean it but it still wont work. Nothing make sense.

    Java Code:
    import pjjava.nbie.*;
    import java.util.*;
    import java.lang.*;
    import java.io.*;
    
    class Pojahns
    {
    	public static void main (String[] args) throws Exception
    	{
    		Scanner	in = new Scanner (System.in);
    
    		System.out.println ("How many studens are there in your class?");
    		int students = in.nextInt ();
    		in.nextLine ();
    
    		String[] info = new String[students];
    		System.out.println ("Please specify the student information by FIRSTNAME LASTNAME CIVI-CREG-NUMBER");
    		for (int i = 0; i < info.length; i++) //Also tried replacing info.length with students
    		{
    			while (true)
    			{
    				info[i] = in.nextLine ();
    				in.nextLine ();
    
    				if (i == 0)
    				{
    					System.out.println ("Sys1");
    					break;
    				}
    
    				for (int j = 0; j != i; j++)
    				{
    					System.out.println ("Sys2");
    
    					if (info[i].equals (info[j]))
    					{
    						System.out.println ("That person already exists!!");
    						continue;
    					}
    					else
    					{
    						System.out.println ("Sys3");
    						break;
    					}
    				}
    			}
    		}
    	}
    }
    Could it be that the stirng array is missing a split code? I am not sure what to do if split is required here.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    What is happening when you run this?
    You haven't told us what's happening that shouldn't be, and what should happen.

  6. #6
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    382
    Rep Power
    3

    Default

    Correct me if I'm wrong, but won't this always create an infinite loop for i>=1? The first break skips out of the while(true), sure, but the second one should skip out of the for-loop, right?

  7. #7
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    First I enter number of students. Then I enter student information two times before Sys1 is printed. Thats why I think this have to do with the inputbuffer not being cleaned.
    But I am still unsuccessful.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    That's part of the problem, there is some other logic to work out as well. Give me 35 mins and I'll help you work it out. But try to think about it logically, what is the purpose of the program? Try to break it down.

  9. #9
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    382
    Rep Power
    3

    Default

    Quote Originally Posted by Pojahn_M View Post
    First I enter number of students. Then I enter student information two times before Sys1 is printed. Thats why I think this have to do with the inputbuffer not being cleaned.
    But I am still unsuccessful.
    With the current code, that's not surprising; you have two calls to in.nextLine (); after all. Remove the last one of them and you should only have to input it once (aside from any possible other bugs).

  10. #10
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    well that did diffrence, but still not working. the code is a mess, I am rewriting it. I report back soon.

  11. #11
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    After a lot of struggling, I got it to work! :)

    Java Code:
    import pjjava.nbie.*;
    import java.util.*;
    import java.lang.*;
    import java.io.*;
    
    class Pojahns
    /*********************
    System.out.println ();
    System.out.print ("");
    *********************/
    {
    	public static void main (String[] args) throws Exception
    	{
    		Scanner	in = new Scanner (System.in);
    
    		System.out.println ("How many studens are there in your class?");
    		int students = in.nextInt ();
    		in.nextLine ();
    
    		String[] info = new String[students];
    		System.out.println ("Please specify the student information by FIRSTNAME LASTNAME CIVI-CREG-NUMBER");
    
    		for (int i = 0; i < info.length; i++)
    		{
    			info[i] = in.nextLine ();
    
    			if (i == 0)
    				continue;
    
    			for (int j = 0; i != j; )
    			{
    				if (info[i].equals (info[j]))
    				{
    					System.out.println ("That student already exists!");
    					i--;
    					break;
    				}
    
    				else
    				{
    					j++;
    					continue;
    				}
    			}
    		}
    	}
    }
    But I am not done, is there ways I can make this in a easier way/make it look more professional?

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I'm glad you solved it. I was actually able to solve it with the 3 original loops you had, so there is more than one way to solve it.

    I think perhaps it would look more elegant if you created a separate method which takes a string and a string array and determines if the array contains the string. Then you can prompt for input and determine if it's already contained. If it is, continue the loop.

    Java Code:
    for(int i = 0; i < info.length; ++i){
      String s = in.nextLine();
      while(arrayContains(s, info)){
        //re-prompt
      }
      info[i] = s;
    }
    This should work, I didn't actually test it, but the logic seems like it would work.

    edit: tested, logic works. It has a few nice things, it looks cleaner and simpler, but it also separates the array check from the loop. You create a static helper method which does the checking for you. It does the same thing as putting the loop inside the main method but it makes it simpler to test, since you can test the helper method independent of the rest of the stuff.
    Last edited by sunde887; 05-09-2011 at 09:43 PM.

Similar Threads

  1. How do you properly use break?
    By louist in forum New To Java
    Replies: 3
    Last Post: 03-08-2011, 02:58 AM
  2. Replies: 10
    Last Post: 01-07-2011, 10:28 AM
  3. break statement
    By AbdulAziz Bader in forum New To Java
    Replies: 17
    Last Post: 06-02-2010, 03:42 PM
  4. How to use Break with a label
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:45 PM
  5. How to use Break
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •