0

I just want to print the String "code", which the user enters in the method, but it is printing "null", but when i want to print int num, it prints what the user enters. The output should print all the details that the user entered; sort of like an ID thing. This should be very simple but for some reason I am not able to do it and Im starting to lose hope. Any help would be appreciated. Thanks

public class Lab2 {
    static String code;
    static String num;
    int year;
    static int sem;

    public static void main(String[] args) {
        info();
        num();
        sem();
        System.out.println(num + " " + sem + " " + code);
    }

    public static void info() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Course ID Generator\n--------------------------");
        System.out.println("Please Enter Course Information:");
        String message = "Initial";
        String code = "";

        while (!message.isEmpty()) {
            if (!message.equals("Initial")) {
                System.out.println(message);
            }

            System.out.print("Course Code (only capital letters " + "with a length between 2 and 4): ");
            code = sc.nextLine();

            message = checkLength(code);
            if (message.isEmpty()) {
                message = checkUpperCase(code);
            }
        }

    }

    private static String checkLength(String code) {
        String output = "";
        if (code.length() < 2 || code.length() > 4) {
            output = "Course Code length was not between " + "2 and 4, Please try again";
        }
        return output;
    }

    private static String checkUpperCase(String code) {
        String output = "";
        for (int i = 0; i < code.length(); i++) {
            char ch = code.charAt(i);
            if (Character.isAlphabetic(ch) && Character.isUpperCase(ch)) {
            } else {
                output = "Course Code must only have capital " + "letters, please try again";
                break;
            }
        }
        return output;
    }

    public static void num() {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.print("Course Number (consists of only 3 digits): ");
            num = sc.nextLine();

            if (num.length() == 3) {
                break;
            } else {
                System.out.println("Course Number length was not 3, please try again");
            }
            sc.close();
        }

    }

    public static void sem() {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.print("Semester (Fall=01, Sprint=02, Summer=03):");
            sem = sc.nextInt();

            if (sem > 0 && sem < 4) {
                break;
            } else {
                System.out.println("Please enter correct semester code, try again");
            }
        }

        sc.close();
    }

}
Boss
  • 3
  • 3
  • 1
    Because you have a local variable `code` and a global one of the same name, they hide each other. – luk2302 Feb 14 '21 at 12:19
  • 1
    This `String code = "";` is called variable shadowing. See https://en.wikipedia.org/wiki/Variable_shadowing – njzk2 Feb 14 '21 at 12:20
  • Not directly related to the issue you are having, but you should [never call `Scanner#close()` on a Scanner that uses `System.in`](https://stackoverflow.com/questions/13042008/java-util-nosuchelementexception-scanner-reading-user-input). – Ivar Feb 14 '21 at 12:22

1 Answers1

0

Remove the line

String code = "";

in your info Method

You are using the same variable code twice.

Read this for further information https://dzone.com/articles/variable-shadowing-and-hiding-in-java

JavaMan
  • 1,142
  • 12
  • 22