0

Hi guy I'm a beginner of android studio. now I try to create for loop code but it errror and said

Process 'command 'C:/Program Files/Android/Android Studio/jre/bin/java.exe'' finished with non-zero exit value 1

and this's my code

public class FindOldestStudent {
    //static Scanner console = new Scanner(System.in);
    public static void main ( String [ ] args ) {
        String name, tel, school;
        int age;
        Student[ ] student = new Student[2];

        Student oldestStudent;
        for (int i = 0; i < 2 ; i++) {
            student[i] = new Student( );
            int a = 0;
            student[a] = new Student();
            System.out.print(i+1 + ".Enter student name: ");
            name = args[a];
            student[a].setName(name);
            System.out.print(name);
            System.out.print(" Enter student tel: ");
            tel = args[a];
            student[a].setTel(tel);
            System.out.print(" Enter student school: ");
            school = args[a];
            student[a].setSchool(school);
            System.out.print(" Enter student age: ");
            age = Integer.parseInt(args[i]);
            student[a].setAge(age);
        }
        oldestStudent = maxAge(student);
        System.out.print(oldestStudent.getName( ) + " is the oldest student, " +
                oldestStudent.getAge( ) + " years old.");
    }
    private static Student maxAge( Student[ ] student ) {
        Student maxStudent = new Student();
        for (int i = 0; i < 2 ; i++ ) {
            if (student[i].getAge( ) > maxStudent.getAge( ))
                maxStudent = student[i];
        }
        return(maxStudent);
    }
}

and

class Student{
    private String name, tel, school;
    private int age;
    public Student(String name, String tel, String school, int age) {
        this.name = name;
        this.tel = tel;
        this.school = school;
        this.age = age;
    }
    public Student( ) {
        this.name = "";
        this.tel = "";
        this.school = "";
        this.age = 0;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setTel(String tel) {
        this.tel = tel;
    }
    public void setSchool(String school) {
        this.school = school;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName( ) {
        return (name);
    }
    public String getTel( ) {
        return (tel);
    }
    public String getSchool( ) {
        return (school);
    }
    public int getAge( ) {
        return(age);
    }
}

this's my code maybe it have to takes 2 agrs, but I'm not sure how to write it. so pls guide me.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
K Ns
  • 3
  • 2
  • JavaScript is very different from Java, please don't add unrelated tags –  Feb 24 '22 at 10:41
  • Also this looks like purely `java` question, nothing related to about `android` or `android-studio` (tags removed). – Pshemo Feb 24 '22 at 10:42
  • Anyway what are you trying to achieve with `int a = 0; student[a] = new Student();` inside your loop? In each iteration `a` will always be `0` so you are effectively always ending up with `student[0]` when you use `student[a]`. Could you use [edit] option and clarify your question more (like explain what is this code *supposed* to do)? – Pshemo Feb 24 '22 at 10:46
  • Primarily remove the `a` int and use `student[i]` instead. No idea why you switched to `student[a]` when you were successfully creating `student[i]` right beforehand. –  Feb 24 '22 at 10:50

2 Answers2

2

You are confusing the args param of the main method with reading data from the console.

The String[] args of the main method are the args that the java process gets when its running. For instance "java -jar test.jar a b c" will recieve ["a", "b", "c"] as the args params.

I think your intention are read from the console. In that case, I suggest you to create a new method to do that. Here is an example of how I would do that:

public static void main(String[] args) throws IOException {
    String name, tel, school;
    int age;
    Student[] student = new Student[2];

    Student oldestStudent;
    for (int i = 0; i < 2; i++) {
        student[i] = new Student();
        System.out.print(i + 1 + ".Enter student name: ");
        name = readValue();
        student[i].setName(name);
        System.out.print(name);
        System.out.print(" Enter student tel: ");
        tel = readValue();
        student[i].setTel(tel);
        System.out.print(" Enter student school: ");
        school = readValue();
        student[i].setSchool(school);
        System.out.print(" Enter student age: ");
        age = Integer.parseInt(readValue());
        student[i].setAge(age);
    }
    oldestStudent = maxAge(student);
    System.out.print(oldestStudent.getName() + " is the oldest student, " +
            oldestStudent.getAge() + " years old.");
}

private static String readValue() throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    return reader.readLine();
}

private static Student maxAge(Student[] student) {
    Student maxStudent = new Student();
    for (int i = 0; i < 2; i++) {
        if (student[i].getAge() > maxStudent.getAge())
            maxStudent = student[i];
    }
    return (maxStudent);
}

With correct I/O:

1.Enter student name: a
a Enter student tel: 33
 Enter student school: a
 Enter student age: 12
2.Enter student name: b
b Enter student tel: 22
 Enter student school: s
 Enter student age: 10
a is the oldest student, 12 years old.
Soteck
  • 51
  • 1
  • 6
1

To get input from console. you need to use scanner object rather than using args array, to be clear args is used to pass command line argument while running the code, those arguments will be stored in args array, so you'll need to do

System.out.print(i+1 + ".Enter student name: ");
name = console.nextLine(); // next line for string value
System.out.print(" Enter student age: ");
age = console.nextInt(); // next int for integer value

as console is the name of your scanner object

PS- one thing to consider here, while doing nextInt it reads next integer, not complete line so if you're reading a number and then a line below, you'll have to do one nextLine after nextInt it reads line after the integer which is blank, but it reads an empty string.

More info: Scanner is skipping nextLine() after using next() or nextFoo()?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Achal Urankar
  • 159
  • 1
  • 11
  • yeah, i didnt find that link, thanks. @Pshemo – Achal Urankar Feb 24 '22 at 10:58
  • 1
    You can bookmark that link somewhere as this is one of most common duplicate targets for Java questions along with [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/q/218384) and [How do I compare strings in Java?](https://stackoverflow.com/q/513832) :) – Pshemo Feb 24 '22 at 11:02