public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Please choose.('e' to encrypt, 'd' to decrypt, 'q' to quit): ");
String userIn= in.next();
if(userIn.equals("e")){
System.out.println("Please enter your text that you want to encrypt: ");
String userInput = in.nextLine();
System.out.print("Please enter your shift key(0-25): ");
int userS = in.nextInt();
if(userS < 0 || userS > 25){
System.out.print("Invalid shift key, please enter a valid shift key: ");
userS = in.nextInt();
}
In my above program at following part of code:
System.out.println("Please enter your text that you want to encrypt: ");
String userInput = in.nextLine();
System.out.print("Please enter your shift key(0-25): ");
It is skipping this userInput, it goes over it and asks for the shift key before I enter the text.
This fixed it (tested in Eclipse):
Scanner in = new Scanner(System.in);
System.out.print("Please choose.('e' to encrypt, 'd' to decrypt, 'q' to quit): ");
String userInput = in.nextLine();
if (userInput.equals("e"))
{
System.out.println("Please enter your text that you want to encrypt: ");
userInput = in.nextLine();
System.out.print("Please enter your shift key(0-25): ");
int userS = Integer.parseInt(in.nextLine());
if (userS < 0 || userS > 25)
{
System.out.print("Invalid shift key, please enter a valid shift key: ");
userS = Integer.parseInt(in.nextLine());
}
}
in.close();
I changed your userIn variable to just be userInput, since we didn't need it; your next() call was also changed to nextLine().
I also changed all your nextInt()'s to nextLine()'s. This will help you avoid an Exception later on.
Lastly, always close a Scanner when you are done with it to conserve system resources.
Change:
String userInput = in.nextLine()
to
in.nextLine(); String userInput = in.nextLine();
Simple, change you code to
String userInput = in.next();
Related
I have a method that must get an integer as input. How can I check that it is a integer certainly. if input is a string so get number again.
public int getMobileNumber() {
System.out.println("Enter Mobile Number: ");
int mobileNumber = input.nextLong();
System.out.println("Mobile Number Registered!");
return mobileNumber;
}
You can use the hasNextInt method and a while loop to get definitely an integer:
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number:");
while (!sc.hasNextInt()) {
System.out.printf("Your input was \"%s\". Please enter a number:%n", sc.next());
}
int num = sc.nextInt();
System.out.println(num);
sc.close();
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String sent = "y";
int a = 0;
int b = 0;
System.out.println("Welcome!\nThis program compares two letters in a sentence.");
while(!sent.equalsIgnoreCase("X")) {
System.out.print("Enter a sentence or X to exit: ");
sent = input.next();
System.out.print("Enter the numeric location of the first letter: ");
while(!input.hasNextInt()) {
System.out.println("Error! Please enter a number not text!");
input.nextLine();
System.out.print("Enter the numeric location of the first letter: ");
}
a = input.nextInt();
System.out.print("Enter the numeric location of the second letter: ");
while(!input.hasNextInt()) {
System.out.println("Error! Please enter a number not text!");
input.nextLine();
System.out.print("Enter the numeric location of the second letter: ");
}
b = input.nextInt();
System.out.println();
char c = sent.charAt(a);
char d = sent.charAt(b);
if(c==d) {
System.out.println(c+" and "+d+" are identical.\n");
}
else {
System.out.println(c+ " and "+ d + " are unique characters. \n");
}
}
System.out.print("GoodBye!");
}
This loop continues to run even if you input X and I don't know why. Also, when you input a text instead of a number, the error message will like so:
Error! Please enter a number not text!
Enter the numeric location of the first letter: Error! Please enter a number not text!
Enter the numeric location of the first letter:
Only the third sentence will take in user input.
This code sent = input.next()
is not reading a whole sentence. It just reads one word.
Try nextLine() instead.
Also, you should test the exit condition immediately after sent is set.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String sent = "y";
int a = 0;
int b = 0;
System.out.println("Welcome!\nThis program compares two letters in a sentence.");
System.out.print("Enter a sentence or X to exit: ");
sent = input.next();
while(!sent.equalsIgnoreCase("X")) {
//moved accepting input above while condition.This will fix
//X not recognized issue
System.out.print("Enter the numeric location of the first letter: ");
while(!input.hasNextInt()) {
System.out.println("Error! Please enter a number not text!");
input.nextLine();
System.out.print("Enter the numeric location of the first letter: ");
}
a = input.nextInt();
System.out.print("Enter the numeric location of the second letter: ");
while(!input.hasNextInt()) {
System.out.println("Error! Please enter a number not text!");
input.nextLine();
System.out.print("Enter the numeric location of the second letter: ");
}
b = input.nextInt();
System.out.println();
char c = sent.charAt(a);
char d = sent.charAt(b);
if(c==d) {
System.out.println(c+" and "+d+" are identical.\n");
}
else {
System.out.println(c+ " and "+ d + " are unique characters. \n");
}
}
System.out.print("GoodBye!");
}
Im trying to ask the user for two numbers. I want to check if those inputs are in fact numbers but the code I have so far does not let me enter a second value if the first input is a string.
So the scanner does not read anything the else statement.
How could I make it work?
import java.util.Scanner;
public class calculations {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.print("Please enter your first name: ");
String fname = console.nextLine();
System.out.print("Please enter your last name: ");
String lname = console.nextLine();
System.out.print("Please enter your first number: ");
if (console.hasNextInt()) {
int number1 = console.nextInt();
System.out.print("Please enter your second number: ");
if (console.hasNextInt()) {
int number2 = console.nextInt();
}
} else
System.out.print("Please enter your second number: ");
if (console.hasNextInt()) {
int number2 = console.nextInt();
// this part does not work
}
}
}
You just need to add console.nextLine(); after your else statement, because the Scanner.hasNextInt method does not move cursor past your previous input (if it is a string).
I just want the scanner to read new line as empty string then continue to next process if the user press enter. So valid input must be y,n,enter. Any idea how to do this?
This is my code:
String gender = "", employed = "";
Scanner in = new Scanner(System.in);
System.out.print("Gender M/F, press enter to skip... ");
while(!in.hasNext("[mfMF]$")){
System.out.print("Invalid, please choose m/f only... ");
in.nextLine();
}
if(in.hasNextLine()){
gender = in.nextLine();
}
System.out.print("Employed? y/n, press enter to skip... ");
while(!in.hasNext("[ynYN]$|")){
System.out.print("Invalid, please choose y/n only... ");
in.nextLine();
}
if(in.hasNextLine()){
employed = in.nextLine();
}
System.out.println(gender + " : " + employed);
Try This :
import java.util.*;
class Scanner1
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s;
do
{
s=sc.nextLine();
if(!(s.equalsIgnoreCase("y")||s.equalsIgnoreCase("n")||s.equalsIgnoreCase("")))
{
System.out.println("Please Enter valid input");
}
}while(!(s.equalsIgnoreCase("y")||s.equalsIgnoreCase("n")||s.equalsIgnoreCase("")));
}
}
So, to check if the user has pressed enter, you would have to make use of the isEmpty() method. The way to do that is shown below:
String enter = in.nextLine();
if (enter.isEmpty()) {
// do what is needed
}
i want to end my java program if i input q or Q.
otherwise, the program will continue running, printing the question and asking for input.
will this work?
Scanner sc = new Scanner(System.in);
while(sc.hasNext())
{
System.out.print("Enter departure city <'Q' or 'q' to exit>: ");
String input = sc.next();
if(!(input.equals('q')) || !(input.equals('Q'))){
//system continues
}
else{
System.exit(0);
}
}
String input = "";
while (!input.toUpperCase().equals("Q"))
{
System.out.print("Enter departure city. <'Q' or 'q' to exit>: ");
input = sc.next();
}
Exit after the while loop.