Java issue with String returning Null value, despite using Scanner class to input data

I am writing a program for a class that is very simple. All it requires of me is to print a grade sheet, and be able to input 3 pieces of information (here represented by Strings studentName, idNumber, and assignmentTitle). The grade sheet has to be stored in a single string. Ok so this to me appears as though it should work but for whatever reason it outputs null values where the inputted studentName, idNumber, and assignmentTitle should appear. Very simple problem I'm sure, any ideas? Here is my code.

import java.util.Scanner;

class GradingForm
{
static String studentName;
static String idNumber;
static String assignmentTitle;
    static String gradeSheet = "********************************* \n\n" + 
                                assignmentTitle + "\n\n" +
                                studentName + " " + idNumber + "\n\n" +
                                "Grade Summary:\n\n" +
                                "Program Correctness:        Quality of Style:\n" +
                                "Late Deduction:              Overall Score:\n" +
                                "Comments:";
public static void gradeFormValues()
{
    Scanner inData;
    inData = new Scanner(System.in);
    System.out.println("Enter student's name: ");
    studentName = inData.nextLine();
    System.out.println("Enter student ID number: ");
    idNumber = inData.nextLine();
    System.out.println("Enter Assignment title: ");
    assignmentTitle = inData.nextLine();
}
public static void printGradeSheet()
{
    System.out.println(gradeSheet);
}

public static void main(String[] args)
{
            gradeFormValues();
            printGradeSheet();
}
}

Answers


Your problem is that you use those variables before assigning values to them:

static String gradeSheet = "********************************* \n\n" + 
                                assignmentTitle + "\n\n" +
                                studentName + " " + idNumber + "\n\n" +
                                "Grade Summary:\n\n" +
                                "Program Correctness:        Quality of Style:\n" +
                                "Late Deduction:              Overall Score:\n" +
                                "Comments:";

You need to build the string only after you assign values to them for example:

public static void printGradeSheet()
{
    gradeSheet = "********************************* \n\n" + 
       assignmentTitle + "\n\n" +
       studentName + " " + idNumber + "\n\n" +
       "Grade Summary:\n\n" +
       "Program Correctness:        Quality of Style:\n" +
       "Late Deduction:              Overall Score:\n" +
       "Comments:";
    System.out.println(gradeSheet);
}

Need Your Help

jQuery effects combination

javascript jquery highlight

I want to apply highlight and remove effects to the same dom. One after the other. Unfortunately the highlight effect is not visible as the remove gets triggered immediately after.

How to get the display of 9x9 grid in the android?

android user-interface grid cell

Now it's time to explore Android for me. I have designed one ui which is shown below, The thing is I am getting confused with what should I do in the first frame in order to get 9x9 grid. Actually ...