I have no idea how to put this...
basically, We are supposed to make an Array. where we input the number of students, their names, and age. after that an output will be displayed. It will show the names and age of a student.
It seems easy for you to look at. but I really need advice on how to shorten the output. like if there is anything I could do to minimize the code and make an infinite number of outputs so that I can input any number of students.
public static void main(String[] args)
{
int ageInput;
Scanner input = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
System.out.print("Number of Students > ");
int studentValue = input.nextInt();
String[] arraylist = new String[studentValue];
int[] ageinput = new int[studentValue];
char count;
for (count = 0; count < studentValue; count++)
{
System.out.println("Please input a student name and age:");
System.out.println("------------------------------------");
System.out.println("Name: ");
arraylist[count] = input.next();
System.out.println("Age: ");
ageinput[count] = input2.nextInt();
System.out.println(" ");
}
for (String a: arraylist)
{
System.out.println("Student #1");
System.out.println("name: " +arraylist[0]);
System.out.println("age: " +ageinput[0]);
System.out.println("Student #2");
System.out.println("name: " +arraylist[1]);
System.out.println("age: " +ageinput[1]);
System.out.println("Student #3");
System.out.println("name: " +arraylist[2]);
System.out.println("age: " +ageinput[2]);
System.out.println("Student #4");
System.out.println("name: " +arraylist[3]);
System.out.println("age: " +ageinput[3]);
System.out.println("Student #5");
System.out.println("name: " +arraylist[4]);
System.out.println("age: " +ageinput[4]);
}
}
}
I'm not very good at this stuff. soo if you could answer the question I really appreciate it :) Thank you all!
In order to use a loop to print your results i suggest you are using the following code:
for(int i = 0; i <studentValue; i++)
{
System.out.println("Student #" + i + 1);
System.out.println("name: " + arraylist[i]);
System.out.println("age: " + ageinput[i]);
}
Related
When I run the program it shows me on the first values on the list which is the one with the [0] here is my array code:
KeyStroke[] clientsDetails = new KeyStroke[5];
clientsDetails[0] = new KeyStroke(9,"OX5BJM","Peter",2039489);
clientsDetails[1] = new KeyStroke(12,"OX1BOL","Kim",2434587);
clientsDetails[2] = new KeyStroke(67,"OX2VBN","Patrick",2233842);
clientsDetails[3] = new KeyStroke(34,"OX2XHB","Liam",2432340);
clientsDetails[4] = new KeyStroke(54,"OX3BUN","Bob",2234098);
And here is the code to support the array:
if(input.matches("S")){
System.out.println("Enter an array postion from 1 to 4 to show paitient's details");
number1 = enterNumber0.nextInt();
System.out.println("Name: " +clientsDetails[0].nameLable);
System.out.println("Age: " +clientsDetails[0].howOld);
System.out.println("Postcode: " +clientsDetails[0].postcode);
System.out.println("Phone Number: " + clientsDetails[0].cellPhoneNumber);
number2 = enterNumber1.nextInt();
System.out.println("Name: " +clientsDetails[1].nameLable);
System.out.println("Age: " +clientsDetails[1].howOld);
System.out.println("Postcode: " +clientsDetails[1].postcode);
System.out.println("Phone Number: " + clientsDetails[1].cellPhoneNumber);
number3 = enterNumber2.nextInt();
System.out.println("Name: " +clientsDetails[2].nameLable);
System.out.println("Age: " +clientsDetails[2].howOld);
System.out.println("Postcode: " +clientsDetails[2].postcode);
System.out.println("Phone Number: " + clientsDetails[2].cellPhoneNumber);
number4 = enterNumber1.nextInt();
System.out.println("Name: " +clientsDetails[3].nameLable);
System.out.println("Age: " +clientsDetails[3].howOld);
System.out.println("Postcode: " +clientsDetails[3].postcode);
System.out.println("Phone Number: " + clientsDetails[3].cellPhoneNumber);
}
Help will be greatly appreciated this is the last bit of my work that I'm stuck on. I have no teacher to advice this for me as to why I added a question on this forum I hope nobody gets offended.
Assuming enterNumber0 is a Scanner that is already created, we can do something like this:
// Populate the array
KeyStroke[] clientsDetails = new KeyStroke[]{
new KeyStroke(9,"OX5BJM","Peter",2039489),
new KeyStroke(12,"OX1BOL","Kim",2434587),
new KeyStroke(67,"OX2VBN","Patrick",2233842),
new KeyStroke(34,"OX2XHB","Liam",2432340),
new KeyStroke(54,"OX3BUN","Bob",2234098)
}
Not 100% sure what input does here, so I've kept this part. Also note that we fetch the ID from the user non-zero-indexed, so we have to subtract 1 to all the values. You also seem to have misunderstood how to use this value to return the correct client details. You use this value as the index in the array.
if (input.matches("S")) {
System.out.println("Enter an array postion from 1 to 5 to show paitient's details");
int number = enterNumber0.nextInt();
if (number >= 1 && number <= 5) {
System.out.println("Name: " +clientsDetails[number - 1].nameLable);
System.out.println("Age: " +clientsDetails[number - 1].howOld);
System.out.println("Postcode: " +clientsDetails[number - 1].postcode);
System.out.println("Phone Number: " + clientsDetails[number - 1].cellPhoneNumber);
}
}
You can use matches which can accept an integer from 1 to 5:
String number = enterNumber0.nextLine();
if(number.matches("[1-5]")){
int index = Integer.parseInt(input) - 1;
//use this index to get the value from your array
System.out.println("Name: " +clientsDetails[index ].nameLable);
System.out.println("Age: " +clientsDetails[index ].howOld);
System.out.println("Postcode: " +clientsDetails[index ].postcode);
System.out.println("Phone Number: " + clientsDetails[index ].cellPhoneNumber);
}
Note -1, because the array start from 0.
The code works for entering wife's name and age, but fails to print the names of sons, though it displays their ages properly.
import java.util.Scanner;
public class Check2
{
public static void main(String[] args)
{
String wife;
String son1;
String son2;
int wifeAge;
int son1Age;
int son2Age;
Scanner keyboard = new Scanner(System.in);
System.out.println("Wife's name? ");
wife = keyboard.nextLine();
System.out.println("Her age? ");
wifeAge = keyboard.nextInt();
System.out.println();
System.out.println("First son's name? ");
son1 = keyboard.nextLine();
keyboard.nextLine();
System.out.println("His age? ");
son1Age = keyboard.nextInt();
System.out.println();
System.out.println("Second son's name? ");
son2 = keyboard.nextLine();
keyboard.nextLine();
System.out.println("His age? ");
son2Age = keyboard.nextInt();
System.out.println();
keyboard.nextLine();
System.out.println("My wife's name is " + wife + ". She is " +
wifeAge + " years old.\nOur first son is " +
son1 + ". He is " + son1Age + ".\nOur " +
"second son is " + son2 + ". He is " +
son2Age + ".");
}
}
You have the extra keyboard.nextLine(); in the wrong place in your code.
son1 = keyboard.nextLine();
keyboard.nextLine(); // you don't need this here.
son2 = keyboard.nextLine();
keyboard.nextLine(); // nor here
Keep the extra keyboard.nextLine(); line after each keyboard.nextInt(); and your program should run fine.
wifeAge = keyboard.nextInt();
keyboard.nextLine(); // put it here
...
son1Age = keyboard.nextInt();
keyboard.nextLine(); // and here
The nextInt() reads only an integer value and not a new line. If you need to read a new line, you will need to put the keyboard.nextLine(); every time you read integer values from the keyboard.
Hope this helps!
import java.util.Scanner;
public class Check2
{
public static void main(String[] args)
{
String wife;
String son1;
String son2;
int wifeAge;
int son1Age;
int son2Age;
Scanner keyboard = new Scanner(System.in);
System.out.println("Wife's name? ");
wife = keyboard.nextLine();
System.out.println("Her age? ");
wifeAge = keyboard.nextInt();
System.out.println();
System.out.println("First son's name? ");
keyboard.nextLine();
son1 = keyboard.nextLine();
System.out.println("His age? ");
son1Age = keyboard.nextInt();
System.out.println();
System.out.println("Second son's name? ");
keyboard.nextLine();
son2 = keyboard.nextLine();
System.out.println("His age? ");
son2Age = keyboard.nextInt();
System.out.println();
keyboard.nextLine();
System.out.println("My wife's name is " + wife + ". She is " +
wifeAge + " years old.\nOur first son is " +
son1 + ". He is " + son1Age + ".\nOur " +
"second son is " + son2 + ". He is " +
son2Age + ".");
}
}
nextLine() isn't the method you want.
You want to use next() instead, as shown in the following corrected and simplified code. You don't really want to have to manage the newlines, just let the scanner treat all the input as a string of tokens separated by whitespace, and read them in sequence. Use the next() method for inputting strings and nextInt() for inputting integers...
import java.util.Scanner;
public class Check2
{
public static void main(String[] args)
{
String wife;
String son1;
String son2;
int wifeAge;
int son1Age;
int son2Age;
Scanner keyboard = new Scanner(System.in);
System.out.println("Wife's name? ");
wife = keyboard.next();
System.out.println("Her age? ");
wifeAge = keyboard.nextInt();
System.out.println();
System.out.println("First son's name? ");
son1 = keyboard.next();
System.out.println("His age? ");
son1Age = keyboard.nextInt();
System.out.println();
System.out.println("Second son's name? ");
son2 = keyboard.next();
System.out.println("His age? ");
son2Age = keyboard.nextInt();
System.out.println();
System.out.println("My wife's name is " + wife + ". She is " +
wifeAge + " years old.\nOur first son is " +
son1 + ". He is " + son1Age + ".\nOur " +
"second son is " + son2 + ". He is " +
son2Age + ".");
}
}
This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 6 years ago.
I keep getting an error when trying to delete or display the users that I've created. Pointing to the line of code where it actually does the deletion or displaying of a certain user. I can't seem to figure out where the error is coming from.
import java.util.*;
public class contactInfo {
public static void main(String[] args) {
String tracker[][] = {
{" ", " ", " ", " ",},
{" ", " ", " ", " ",},
{" ", " ", " ", " ",},
{" ", " ", " ", " ",},
{" ", " ", " ", " ",},
{" ", " ", " ", " ",},
{" ", " ", " ", " ",},
{" ", " ", " ", " ",},
{" ", " ", " ", " ",},
{" ", " ", " ", " ",}
};
Scanner input = new Scanner(System.in);
int picker = 0;
while (true) {
System.out.print("Please choose an option: \n 1. Add a user \n 2. Delete a user \n 3. Display a user \n 4. Quit ");
picker = input.nextInt();
if (picker == 1) {
addUser(tracker);
} else if (picker == 2) {
deleteUser(tracker);
} else if (picker == 3) {
displayUser(tracker);
} else {
break;
}
}
}
public static String[][] addUser(String[][] add) {
Scanner input = new Scanner(System.in);
System.out.print("Which user is this information for (1 - 10): ");
int user = input.nextInt();
System.out.println(" ");
System.out.print("Enter the users first name: ");
add[user][0] = input.next();
System.out.println(" ");
System.out.print("Enter the users last name: ");
add[user][1] = input.next();
System.out.println(" ");
System.out.print("Enter the users phone number (without dashes): ");
add[user][2] = input.next();
System.out.println(" ");
System.out.print("Enter the users age: ");
add[user][3] = input.next();
System.out.println(" ");
return add;
}
public static String[][] deleteUser(String[][] del) {
Scanner input = new Scanner(System.in);
System.out.print("Which user would you like to delete?: ");
int user = input.nextInt();
for (int i = 0; i < del.length - 1; i++) {
del[user][i] = " ";
}
return del;
}
public static String[][] displayUser(String[][] displ) {
Scanner input = new Scanner(System.in);
System.out.print("Which user would you like to display?: ");
int user = input.nextInt();
for (int i = 0; i < displ.length; i++) {
System.out.print(displ[user][i] + " ");
}
return displ;
}
}
Your tracker is a 2-dimensional array. 10 rows of 4 strings each. The problem is this code:
for (int i = 0; i < del.length - 1; i++)
{
del[user][i] = " ";
}
You iterate over the array of 4 strings, but the variable i will go from 0 to 8. What you want is something like:
for (int i = 0; i < del[user].length; i++)
{
del[user][i] = " ";
}
Instead of using displ.length or del.length for you limits, use displ[user].length or del[user].length.
Apologies for the silly question, I am currently struggling to learn java. I need this code to work so that it will repeat unless '0' is entered for the studentNumber, I'm unsure of how to get the "please enter student number" part to work when I have to declare the int for that before the if statement? I'm not sure if I've approached this completely wrong or what, but I need to be able to repeat the data entry unless "0" is entered as the studentNumber. Thanks for any help!
class Main {
public static void main( String args[] ) {
int studentNumber = BIO.getInt();
if(studentNumber > 0) {
System.out.print("#Please enter the student number : ");
System.out.print("#Please enter the coursework mark : ");
int courseWork = BIO.getInt();
System.out.print("#Please enter the exam mark : ");
int examMark = BIO.getInt();
double average = (double)(courseWork + examMark) / 2;
System.out.printf("sn = " + studentNumber
+ " ex = " + examMark + " cw = " + courseWork
+ " mark = " + average);
} else {
System.out.print("#End of data");
}
}
}
}
Use while()
while(studentNumber > 0){
studentNumber = BIO.getInt();
.........
........
}
See also
while in Java
Use while() instead of if, along with the following changes:
System.out.print("#Please enter the student number : ");
int studentNumber = BIO.getInt();
while(studentNumber > 0) {
System.out.print("#Please enter the coursework mark : ");
int courseWork = BIO.getInt();
System.out.print("#Please enter the exam mark : ");
int examMark = BIO.getInt();
double average = (double)(courseWork + examMark) / 2;
System.out.printf("sn = " + studentNumber
+ " ex = " + examMark + " cw = " + courseWork
+ " mark = " + average);
System.out.print("#Please enter the student number : ");
studentNumber = BIO.getInt();
}
System.out.print("#End of data");
This, as opposed to the other answers, will ensure that even in the first iteration, you perform the check (and promt the user for the student number).
Using Scanner to get the input from the user and process the input value
import java.util.Scanner;
public class ConditionCheck {
public static void main(String[] args) {
Scanner BIO = new Scanner(System.in);
System.out.print("#Please enter the student number : ");
int studentNumber = BIO.nextInt();
if(studentNumber > 0) {
System.out.print("#Please enter the coursework mark : ");
int courseWork = BIO.nextInt();
System.out.print("#Please enter the exam mark : ");
int examMark = BIO.nextInt();
double average = (double)(courseWork + examMark) / 2;
System.out.printf("sn = " + studentNumber
+ " ex = " + examMark + " cw = " + courseWork
+ " mark = " + average);
} else {
System.out.print("#End of data");
}
}
}
You should be using a while statement and do something as below:
class Main
{
public static void main( String args[] )
{
int studentNumber = 1;
While(studentNumber > 0)
{
studentNumber = BIO.getInt();
System.out.print("#Please enter the student number : ");
System.out.print("#Please enter the coursework mark : ");
int courseWork = BIO.getInt();
System.out.print("#Please enter the exam mark : ");
int examMark = BIO.getInt();
double average = (double)(courseWork + examMark) / 2;
System.out.printf("sn = " + studentNumber + " ex = " + examMark + " cw = " + courseWork + " mark = " + average);
}
else
{
System.out.print("#End of data");
}
}
}
This is just for the sake of formality/neatness. Backlash t (\t), which is all I know, doesn't seem to work very well. Here's my code:
import java.util.Scanner;
public class RegisterStudent {
public static void register() {
System.out.println("----------------------------------------");
System.out.println("Student Class Card Evaluation System");
System.out.println("----------------------------------------");
System.out.println("Personal Data");
Scanner input = new Scanner(System.in);
System.out.print("\tEnter student no.: ");
long studNo = input.nextLong();
System.out.print("\tEnter first name: ");
String fName = input.next();
System.out.print("\tEnter last name: ");
String lName = input.next();
System.out.print("\tEnter course: ");
String course = input.next();
System.out.println();
System.out.println("----------------------------------------");
System.out.println("Subject: Computer Programming 2");
System.out.println("----------------------------------------");
System.out.println();
System.out.print("\tPrelim Grade: ");
double pg = input.nextDouble();
System.out.print("\tMidterm Grade: ");
double mg = input.nextDouble();
System.out.print("\tPrefinal Grade: ");
double pfg = input.nextDouble();
System.out.print("\tFinal Grade: ");
double fg = input.nextDouble();
System.out.println();
System.out.println("----------------------------------------");
double gwa = (pg + mg + pfg + fg)/4;
System.out.println();
System.out.println("Student Evaluated successfully...");
System.out.println("Viewing Report...");
System.out.println();
System.out.println("--------------------------------------------------------------------------------------------");
System.out.println("ID No.\t\t" + "LASTNAME\t\t" + "FIRSTNAME\t\t" + "COURSE\t\t"
+ "PG\t\t" + "MG\t\t" + "PFG\t\t" + "FG\t\t" + "GWA");
System.out.println("--------------------------------------------------------------------------------------------");
System.out.println(studNo + "\t\t" + lName + "\t\t" + fName + "\t\t" + course + "\t\t"
+ pg + "\t\t" + mg + "\t\t" + pfg + "\t\t" + fg + "\t\t" + gwa);
The output should look something like this:
Everything that the user will input should be properly aligned. Please help me fix my program, thanks guys!
My current output is this:
You see it's not properly aligned
Since this is homework, here are a couple of hints:
The printf method on a PrintWriter or PrintStream classes can do alignment. So can String.format and the Formatter class.
Don't use TAB characters (i.e. '\t') for alignment in simple text output if you want the alignment to be portable. Different OSes have different ideas on the "width" of a TAB.
The number of tabs you need on each line of input will be determined by the length of the prompt text which appears first on the line i.e. the shorter the prompt, the more tabs required. Simple trial and error should sort out your output to give you the required result.