Home > Software design >  How to display an array on another method with while loop (I am having null)
How to display an array on another method with while loop (I am having null)

Time:10-02

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

CodePudding user response:

It is because in method academicInfo you are declaring and initialising a local variable String[] subj, while the class variable subj remains unchanged. You ought to remove the declaration of subj in the foregoing method and declare and initialise subj as an array of Strings at class level.

public class StudentInfo {   
    static String lname, fname, mname, program;
    static int studentno, unit;
    static String[] subj = new String[8];
...
...
static void academicinfo() {
            Scanner sc = new Scanner(System.in);
            System.out.print("Program: ");
            program = sc.nextLine();
            System.out.println("Courses: ");
            int i = 0;
            while (i < subj.length) {
                subj[i] = sc.nextLine();
                    i  ;
        }
    }
...

CodePudding user response:

Inside the method academicInfo you are creating the variable subj again, but as String[]. I believe you want to use the variable declared in line 4, in that case it would be like this:

static void academicinfo() {
        Scanner sc = new Scanner(System.in);
        System.out.print("Program: ");
        program = sc.nextLine();
        System.out.println("Courses: ");
        subj = sc.nextLine();
}

Now, to align the texts, you must use the System.out.format, passing as a parameter the text and the division number, it would look like this:

String courses_text = "Courses:";
String units_text = "Units:";
System.out.format("%-5d" courses_text,1);
System.out.format("%-5d" units_text,2);
System.out.println();
System.out.format("%-5d" subj, 1);
System.out.format("%-5d" unit, 2);

See more about alignment here

  • Related