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);
Related
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);
}
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.
I have code which is:
import java.util.Scanner;
class shoham
{
public static void main (String args[])
{
System.out.println("Enter your name: ");
Scanner name = new Scanner(System.in);
System.out.println("Your name is: ");
System.out.print(name.nextLine());
}
}
When I execute this command, I get output like:
Enter your name:
Your name is:
***** (user input)
***** (output)
Can anyone give a code that gives output like:
Enter your name: **** - (user input)
Your name is: **** - (output from user input)`
Well you're reading input after your print statements. You probably want:
Scanner in = new Scanner(System.in); // do this first, and give it a
// better name
System.out.print("Enter your name: "); // use print to keep it on one line
String name = in.nextLine(); // read input, store it
System.out.print("Your name is: ");
System.out.println(name); // print name
Enter your name: Joe
Your name is: Joe
You may try something like this ie try using print only instead of println:-
Scanner name = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = name.nextLine();
System.out.print("Your name is: ");
System.out.println(name);
try this
public static void main (String args[])
{
System.out.print("Enter your name: ");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine()
System.out.println("Your name is: "+name);
}
here is your code:
System.out.println("Enter your name: ");
Scanner name = new Scanner(System.in);
System.out.println("Your name is: "+name.nextLine());
You have got lot of sloutions here, but you need to know the issue with your code is,
You did not store the input from the user.
System.out.print("Enter your name: ");
String name = name.nextLine(); // read input, store it