Results 1 to 12 of 12

Thread: Loop Issue

  1. #1
    burrish is offline Member
    Join Date
    Sep 2011
    Location
    Texas
    Posts
    17
    Rep Power
    0

    Default Loop Issue

    Hi, everyone. I hate to put up such a bush-league problem as my first post, but I can't quite figure this out. If you run my code, you'll see that "Name 1:" does not prompt for user-entry. Why is this happening?

    Java Code:
     
    
    import java.util.Scanner;
    import java.util.Arrays;
    
    public class Search {
    
    	public static void main(String[] args) {
    	
    		Scanner sc = new Scanner(System.in);
    		String[] fullName = new String[50];
    		String[] bDay = new String[10];
    		int nameCount = 0;
    		
    		System.out.println("How many names would you like to enter? ");
    		nameCount = sc.nextInt();
    		
    		for (int i = 0; i < nameCount; i++) {
    			System.out.println("Name " + (i+1) + ": ");
    			fullName[i] = sc.nextLine();
    			System.out.println("Birthday: ");
    			bDay[i] = sc.nextLine();
    		}
    		
    		
    	}
    
    
    
    }
    Your help is much appreciated!

  2. #2
    Join Date
    Sep 2011
    Location
    Mumbai, India
    Posts
    35
    Rep Power
    0

    Default Re: Loop Issue

    You are facing that problem because this is what you have written in for loop
    for(int i=0;i<nameCount;i++)
    i.e. the all the statements under for-loop should execute if variable i remains less than nameCount.
    your variable i=0 and nameCount is also 0 then how can i<nameCount since both are zero.
    SUMMARY: change your for-loop condition

  3. #3
    burrish is offline Member
    Join Date
    Sep 2011
    Location
    Texas
    Posts
    17
    Rep Power
    0

    Default Re: Loop Issue

    Thank you, Rameshwar.

    I just changed the condition to "i <= nameCount", and it's still skipping the opportunity to enter info for "Name 1:". It still prompts for the first iteration of "Birthday: ", but I guess I'm not approaching the solution in the manner needed.

  4. #4
    loja11 is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default Re: Loop Issue

    I think, the first prompt for "How many names ...." will will already change
    nameCount to a positive number. You can insert a System.out.. methode above
    the if statement to see that. The problem must be something else.

  5. #5
    loja11 is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default Re: Loop Issue

    It may not be efficient, but if you created three scanner objects instead of one, then
    it will wait each time for prompt before going to the next step.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: Loop Issue

    Quote Originally Posted by Rameshwar Soni View Post
    You are facing that problem because this is what you have written in for loop
    for(int i=0;i<nameCount;i++)
    i.e. the all the statements under for-loop should execute if variable i remains less than nameCount.
    your variable i=0 and nameCount is also 0 then how can i<nameCount since both are zero.
    SUMMARY: change your for-loop condition
    This is hogwash; please check the facts before you reply and don't confuse the OP. A readInt() does exactly what its name says: it reads an int if it can. It doesn't read any other character, nor a newline character. If you type a number, say, 123, and press <enter> there are four characters in the input buffer: 123<enter>. the readInt() method reads the first three characters but leaves the <enter> character in the input buffer. A nex readLine() method call 'sees' that <enter> character and assumes an empty String to be read. An easy way out would be to add an additional (dummy) call of the readLine() method to get rid of the <enter> character in the input buffer.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    loja11 is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default Re: Loop Issue

    Does the array pbject still store the entries provided above after the program has finished.
    I tried to print them by creating an instance in another class but all i got was null.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: Loop Issue

    Quote Originally Posted by loja11 View Post
    Does the array pbject still store the entries provided above after the program has finished.
    I tried to print them by creating an instance in another class but all i got was null.
    After a program has finished all of the objects are no more; they are ex-objects, gone to meet their maker, pushing daisies, joined the choir invisible, shuffled of their mortal coil ...
    Show us some code so we can see the details of your intentions.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    burrish is offline Member
    Join Date
    Sep 2011
    Location
    Texas
    Posts
    17
    Rep Power
    0

    Default Re: Loop Issue

    Quote Originally Posted by JosAH View Post
    This is hogwash; please check the facts before you reply and don't confuse the OP. A readInt() does exactly what its name says: it reads an int if it can. It doesn't read any other character, nor a newline character. If you type a number, say, 123, and press <enter> there are four characters in the input buffer: 123<enter>. the readInt() method reads the first three characters but leaves the <enter> character in the input buffer. A nex readLine() method call 'sees' that <enter> character and assumes an empty String to be read. An easy way out would be to add an additional (dummy) call of the readLine() method to get rid of the <enter> character in the input buffer.

    kind regards,

    Jos
    Thanks to everyone for your input.

    Jos, I appreciate the explanation. If my for-loop had been asking for an integer instead of a string, would it be correct to say that this problem wouldn't have happened since readInt() wouldn't be looking for <enter>? I hope I'm reading you, correctly.

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: Loop Issue

    Quote Originally Posted by burrish View Post
    Jos, I appreciate the explanation. If my for-loop had been asking for an integer instead of a string, would it be correct to say that this problem wouldn't have happened since readInt() wouldn't be looking for <enter>? I hope I'm reading you, correctly.
    If there only was an <enter> character in the input buffer and the readInt() method were called, it would block because it considers the <enter> character a white space character and not a digit, i.e. it would wait for a digit in the input buffer; if any other (non white space) character were present in the input buffer, the readInt() method call would fail and it would throw an Exception.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    loja11 is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default Re: Loop Issue

    I just modified the code by burrish above as below. Then i tried the code at the bottom of this post.

    import java.util.Scanner;

    public class Searcher {

    static String[] fullName = new String[50];

    public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    Scanner bc = new Scanner(System.in);

    String[] bDay = new String[10];
    int nameCount = 0;

    System.out.println("How many names would you like to enter? ");
    nameCount = sc.nextInt();

    System.out.println(nameCount);

    for (int i = 0; i < nameCount; i++) {
    System.out.println("Name " + (i+1) + ": ");
    fullName[i] = bc.nextLine();
    System.out.println("Birthday: ");
    bDay[i] = bc.nextLine();
    }

    }

    }

    public class SearcherSupport {
    public static void main(String[] args){
    Searcher susi = new Searcher();

    for (int i = 0; i < 5; i++)
    System.out.println(susi.fullName[i]);
    }



    }

  12. #12
    burrish is offline Member
    Join Date
    Sep 2011
    Location
    Texas
    Posts
    17
    Rep Power
    0

    Default Re: Loop Issue

    Quote Originally Posted by JosAH View Post
    If there only was an <enter> character in the input buffer and the readInt() method were called, it would block because it considers the <enter> character a white space character and not a digit, i.e. it would wait for a digit in the input buffer; if any other (non white space) character were present in the input buffer, the readInt() method call would fail and it would throw an Exception.

    kind regards,

    Jos
    Thanks again, Jos!

    I ended up writing code similar to loja's, but it's unfortunate that something like that is needed to get around this. The way that I'm prompting for names couldn't possibly be that unusual. Pardon my ignorance, but is it common practice to use more than one Scanner variable in a program?

Similar Threads

  1. Java Beginner: Can't Figure out Loop Array Issue.
    By CMDR Reservoir in forum New To Java
    Replies: 4
    Last Post: 07-19-2011, 02:30 AM
  2. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM
  3. Issue with nested for loop
    By sunshine64 in forum New To Java
    Replies: 5
    Last Post: 02-03-2011, 02:45 AM
  4. [Q] Loop issue (while loop)
    By iriscience in forum New To Java
    Replies: 9
    Last Post: 01-31-2011, 04:21 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
  •