Array from user input

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stupidnewb
    New Member
    • Apr 2009
    • 6

    Array from user input

    I am having some trouble getting a prompt to print out so a user can know what information to type. I have a student class that has a scanner as a constructor.

    Code:
    public Person(Scanner in)
    	{
    		this.firstName = in.nextLine();
    		this.lastName = in.nextLine();
    		this.uid = in.next();
    		this.phone = in.next();
    	}
    Code:
    public Student(Scanner in)
    	{
    		super(in);
    		this.credits = in.nextInt();
    		this.points = in.nextInt();
    	}
    I also have a database class creates a menu for the user to pick the option to add a student to the array. Defining the addStudent method the menu calls is where I am having problems.

    Code:
    public void addStudent()
    	{	
    		if(records[numStudents] == null)
    		{
    			System.out.print("Enter the student's first name: ");
    			Scanner in = new Scanner(System.in);
    			records[numStudents] = new Student(in);
    			numStudents++;
    		}
    		else if(numStudents >= capacity)
    		{
    			System.out.println("The student database is full.");
    		}
    	}
    I can either get all the prompt's to print at once or not at all. If I include them in a loop I won't be able to get them to say different things.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    After printing the prompt, flush() the stream, e.g.

    Code:
    System.out.print("Enter your age: ");
    System.out.flush();
    kind regards,

    Jos

    Comment

    • stupidnewb
      New Member
      • Apr 2009
      • 6

      #3
      It still prints the lines all at once.

      Code:
      public void addStudent()
          {    
              if(records[numStudents] == null)
              {
                  System.out.print("Enter the student's first name: ");
                  System.out.flush();
                  System.out.print("Enter the student's last name: ");
                  System.out.flush();
                  Scanner in = new Scanner(System.in);
                  records[numStudents] = new Student(in);
                  numStudents++;
              }
              else if(numStudents >= capacity)
              {
                  System.out.println("The student database is full.");
              }
          }

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Well you are just printing them one after the other.
        You need to allow the user to type something to the prompt and to read what has been typed using the nextXXX methods.

        Comment

        Working...