I am having a problem displaying the user input of subjects (subj) and their units (unit). It says null, which part do you think I have to fix/add? And if possible, can I add column on this so that subjects and units will align together? What do I have to do/import? This is my whole code (I am a beginner):
package com.mycompany.studentinfo;
import java.util.Scanner;
public class StudentInfo {
static String lname, fname, mname, program, subj;
static int studentno, unit;
public static void main(String[] args){
student();
academicinfo();
academicunit();
finaloutput();
}
static void student() {
try {
System.out.println("***PROVIDE THE FOLLOWING***");
Scanner sc = new Scanner(System.in);
System.out.print("Student No: ");
studentno = sc.nextInt();
String xd = sc.nextLine(); // extra
System.out.print("Last Name: ");
lname = sc.nextLine();
System.out.print("First Name: ");
fname = sc.nextLine();
System.out.print("Middle Name: ");
mname = sc.nextLine();
}
catch (Exception x){
System.out.println("***ERROR***");
student();
}
}
static void academicinfo() {
Scanner sc = new Scanner(System.in);
String[] subj = new String[8];
System.out.print("Program: ");
program = sc.nextLine();
System.out.println("Courses: ");
int i = 0;
while (i < subj.length) {
subj[i] = sc.nextLine();
i++;
}
}
static void academicunit() {
Scanner sc = new Scanner(System.in);
int[] unit = new int[8];
System.out.println("Units in corresponding order");
int i = 0;
while (i < unit.length) {
unit[i] = sc.nextInt();
i++;
}
}
static void finaloutput() {
System.out.println("PERSONAL INFORMATION");
System.out.println("\tStudent No. " + studentno);
System.out.println("\tLast Name: " + lname);
System.out.println("\tFirst Name: " + fname);
System.out.println("\tMiddle Name: " + mname);
System.out.println("\nACADEMIC INFORMATION");
System.out.println("Program: " + program);
System.out.println("Courses: ");
System.out.println(subj);
System.out.println(unit);
}
}
With this code, the final output will be:
PERSONAL INFORMATION
Student No. 123
Last Name: asd
First Name: asd
Middle Name: asd
ACADEMIC INFORMATION
Program: asd
Courses:
null
0
and the outcome I am trying to do is:
PERSONAL INFORMATION
Student No. 123
Last Name: asd
First Name: asd
Middle Name: asd
ACADEMIC INFORMATION
Program: asd
Courses: Units:
this is course 0