473,395 Members | 1,454 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1. public Person(Scanner in)
  2.     {
  3.         this.firstName = in.nextLine();
  4.         this.lastName = in.nextLine();
  5.         this.uid = in.next();
  6.         this.phone = in.next();
  7.     }
Expand|Select|Wrap|Line Numbers
  1. public Student(Scanner in)
  2.     {
  3.         super(in);
  4.         this.credits = in.nextInt();
  5.         this.points = in.nextInt();
  6.     }
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.

Expand|Select|Wrap|Line Numbers
  1. public void addStudent()
  2.     {    
  3.         if(records[numStudents] == null)
  4.         {
  5.             System.out.print("Enter the student's first name: ");
  6.             Scanner in = new Scanner(System.in);
  7.             records[numStudents] = new Student(in);
  8.             numStudents++;
  9.         }
  10.         else if(numStudents >= capacity)
  11.         {
  12.             System.out.println("The student database is full.");
  13.         }
  14.     }
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.
May 1 '09 #1
3 5573
JosAH
11,448 Expert 8TB
After printing the prompt, flush() the stream, e.g.

Expand|Select|Wrap|Line Numbers
  1. System.out.print("Enter your age: ");
  2. System.out.flush();
  3.  
kind regards,

Jos
May 2 '09 #2
It still prints the lines all at once.

Expand|Select|Wrap|Line Numbers
  1. public void addStudent()
  2.     {    
  3.         if(records[numStudents] == null)
  4.         {
  5.             System.out.print("Enter the student's first name: ");
  6.             System.out.flush();
  7.             System.out.print("Enter the student's last name: ");
  8.             System.out.flush();
  9.             Scanner in = new Scanner(System.in);
  10.             records[numStudents] = new Student(in);
  11.             numStudents++;
  12.         }
  13.         else if(numStudents >= capacity)
  14.         {
  15.             System.out.println("The student database is full.");
  16.         }
  17.     }
  18.  
May 8 '09 #3
r035198x
13,262 8TB
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.
May 8 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: deko | last post by:
Is there a way to check user input for illegal characters? For example, a user enters something into a text box and clicks OK. At that point I'd like to run code such as this: illegal =...
0
by: Paulers | last post by:
hello VB masters I have an issue that I am hoping you can help me with. I am creating an application that accepts user input via a richtextbox. the user can enter multiple entries and I need to...
3
by: dei3cmix | last post by:
Hey, I am having a problem with a program I am working on. Basically, the first part of the program gets input from a file using cin.getline. Then the second part, (still in the same main as the...
6
by: AZRebelCowgirl73 | last post by:
Here is my problem: I have two java files: One named Car.java and the other named CarDealerApp.java: In the CarDealerApp program, I read in through user input the make, model, year and price of...
14
by: n3o | last post by:
Hello Comp.Lang.C Members, I have an issue with user input that I have been trying to figure out for the longest. For instance, let's say you have something like this: void foo() { int num;...
4
by: mohaaron | last post by:
I can think of a lot of reasons why this might need to be done but as far as I can tell it's not possible. I've been looking for a way to add HtmlTableRows to a table using a button click for a...
5
by: Dbarten1982 | last post by:
I am brand new to C++ programming, and am completely stuck on a program. In the program I am supposed to create a string array that stores 5 user input words, and the string constant "END_OF_ARRAY'...
8
Misaio
by: Misaio | last post by:
I'm trying to create a program that prompts the user to put in several different numbers. I'd like to put those numbers into an array for easy use. To save me time on coding, I want to loop the...
3
by: rickytb | last post by:
Hey all, I've hit a snag on a beginner Java problem involving building occupancy. I'm supposed to get user input from a GUI. My main problem is that I'm totally lost on how to take the information...
5
by: Bernardo | last post by:
Okay, so I am making a program that accepts user input of integers. As you can see I am using an array and i need to get the greatest number, and smallest, range, mean, etc. But I successfully did...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.