I have an "Exception in thread" error. I create a public class student and give it three classes: NAME of student, ID of student and GPA of student. When I run the code and enter information for student1 it's good. But when I enter student2, the code skips the name for student2, and gives me the student id and GPA. How can I remedy this?
Here is the code:
import java.util.Scanner;
class Student {
public String name;
public int id;
public float gpa;
}
public class learning {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Student s1 = new Student();
System.out.print("Enter your name: ");
s1.name = input.nextLine();
System.out.print("Enter your id: ");
s1.id = input.nextInt();
System.out.print("Enter your gpa: ");
s1.gpa = input.nextFloat();
Student s2 = new Student();
System.out.print("Enter your name: ");
s2.name = input.nextLine();
System.out.print("Enter your id: ");
s2.id = input.nextInt();
System.out.print("Enter your gpa: ");
s2.gpa = input.nextFloat();
Student s3 = new Student();
System.out.print("Enter your name: ");
s3.name = input.nextLine();
System.out.print("Enter your id: ");
s3.id = input.nextInt();
System.out.print("Enter your gpa: ");
s3.gpa = input.nextFloat();
System.out.println("your name: " + s1.name + "\n"
+ "your id: " + s1.id + "\n"
+ "your GPA: " + s1.gpa);
}
}
Your scanner is getting screwed up if you both use nextLine and next/nextInt/nextDouble refer to this for more Information.
I changed your main method accordingly, it now works like you wanted it to.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Student s1 = new Student();
System.out.print("Enter your name: ");
s1.name = input.nextLine();
System.out.print("Enter your id: ");
s1.id = Integer.parseInt(input.nextLine());
System.out.print("Enter your gpa: ");
s1.gpa = Float.parseFloat(input.nextLine());
Student s2 = new Student();
System.out.print("Enter your name: ");
s2.name = input.nextLine();
System.out.print("Enter your id: ");
s2.id = Integer.parseInt(input.nextLine());
System.out.print("Enter your gpa: ");
s2.gpa = Float.parseFloat(input.nextLine());
Student s3 = new Student();
System.out.print("Enter your name: ");
s3.name = input.nextLine();
System.out.print("Enter your id: ");
s3.id = Integer.parseInt(input.nextLine());
System.out.print("Enter your gpa: ");
s3.gpa = Float.parseFloat(input.nextLine());
System.out.println("your name: " + s1.name + "\n" + "your id: " + s1.id + "\n" + "your GPA: " + s1.gpa);
}
Related
I am just a beginner, so any help would be appreciated.
So here is my code:
import java.util.Scanner;
public class AccountScanner {
public static void main (String[] args) {
String name, course;
int age;
Scanner input = new Scanner(System.in);
System.out.print("Enter name: ");
name = input.nextLine();
System.out.print("Enter age: ");
age = input.nextInt();
System.out.print("Enter Course: ");
course = input.next();
System.out.println();
System.out.println("Name:" +name);
System.out.println("Age:" +age);
System.out.println("Course:" +course);
System.exit(0);
}
}
but what if the user did enter one of the details. What can I do to make it display "Not Available" instead
example:
enter name: Zack
enter age:
enter course: BSCS
output would be:
Name: Zack
Age: Not available
Course: BSCS
You can use a conditional operator to check if the inputs are empty and show the result correspondingly as follows:
String name, course, age;
Scanner input = new Scanner(System.in);
System.out.print("Enter name: ");
name = input.nextLine();
System.out.print("Enter age: ");
age = input.nextLine();
System.out.print("Enter Course: ");
course = input.nextLine();
System.out.println();
System.out.println("Name:" +(name.isEmpty()?"Not Available":name));
System.out.println("Age:" +(age.isEmpty()?"Not Available":new Integer(age)));
System.out.println("Course:" +(course.isEmpty()?"Not Available":course));
System.exit(0);
So for this program I need to have three arrays accept data and display the gross pay, It seems to work at first but after enter the first person's data, my code begins to stack on top of itself in the window, please help me fix this to work properly.
I am required to use arrays. Objects are not allowed.
import java.util.Scanner;
public class ThreeArrays {
public static void main(String args[]) {
float[] payRate = new float[5];
float[] hours = new float[5];
String[] name = new String[5];
getPayData(name, hours, payRate);
displayGrossPay(name, hours, payRate);
}
public static void getPayData(String[] name, float[] hours, float[] payRate) {
Scanner kb = new Scanner(System.in);
for (int i = 0; i < hours.length; i++) {
System.out.print("Enter the employee's name: ");
name[i] = kb.nextLine();
System.out.print("Enter the employee's hours: ");
hours[i] = kb.nextFloat();
System.out.print("Enter the employee's hourly rate: ");
payRate[i] = kb.nextFloat();
}
}
public static void displayGrossPay(String[] name, float[] hours, float[] payRate) {
for (int i = 0; i < hours.length; i++) {
System.out.println("Employee name: " + name[i] + " Gross Pay: " + hours[i] *
payRate[i]);
}
}
}
Put System.out.println(); between each statement where you ask for data.
EDIT: It could also be that the \n character isn't being read with .nextInt(). To fix it, put kb.nextLine(); after kb.nextFloat();
for (int i = 0; i < hours.length; i++) {
System.out.print("Enter the employee's name: ");
name[i] = kb.nextLine();
System.out.println("");
System.out.print("Enter the employee's hours: ");
hours[i] = kb.nextFloat();
kb.nextLine();
System.out.println("");
System.out.print("Enter the employee's hourly rate: ");
payRate[i] = kb.nextFloat();
kb.nextLine();
System.out.println("");
}
// Enter the employee's name: Foo
// (Blank Line)
// Enter the employee's hours: 1
// (Blank Line)
// Enter the employee's hourly rate: -20.00 // They owe me now. :D
// (Blank Line)
The problem is related to stdin flushing. Because last entered float value for line payRate[i] = kb.nextFloat(); don't removes \n character. This \n character is being fetched by line name[i] = kb.nextLine(); in next iteration. Which further leaves a String value for line hours[i] = kb.nextFloat();. Since it is not a valid float hence you are getting exception: java.util.InputMismatchException.
To solve this use line if(i < hours.length-1) kb.next(); after line payRate[i] = kb.nextFloat();.
Following is corrected code. See complete working code here:
public class ThreeArrays {
public static void main(String args[]) {
float[] payRate = new float[5];
float[] hours = new float[5];
String[] name = new String[5];
getPayData(name, hours, payRate);
displayGrossPay(name, hours, payRate);
}
public static void getPayData(String[] name, float[] hours, float[] payRate) {
Scanner kb = new Scanner(System.in);
for (int i = 0; i < hours.length; i++) {
System.out.print("Enter the employee's name: ");
name[i] = kb.nextLine();
System.out.print("Enter the employee's hours: ");
hours[i] = kb.nextFloat();
System.out.print("Enter the employee's hourly rate: ");
payRate[i] = kb.nextFloat();
if(i < hours.length-1) kb.next();
}
}
public static void displayGrossPay(String[] name, float[] hours, float[] payRate) {
for (int i = 0; i < hours.length; i++) {
System.out.println("Employee name: " + name[i] + " Gross Pay: " + (hours[i] *
payRate[i]));
}
}
}
I have figured it out everyone
The problem lies with using
System.out.print("Enter the employee's name: ");
name[i] = kb.nextLine();
I had to change it to
System.out.print("Enter the employee's name: ");
name[i] = kb.next();
I am writing a program to print out the name of a student and their grade. It works if I only enter their first name and I get an Error when I enter their last. I am wondering if I have overlooked something with the String name.
package project_2;
import java.util.Scanner;
public class Project_2 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Enter your name: ");
String name = input.next();
System.out.print("Enter Grade: \t");
double Grade = input.nextDouble();
if(Grade>=88)
System.out.println(name + "You have an A!");
else if (Grade<=87 && Grade>=80)
System.out.println(name + "You have a B!");
else if (Grade<=79 && Grade>=67)
System.out.println(name + "You have a C!");
else if (Grade<=66 && Grade>=60)
System.out.println(name + "You have a D!");
else
System.out.println(name + "You have a F!");
}
}
Just do this:
Scanner input = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = input.nextLine();
System.out.print("Enter Grade: \t");
Then you can take the full name.
Output:
Enter your name: Jack Davidson
Enter Grade: 88
Please note that, Using next() will only return what comes before a space. nextLine() automatically moves the scanner down after returning the current line.
I am trying to write a Java program in which the user specifies how many "student records" they would like to input, followed by the student's name, age, and GPA, which then gets stored as text. However, I am having a problem with my text not including all entered data and a mysterious dangling newline that I cannot get rid of.
Here is my program:
import java.io.*;
import java.util.Scanner;
public class CreateFile {
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
FileWriter fwriter = new FileWriter("c:\\Students.dat");
PrintWriter StudentFile = new PrintWriter(fwriter);
String name = " ";
String next = " ";
int age = 0;
int hm = 0;
double gpa = 0.0;
System.out.print("How many student records would you like to enter: ");
hm = input.nextInt();
for (int x = 1; x <= hm; x++) {
System.out.print("Enter Name: ");
name = input.nextLine();
input.nextLine();
System.out.print("Enter Age: ");
age = input.nextInt();
System.out.print("Enter GPA: ");
gpa = input.nextDouble();
next = input.nextLine();
StudentFile.println(name);
StudentFile.println(age);
StudentFile.println(gpa);
}
StudentFile.close();
System.exit(0);
}
}
Here is sample input and output to illustrate my issues:
run:
How many student records would you like to enter: 3
Enter Name: Jon
Enter Age: 20
Enter GPA: 3.4
Enter Name: Bill
Enter Age: 24
Enter GPA: 3.6
Enter Name: Ted
Enter Age: 34
Enter GPA: 3.9
This is the produced text file:
20
3.4
Bill
24
3.6
Ted
34
3.9
Why doesn't it store the first name entered? Why isn't there a newline in the first entry, but it is in the others?
The problem is that you're using nextLine() when you need to be using next(). I'm assuming you put the second input.nextLine() in there because you were initially having a problem where it would print out "Enter Name: " and then immediately "Enter Age: ". nextLine() is telling your program to skip whatever is there, and not to wait for it. The reason that this paradigm worked at all for any of your entries is that you put next = input.nextLine() at the bottom of your loop. Here's a fix:
package createfile;
import java.io.*;
import java.util.Scanner;
public class CreateFile {
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
FileWriter fwriter = new FileWriter("c:Students.dat");
PrintWriter StudentFile = new PrintWriter(fwriter);
String name = " ";
String next = " ";
int age = 0;
int hm = 0;
double gpa = 0.0;
System.out.print("How many student records would you like to enter: ");
hm = input.nextInt();
for (int x = 1; x <= hm; x++) {
System.out.print("Enter Name: ");
name = input.next();
System.out.print("Enter Age: ");
age = input.nextInt();
System.out.print("Enter GPA: ");
gpa = input.nextDouble();
StudentFile.println(name);
StudentFile.println(age);
StudentFile.println(gpa);
}
StudentFile.close();
System.exit(0);
}
}
You could also just move your input.nextLine() above name=input.nextLine() and it would have the same effect.
The other examples only work if you don't have names like "James Peter" - in their code examples only James would be saved as name.
I'd prefer this:
System.out.print("How many student records would you like to enter: ");
hm = input.nextInt();
input.nextLine();
for (int x = 1; x <= hm; x++) {
System.out.print("Enter Name: ");
name = input.nextLine();
System.out.print("Enter Age: ");
age = input.nextInt();
input.nextLine();
System.out.print("Enter GPA: ");
gpa = input.nextDouble();
input.nextLine();
StudentFile.println(name);
StudentFile.println(age);
StudentFile.println(gpa);
}
This is the corrected for loop:
for ( int x = 1; x <= hm; x++ )
{
System.out.print( "Enter Name: " );
name = input.next();
input.nextLine();
System.out.print( "Enter Age: " );
age = input.nextInt();
input.nextLine();
System.out.print( "Enter GPA: " );
gpa = input.nextDouble();
next = input.nextLine();
StudentFile.println( name );
StudentFile.println( age );
StudentFile.println( gpa );
}
Some things you may want to consider:
Handle the IOException - it should not be ignored!!
Use the methods hasNextXXX() of the Scanner to check if something is available.
Refactor your usage of the variable next, it's never really used.
It's not necessary to call System.exit( 0 ) from the main method - rather use the return statement with a meaningful value.
It's not letting me put my name in but it does the age works fine.
I know i can change the order of the statements but is there another way I could do it?
import java.util.Scanner;
public class ScannerErr2
{
public static void main(String [] args)
{
Scanner keyboard= new Scanner(System.in);
String name;
int age;
System.out.print("Enter your age : ");
age= keyboard.nextInt();
System.out.print("Enter your name: ");
name= keyboard.nextLine();
System.out.println("Age : "+age);
System.out.println("Name: "+name);
}
}
You problem is that the next int doesn't consider the new line character which goes in the input for your name part. Hence name is returned as blank.
You can change your code in 2 ways:
System.out.print("Enter your age : ");
age = keyboard.nextInt();
keyboard.nextLine();
System.out.print("Enter your name: ");
name = keyboard.nextLine();
or
System.out.print("Enter your age : ");
age = Integer.parseInt(keyboard.nextLine().trim());
System.out.print("Enter your name: ");
name = keyboard.nextLine();
I personally like the second way.