-6

The requirement is to display the user input student details in table format.

For example:
Enter the number of students
2
Enter the student1 details
28
Science
Is the student from same country[Y/N]
N
Enter the country
Australia
Enter the Student2 details
29
Commerce
Is the Student from same country[Y/N]
Y
The student details are
Age         Subject     Country        
28          Science    Australia      
29          Commerce     UK

**If the student are from same country by default the value would be printed as UK under country column. I am stuck at the point where the value needs to be displayed in tabular format under headers(Age,name,country)along with the default value(UK in this case). I am very new to java and not able to proceed furthur. Your any help would of great benefit to me. Thanks in advance.

My Code is:

public class StudentTable{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        //Port obj = new Port();
        int a,i;
        String b = null;
        System.out.println("Enter the number of students");
        a = sc.nextInt();
        int[] age = new int[a+1];
        String[] name = new String[a+1];
        for(i=1;i<a+1;i++){
            System.out.println("Enter the students "+i+ " details");
            age[i] = sc.nextInt();
            sc.nextLine();
            name[i] = sc.nextLine();
            System.out.println("Is the student from same country[Y/N]");
            b = sc.nextLine();

                if(b=="N"){
                System.out.println("Enter the country");
                String country = sc.next();
                return;
                }
        }
                if(b=="Y");
                String country = "India";
                    System.out.println("The student details are");
                    System.out.format("%-15s%-15s%-15s","Age","name","country");
luciferxxx
  • 59
  • 1
  • 8
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. Use the [edit] link to improve your *question* - do not add more information via comments. Thanks! – GhostCat Dec 19 '17 at 15:23
  • 2
    We are not here to first read your assignments, to then digest all of your code to see where things deviate. **You** tell us precisely what your problem is. – GhostCat Dec 19 '17 at 15:23
  • Hi, The problem is at the point where the data needs to be displayed in tabular format – luciferxxx Dec 19 '17 at 15:25
  • Hi, i will make sure that the problem raised is more precise and to the point – luciferxxx Dec 19 '17 at 15:28
  • `b == "N"` and `b == "Y"` will fail. See https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java. – VGR Dec 19 '17 at 16:18

1 Answers1

0

1.Read about difference between "==" OR "equals",sysout.printf and clean code.

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



    //Port obj = new Port();
    int count, i;
    String b ;

    System.out.println("Enter the number of students");
    count= sc.nextInt();

    int[] age = new int[count];
    String[]  name = new String[count];
    String[] country=new String[count];
    for (i = 0; i < count; i++) {
        System.out.println("Enter the students " + i+1 + " details");
        System.out.println("Your age?");

        age[i] = sc.nextInt();

        sc.nextLine();
        System.out.println("Your name?");

        name[i] = sc.nextLine();
        System.out.println("Is the student from same country[Y/N]");
        b = sc.nextLine();

        // if(b=="N")
        if (b.equals("N")) {
            System.out.println("Enter the country");
             country[i] = sc.next();

        }
        //if(b=="Y")
        if (b.equals("Y")) {
             country[i] = "India";
        }
    }
   String frmt= String.format("%-15s%-15s%-15s","Age","name","country");
    System.out.println("The student details are");
    System.out.println(frmt);
    for( i=0;i<age.length;i++){

        System.out.printf("%d  %15s  %14s",age[i],name[i],country[i]);
        System.out.println();
    }
}

}