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();
}
}
Your problem is that you use those variables before assigning values to them:
You need to build the string only after you assign values to them for example: