I have a Student class which has
private String name;
private long idNumber;
and getters and setters for them.
I also have a StudentTest class which has three different methods, 1. to ask user for the size of the array and then to create an array of type Student, 2. to ask user to populate the array with names and ID numbers for as long as the array is, 3. to show the contents of the array.
The code I have so far is;
import java.util.Scanner;
public class StudentTest {
// Main method.
public static void main(String [] args) {
}
// Method that asks user for size of array.
public static Student[] createArray() {
System.out.println("Enter size of array:");
Scanner userInputEntry = new Scanner(System.in);
int inputLength = userInputEntry.nextInt();
Student students[] = new Student[inputLength];
return students;
}
// Method that asks user to populate array.
public static void populateArray(Student [] array) {
}
// Method that displays contents of array.
public static void displayArray(Student[] array) {
}
}
I'm not sure as to how to tackle the second method of asking the user to populate the array, any help will be greatly appreciated :)
This may help you
public static Student[] createArray() {
System.out.println("Enter size of array:");
Scanner userInputEntry = new Scanner(System.in);
int inputLength =userInputEntry.nextInt();
Student students[] = new Student[inputLength];
return students;
}
public static void populateArray(Student [] array) {
for(int i=0;i<array.length().i++)
{
array[i]=new Student();
System.out.println("Enter Name");
Scanner userInputEntry = new Scanner(System.in);
array[i].setName(userInputEntry .next());
System.out.println("Enter Id");
array[i].setIdNumber(userInputEntry .nextLong());
}
}
The easiest solution is probably a loop, in which you ask the user for input and then store it in your array.
Something like:
for(int i = 0; i < students.length(); i++){
[Ask for input and store it]
}
i suppose something like this:
// Method that asks user to populate array.
public static void populateArray(Student [] array) {
for(int i = 0; i < array.lenght;i++) {
array[i]=new Student(name, id); //put here student name/id
}
}
Related
I want to have an ArrayList that records user entries and ends when it receives a non-integer value but I am getting an infinite loop and I don't know why.
public class Tryout1
{
public static void main(String[] args)
{
ArrayList entries = new ArrayList();
Scanner obj1 = new Scanner(System.in);
System.out.println("enter numbers");
int i = obj1.nextInt();
boolean accumulating = obj1.hasNextInt(); //check int
while(accumulating) {
entries.add(i);
}
System.out.println(entries);
}
}
you should move your check inside the loop, so it will check every time before the next loop is executed.
In your code, the check is only performed once.
public class tryout1 {
public static void main(String[] args) {
ArrayList entries = new ArrayList();
Scanner obj1 = new Scanner(System.in);
System.out.println("enter numbers");
do {
// get the next int
int i = obj1.nextInt();
entries.add(i);
} while (obj1.hasNextInt()); // <- check here for nextInt
System.out.println(entries);
}
}
I am taking a variable from user and wants the array of same size as that of variable. So if I pass that variable in it as its size it shows me an error so is there any wayout for it.
import java.util.Scanner;
public class passingtheparcel
{
public static void main(String[] args)
{
Scanner obj = new Scanner(System.in);
String firstline;
String song;
int n;
System.out.println("enter the no. of students");
firstline = obj.nextLine();
n = Integer.parseInt(firstline);
System.out.println("enter the lyrics of song");
song = obj.nextLine();
int[n] a;
}
}
The syntax for initializing an array of length n is :
int[] a = new int[n];
System.out.println("enter the no. of students");
int[] a = new int[obj.nextInt()];
import java.util.Scanner;
public class Demo{
public static void main(String a[]){
Scanner obj=new Scanner(System.in);
String firstline;
int n;
System.out.println("Enter the no. of students");
firstline=obj.nextLine();
n=Integer.parseInt(firstline);
int arr[]=new int[n];
}
}
Please try this this is working sample.
I am trying to assign the current array element in the temp array with the Student object returned after calling the getStudent method.... I called the getStudent method (Step 2) and have temp[i] = to assign the current element in the temp array but cannot figure out what it should = to pair it up with the Student object returned. When using getStudent() and running the program, the output is enter the number of students, the user enters the number, and that is all that happens, it does not ask for the user to enter the name and etc, I'm not sure if step 2 is the problem or if there is another issue entirely.
import java.util.Scanner;
public class Students
{
private static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
Student[] students;
students = getStudents();
printStudents(students);
}
private static Student[] getStudents()
{
Student[] temp;
int how_many;
System.out.print("How many students? ");
how_many = input.nextInt();
purgeInputBuffer();
temp = new Student[input.nextInt()]; // Step 1 ???
for (int i = 0; i < temp.length; i++)
{
getStudent(); // Step 2
temp[i] = ; // <----------
}
return temp; // Step 3
}
private static Student getStudent()
{
String name,
address,
major;
double gpa;
System.out.print("Enter name: ");
name = input.nextLine();
System.out.print("Enter address: ");
address = input.nextLine();
System.out.print("Enter major: ");
major = input.nextLine();
System.out.print("Enter GPA: ");
gpa = input.nextDouble();
purgeInputBuffer();
return new Student (name, address, major, gpa); // Step 4
}
private static void printStudents(Student[] s)
{
System.out.println();
for (int i = 0; i < s.length; i++) // Step 5
{
System.out.println(getStudent()); // Step 6
}
}
private static void purgeInputBuffer()
{
// ----------------------------------------------------
// Purge input buffer by reading and ignoring remaining
// characters in input buffer including the newline
// ----------------------------------------------------
input.nextLine();
}
}
So first problem is first on the line:
temp = new Student[input.nextInt()];
in that line you have already asked the user to enter how many Students and store it in how_many. So i'm assuming you want to instead do:
temp = new Student[how_many];
Also what i said in my comment:
But please do also look at your private static void printStudents(Student[] s) method and acutally on the line //step 6 i don't believe that is how you want to be doing that. Instead you want System.out.println(s[i]); not System.out.println(getStudent()); For my code substitution to work though you will need to Override the toString method so it can actually display the information
I am confused on how to instantiate the array using input from the user, the spot where i believe it should be is next to the comment right here
import java.util.Scanner;
public class Sales
{
public static void main(String[] args)
{
int[] sales;
sales = getSales();
printSales(sales);
printSummary(sales);
}
private static int[] getSales()
{
Scanner input = new Scanner(System.in);
int[] temp;
System.out.print("Enter the number of salespeople: ");
temp = _____________; // RIGHT HERE
You should write something like:
temp = new int[input.nextInt()];
I have a Student class which has;
private String name;
private long idNumber;
and getters and setters for them.
I also have a StudentTest class which has three different methods, 1. to ask user for the size of the array and then to create an array of type Student, 2. to ask user to populate the array with names and ID numbers for as long as the array is, 3. to show the contents of the array.
The code I have so far is;
import java.util.Scanner;
public class StudentTest {
// Main method.
public static void main(String [] args) {
}
// Method that asks user for size of array.
public static Student[] createArray() {
System.out.println("Enter size of array:");
Scanner userInputEntry = new Scanner(System.in);
int inputLength = userInputEntry.nextInt();
Student students[] = new Student[inputLength];
return students;
}
// Method that asks user to populate array.
public static void populateArray(Student [] array) {
for(int i=0;i<array.length().i++) {
array[i] = new Student();
System.out.println("Enter student name: ");
Scanner userInputEntry = new Scanner(System.in);
array[i].setName(userInputEntry.next());
System.out.println("Enter student ID number: ");
array[i].setIDNumber(userInputEntry .nextLong());
}
}
// Method that displays contents of array.
public static void displayArray(Student[] array) {
}
}
How do I print the contents of the array back out to the user?
in class Student u can override toString():
#Override
public void toString(){
return "name: " + this.name;
}
and create a simple for loop
for(int i = 0; i < array.length; i++){
System.out.println(array[i].toString());
}
hope this helps a little
Override the toString method in Student class and then use https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#toString(java.lang.Object[])
use this in you display function if you don't want to override the toString method
for (int i = 0; i < array.length; i++) {
System.out.println("Student id : "+array[i].getIdNumber() + " Student name : " +array[i].getName());
}