0

We received a question on making a program for entering the name of people and their gender(n number) and to store the boys and girls in two separate arrays. I wrote the following code but it does not accept both the name and the gender from the second loop. Why?

import java.io.*;

class arrays
{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

    void main()throws IOException
    {
        String name="";
        System.out.println("enter number of students");
        int n=Integer.parseInt(br.readLine());
        String[] c=new String[n];//5
        String[] b=new String[n];//5
        String[] g=new String[n];//5
        char[] s=new char[n];
        System.out.println("enter the name and gender of "+n+" students");
        int i=0;

        do
        {
            System.out.println("enter the data of "+(i+1)+" student");
            c[i]=br.readLine();
            s[i]=(char)br.read();
            i++;
        }
        while(i<n);

        for(int j=0;j<n;j++)
        {
            if(s[j]=='b'||s[j]=='B')
            {
                System.arraycopy(c,j,b,j,1);
            }
            else if(s[j]=='g'||s[j]=='G')
            {
                System.arraycopy(c,j,g,j,1);
            }
        }
        for(int j=0;j<n;j++)
        {
            System.out.print("boys are:-"+b[j]);
            System.out.println("girls are:-"+g[j]);
        }
    }
}
2
  • 1
    Proper indentation (or, really, ANY) would help... Commented Jan 1, 2014 at 14:28
  • which loop is not iterating second time? Commented Jan 1, 2014 at 14:28

2 Answers 2

1

The way input is given is the issue here. Change do-while loop to this:

do {
        System.out.println("enter the data of " + (i + 1) + " student");
        c[i] = br.readLine();
        s[i] = (char) br.read();            
        br.readLine();       // even a br.read(); would work. Used to read newline
        i++;
   } while (i < n);
Sign up to request clarification or add additional context in comments.

Comments

0

Your problem is that when you only do br.read() which behaves differently from readLine(). readLine() reads up to and includes the new-line but removes the new-line from the response. So if you enter "the name<new-line>" "the name" is returned but "<new-line>" is consumed. However, the read() reads just one character and leaves the rest as is, so when you enter "b<new-line>" 'b' is returned while "<new-line>" is left on the input stream. So the next time you come around asking for a name using readLine the input will be parsed up to the the next "" which happens to be the new-line left when entering the gender with no characters before it, thus an empty string is returned.

You can test this using the existing program and the following input:

3
name1
bname2
gname3
b

So you need to make sure the new-line is consumed, perhaps by using a br.readLine()after reading the gender. If you try to consume the new-line by reading an extra character beware that on some systems new-line is actually two characters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.