This question already has answers here:
Scanner issue when using nextLine after nextXXX [duplicate]
Using scanner.nextLine() [duplicate]
(5 answers)
Closed 8 years ago.
import java.util.Scanner;
public class GpaConverterTester
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
GpaConverter g = new GpaConverter();
System.out.println("How many classes are you taking? ");
int classAmount = sc.nextInt();
while(classAmount > 0)
{
System.out.println("Enter Grade: ");
String grade = sc.nextLine();
g.setGpaValue(grade);
classAmount--;
}
System.out.println("Average: " + g.getAverage());
}
}
My basic problem is that it wont let me enter in the grade string. This is what happens...
Output:
"How many classes are you taking?
2
Enter Grade:
Enter Grade:
"
It does not let me enter in the grade string.
Thank you for helping!
I figured it out.
Use sc.next() instead of line.
Related
This question already has answers here:
Validating input using java.util.Scanner [duplicate]
(6 answers)
Closed 6 years ago.
So I am a beginner using java, and I am trying to have the user enter a temperature in Fahrenheit and convert it into Celsius, all while making sure the use enters a valid number for the double variable instead of letters. and than displaying in back to the user. I am particularly having trouble with the loop. Here is my code.
import java.util.Scanner;
public class TempConverter
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
double temp;
do
{
System.out.print("Please enter temperature in Fahrenheit: ");
while(!in.hasNextDouble())
{
System.out.println("ERROR. Please enter a valid temperature: ");
in.nextDouble();
}
temp = in.nextDouble();
}
double finalTemp = ((temp - 32)*5)/9;
System.out.print("Celsius value of Fahrenheit value " + temp + " is " + finalTemp + ".");
}
}
The while must come after. It will keep iterating until the condition in the while has been met.
do
{
System.out.print("Please enter temperature in Fahrenheit: ");
System.out.println("ERROR. Please enter a valid temperature: ");
in.nextDouble();
temp = in.nextDouble();
}
while(!in.hasNextDouble())
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 7 years ago.
I don't know why, but the below code makes the user run the code again, whether they choose to or not. I've tried many things, but it doesn't work correctly.
Thanks!
public static void main (String [ ] args)
{
boolean a = true;
while (a)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter an integer: ");
int x = scan.nextInt();
System.out.print("\n\nEnter a second integer: ");
int z = scan.nextInt();
System.out.println();
System.out.println();
binaryConvert1(x, z);
System.out.println("\n\nWould you like to run this code again? Enter \"Y\" or \"N\".");
System.out.print("Enter your response here: ");
String RUN = scan.nextLine();
String run = RUN.toLowerCase();
if (run.equals("n"))
{
a = false;
}
System.out.println();
System.out.println();
}
System.out.println("Goodbye.");
}
Scanner.nextInt() doesn't consume the line ending characters from the buffer, which is why when you read the value of the "yes/no" question with scan.nextLine(), you'll receive an empty string instead of the value the user entered.
A simple way to fix this is to explicitly parse the integer from raw lines using Integer.parseInt():
System.out.print("Enter an integer: ");
int x = Integer.parseInt(scan.nextLine());
System.out.print("\n\nEnter a second integer: ");
int z = Integer.parseInt(scan.nextLine());
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
import java.util.Scanner;
public class Methods {
/*
* Compound interest Program Quarterly
*/
public static void main(String[] args) {
Scanner keyboard = new Scanner (System.in);
System.out.println("What is the Rate");
int rate = keyboard.nextInt()/100;
System.out.println("What is the Amount");
int amount = keyboard.nextInt();
System.out.println("How many years?");
int years = keyboard.nextInt();
keyboard.nextLine();
System.out.println("How is it compounded? Press Q = Quarterly, A = Anually, S = SemiAnnualy, M = Monthly");
String answer = keyboard.nextLine();
int easy = amount*(1+(rate/years));
int pow = 4 * years;
if (answer == "/Q"){
System.out.println("Your answer compounded Quarterly is: " + Math.pow(easy,pow));
This is the code, but the if statement with String == "Q" doesn't work because when I press Q nothing happens? what's the issue?
Java right? use equalsIgnoreCase because you could be typing 'q' and that / they told you about too, keep it in mind
Maybe you can try
if(answer.equals("Q"))
First issue is that you have a typo (the forward slash) in this line:
if (answer == "/Q"){
The second issue is that you don't compare strings like this.
Instead use the "equals()" method.
if(answer.equals("Q")){
See here for more information: How do I compare strings in Java?
This question already has answers here:
Where is the keyboard specified the System.in method?
(2 answers)
Closed 7 years ago.
public class Adder
{
public static void main(String arr[])
{
//System.in represents Standard Input Device (Keyboard)
//Explain this next line, please:
Scanner in = new Scanner(System.in);
System.out.println("Enter First No.");
int a = in.nextInt();
System.out.println("Enter Second No.");
int b = in.nextInt();
int c = a+b;
System.out.println("Sum is: "+c);
}
}
I just started learning JAVA and i came across this code.Can someone explain me what does the marked line signify ?
Scanner s=new Scanner(System.in);
The System.in in the above code tells the compiler to get the data typed in the Keyboard, Which is a input device
Thanks,
For more basic things in java Please checkout:
Tutorialspoint.com
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Why can't I enter a string in Scanner(System.in), when calling nextLine()-method?
(13 answers)
Closed 9 years ago.
I used Java and I tried to write program that reads a number of patients and then let the employee enter their names and ages.
and I want the program reads full name such as (Maha Saeed) so I wrote the code like this but I don't know why it does not work
name = scan.nextLine();
and this is the full code
import java.util.*;
public class Answer1
{
static Scanner scan = new Scanner (System.in);
public static void main (String[] args)
{
int age ;
int PatientNumber ;
int PatientNO;
String name ;
System.out.print("Enter number of patients :");
PatientNumber = scan.nextInt();
PatientNO = 1;
while ( PatientNO <= PatientNumber)
{
System.out.println("Patient #" +PatientNO);
System.out.print("Enter patient's Name: ");
name = scan.nextLine(); //<- here is the problem if I write scan.next it works but it reads only the first name
System.out.print("Enter patient's Age: ");
age= scan.nextInt();
PatientNO = PatientNO + 1;
}
}
}
thanks all
See Why can't I enter a string in Scanner(System.in), when calling nextLine()-method? and Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods
Simple solution, you can consume the \n character:
scan.nextLine();
name = scan.nextLine();