I have to write a program for my class that takes user
input for homework,labs,tests,etc,puts them in a string, and there is a separate class
that splits them in separate "double"numbers and contains a writeToFile method to write
the student's grade into a file.
I keep getting
a problem though.. When I ask the user for their name the first time, it works fine, but
if the user decides to enter the do-while loop again, the "What is your name" is skipped
and it goes straight to id. I know it has something to do with the last input not being
a string, and the solution to that is to put a "keyboard.nextLine();" above the
question, but I tried that and it just asks the user for their name before the question
is even asked..
import
java.util.Scanner;
import java.io.*;
import
java.text.DecimalFormat;
public class GradeApplication
{
public static void main(String[] args) throws IOException
{
Scanner keyboard = new Scanner(System.in);
//Define
variables
String name;
int id;
String
homework;
String labs;
String tests;
double
project;
double discussion;
int answer;
do
{
System.out.print("\nWhat is your name?
");
name=keyboard.nextLine();
System.out.print("\nWhat
is your student ID? ");
id=keyboard.nextInt();
homework
= keyboard.nextLine();
System.out.println("\nPlease enter homework grades
separated by spaces:");
homework =
keyboard.nextLine();
System.out.println("Please enter
lab grades separated by spaces:");
labs =
keyboard.nextLine();
System.out.println("Please enter test grades
separated by spaces:");
tests = keyboard.nextLine();
System.out.println("Please enter project grade:");
project =
keyboard.nextDouble();
System.out.println("Please enter
discussion grade:");
discussion =
keyboard.nextDouble();
System.out.println("\nResults:
");
//Call toString method
Student_Khazma s = new
Student_Khazma(name,id,homework,labs,tests,project,discussion);
System.out.print(s.toString());
//Open file
PrintWriter outputFile= new PrintWriter("gradeReport.txt");
System.out.println(s.writeToFile());
outputFile.close();
System.out.print("\n\nWould you like to see
your grade report again? (1 is yes, 2 is no): ");
answer=keyboard.nextInt();
System.out.print("\n\nWould you like to
see your grade report again? (1 is yes, 2 is no): ");
answer=keyboard.nextInt();
}while(answer==1);
}
}
No comments:
Post a Comment