Home > Software design >  How can I run 2 args
How can I run 2 args

Time:02-24

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.

CodePudding user response:

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.

CodePudding user response:

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()?

  •  Tags:  
  • java
  • Related