This question already has answers here:
Scanner doesn't see after space
(7 answers)
Closed 2 years ago.
this is my code, it can validate single word but when i input example: John Smith the output is just John
package space;
import java.util.Scanner;
public class validate{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean cusloop=false;
String cusname="";
while(cusloop==false){
System.out.print("Enter customer name: ");
cusname=sc.next();
if(cusname.matches("[a-zA-Z ]+")){
cusloop=true;
}
else{
System.out.println("Invalid Input!");
System.out.println("Input Letters Only!");
}
}
}
}
just change
cusname = sc.nextLine();
Related
This question already has answers here:
Scanner doesn't see after space
(7 answers)
Closed 1 year ago.
I am writing a very simple program to input a string with space and then output it, my problem is, it not printed out fully as I expected.
Here is my code, as you can see, very simple
import java.util.Scanner;
public class Testjapanese {
public static void main(String[] args) {
String x;
Scanner keyboard = new Scanner(System.in);
System.out.println(" Add a string");
x = keyboard.next();
System.out.println(x);
}
}
For example, it print "Add a string" but when I input a string "Today is very", it gave me "Today", not "Today is very".
I search and they said to me that I should use input.nextLine(), but I do not know how to use it.
May be I must use public java.lang.String nextLine() first ?
Sorry if my question is easy to solve. Thank you for your answer.
import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
String x;
Scanner keyboard = new Scanner(System.in);
System.out.println(" Add a string");
x = keyboard.nextLine();
System.out.println(x);
}
}
You should just replace next with nextLine as below
public static void main(String[] args) {
String x;
Scanner keyboard = new Scanner(System.in);
System.out.println(" Add a string");
x = keyboard.nextLine();
System.out.println(x);
}
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 1 year ago.
why isnt this working
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// write your code here
Scanner scan = new Scanner(System.in);
System.out.println("enter year: ");
int year = scan.nextInt();
int age = 0;
System.out.println("is your bday complete this year: ans 'Y/N'");
String bday= scan.nextLine();
scan.close();
if (bday=="Y"||bday=="y"){
age = 2021-year;
}else if(bday=="N"||bday=="n"){
age = 2020-year;
}else{
System.out.println("wrong input");
}
System.out.println(age);
}
}
You need to use equals() method to compare strings. In this case it is better to use equalsIgnoreCase(). Update the if and else if as below:
if (bday.equalsIgnoreCase("y")){
age = 2021-year;
}else if(bday.equalsIgnoreCase("n")){
age = 2020-year;
}
This question already has answers here:
How to use java.util.Scanner to correctly read user input from System.in and act on it?
(1 answer)
How to use scanner in java? [duplicate]
(7 answers)
Closed 7 years ago.
I have to say I am a newbie in java programming so I would like some help with the following question:
How can I delay a java program so that it waits for a user to give an input?
Here is my code:
import java.util.Scanner;
public class test {
public static void main(String[] args) {
System.out.println("1st value= ");
Scanner aa1 = new Scanner(System.in);
System.out.println("2nd value= ");
Scanner bb1 = new Scanner(System.in);
What I get now from this code is "1st value= " and "2nd value= " displayed. After that the program requests the input.
Any ideas on how can I make this simple programm to wait for the user's answer before printing "2nd value= " in the console?
Ask for input from the user:
int index = aa1.nextInt();
You don't need to create two Scanner variables. You can use one of them and use the function input.nextInt(); to get both values, where input it's the name of the Scanner variable.
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number1, number2;
System.out.println("1st value= ");
number1 = input.nextInt();
System.out.println("2nd value= ");
number2 = input.nextInt();
}
}
You must first import the Scanner before your Main class.
import java.util.Scanner;
You must then create a Scanner variable. I use "in" in my example:
Scanner in = new Scanner(System.in);
This will force the program to wait for user input.
` import java.util.Scanner;
public class Example{
public static void main( String[] args )
{
String name;
String address;
String cityStateZip;
Scanner in = new Scanner(System.in);
//User input
System.out.println("What is your name?");
name = in.nextLine();
System.out.println("Enter your address:");
address = in.nextLine();
System.out.println("Enter your City, State, and Zip code:");
cityStateZip = in.nextLine();`
You have to use only one Scanner variable. This is the solution :
import java.util.Scanner;
import java.util.*;
public class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
System.out.println("1st value= " + str);
str = sc.nextLine();
System.out.println("2nd value= " + str);
}
}
You can see the different next***() on the doc on this link http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html.
I advise you to follow a Java formation if you want to code in this language.
You can use it as:
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("1st value= "
+ input.nextInt()
+ "2nd value= "
+ input.nextInt());
}
For further reference to can go through Scanner java doc http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
Add necessary imports by pressing control+shift+o after you have typed the main function.
public static void main(String agrs[])
{
// Take Scanner object
Scanner scanner =new Scanner(System.in);
// Ask user for 1st input
System.out.println("Enter 1st value:");
String value=scanner.nextLine();
System.out.println("1st value is ="+s);
// Ask user for 2nd input
System.out.println("Enter 2nd value:");
value=scanner.nextLine();
System.out.println("2nd value is ="+s);
}
NOTE : scanner.nextLine() will read the user input as a String,
If you need the input as int , use scanner.nextInt(); For float use scanner.nextFloat() and so on.
Now the console is halted for user input and then asks for second input.
Hope it helps.
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.
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();