简体   繁体   中英

Java // Loop certain scanner input x amount of times and null print

package test2;

public class Student {

private String Username;
private String Game;
private int score;
private int time;
private String Game2;
private int score2;
private int time2;

public Student() {
    this.Username = null;
    this.Game = null;
    this.score = -1;
    this.time = -1;
    this.Game2 = null;
    this.score2 = -1;
    this.time2 = -1;
}

public Student(String nName, String nGame, int nScore, int nTime,      String nGame2, int nScore2, int nTime2) {
    this.Username = nName;
    this.Game = nGame;
    this.score = nScore;
    this.time = nTime;
    this.Game = nGame2;
    this.score = nScore2;
    this.time = nTime2;
}

public void setUsername(String newUsername) {
    this.Username = newUsername;
}

public void setGame(String newGame) {
    this.Game = newGame;
}

public void setScore(int newScore) {
    this.score = newScore;
}

public void setTime(int newTime) {
    this.time = newTime;
}

public void setGame2(String newGame2) {
    this.Game = newGame2;
}

public void setScore2(int newScore2) {
    this.score = newScore2;
}

public void setTime2(int newTime2) {
    this.time = newTime2;
}

public String getUsername() {
    return Username;
}

public String getGame() {
    return Game;
}

public int getScore() {
    return score;
}

public int getTime() {
    return time;
}
public String getGame2() {
    return Game2;
}

public int getScore2() {
    return score2;
}

public int getTime2() {
    return time2;
}
@Override
public String toString() {
    return "Username: " + this.getUsername() + 
           ", Game: " + this.getGame() +
           ", Time: " + this.getScore() +
           ", Achievement Score: " + this.getTime() +
           ", Game: " + this.getGame2() +
           ", Time: " + this.getScore2() +
           ", Achievement Score: " + this.getTime2();

}

}

package test2;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class test {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);



    List<Student> Students = new ArrayList();


    Student student1 = new Student();

    student1.setUsername("Bob Marley");
    student1.setGame("GTA 1");
    student1.setScore(1200);
    student1.setTime(150);
    student1.setGame2("GTA 2");
    student1.setScore2(1200);
    student1.setTime2(150);

    Students.add(student1);

    Student student2 = new Student();

    student2.setUsername("Bill Harvey");
    student2.setGame("Minecraft");
    student2.setScore(12001);
    student2.setTime(15);
    student2.setGame2("SHAFT");
    student2.setScore2(12001);
    student2.setTime2(15);

    Students.add(student2);

    Student student3 = new Student();

    student3.setUsername("Dan Marley");
    student3.setGame("The Division");
    student3.setScore(12000);
    student3.setTime(150);
    student3.setGame2("Minecraft");
    student3.setScore2(12001);
    student3.setTime2(15);

    Students.add(student3);


    System.out.println("Add new students: ");
    System.out.println("Enter number of students to add: ");
    int countStudents = input.nextInt();

    for (int i = 0; i < countStudents; i++) {
        Student newStudents = new Student();


        System.out.println("Enter details for student: " + (i + 1));

        System.out.println("Enter Username: ");
        newStudents.setUsername(input.next());

        System.out.println("Enter Game: ");
        newStudents.setGame(input.next());

        System.out.println("Enter Achievement Score: ");
        newStudents.setScore(input.nextInt());

        System.out.println("Enter Time played (Minutes): ");
        newStudents.setTime(input.nextInt());

        System.out.println("Enter Game 2: ");
        newStudents.setGame2(input.next());

        System.out.println("Enter Achievement Score: ");
        newStudents.setScore2(input.nextInt());

        System.out.println("Enter Time played (Minutes): ");
        newStudents.setTime2(input.nextInt());


        Students.add(newStudents);
    }
    System.out.println(Students); 
}
} 

Here is my coding thus far, problem I am having is the console return "null" from the arraylist, each user should have 2 game of input however the second game is returning as null and I can't see what the problem is.

Add new students: 
Enter number of students to add: 
0
[Username: Bob Marley, Game: GTA 2, Time: 1200, Achievement Score: 150,   Game: null, Time: -1, Achievement Score: -1, Username: Bill Harvey, Game:   SHAFT, Time: 12001, Achievement Score: 15, Game: null, Time: -1,  Achievement Score: -1, Username: Dan Marley, Game: Minecraft, Time: 12001, Achievement Score: 15, Game: null, Time: -1, Achievement Score: -1]

You are just setting the attributes to a single object only student1 again and again. Hence for other objects it is showing null .

I think you need to do

    Student student1 = new Student();

    student1.setUsername("Bob Marley");
    student1.setGame("GTA V");
    student1.setScore(1200);
    student1.setTime(150);

    students.add(student1);

    Student student2 = new Student();

    student2.setUsername("Bill Harvey");
    student2.setGame("Minecraft");
    student2.setScore(12001);
    student2.setTime(15);

    students.add(student2);

    student student3 = new Student();

    student3.setUsername("Dan Marley");
    student3.setGame("The Division");
    student3.setScore(12000);
    student3.setTime(150);

    students.add(student3);

Also, You should use lowercaseStartingCamelCase for variable names. Read here Naming conventions

Refering to your problem with the null values. Try using input.nextLine() instead of input.next()

Well one thing's for sure. If you enter 0 when the console asks how many students you want to add, then your loop in the main will never run. So if you want it to run 3 times, enter 3 as an input. Also, for students 2 and 3, you are just resetting the attributes of student1.

Student student2 = new Student();

    student2.setUsername("Bill Harvey");//error occurred here before fix
    student2.setGame("Minecraft");
    student2.setScore(12001);
    student2.setTime(15);

    Students.add(student2);

    Student student3 = new Student();

    student3.setUsername("Dan Marley");//error occurred here before fix
    student3.setGame("The Division");
    student3.setScore(12000);
    student3.setTime(150);

    Students.add(student3);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM