import java.io.*;
import java.util.*;
public class Main{
public static void main(String [] args) throws InputMismatchException{
double width;
int period;
double Ppp;
Scanner in0 = new Scanner(System.in);
Scanner in1 = new Scanner(System.in);
Scanner in2 = new Scanner(System.in);
System.out.println("Give width\n");
while(in0.hasNextDouble()){
width = in0.nextDouble();
}
in0.close();
System.out.println("\n");
System.out.println("Give period");
while(in1.hasNextInt()){
period = in1.nextInt();
}
in1.close();
System.out.println("\n");
System.out.println("Insert width peak to peak");
while(in2.hasNextDouble()){
Ppp = in2.nextDouble();
}
in2.close();
}
I run this code block
I insert the first input but it displays null for each input
and then it crash
May someone run it and tell if he has the same problem
I use BlueJ compiler
The cause of the problem is this
Scanner in0 = new Scanner(System.in);
Scanner in1 = new Scanner(System.in);
Scanner in2 = new Scanner(System.in);
and this
in0.close();
...
in1.close();
...
in2.close();
When you create the Scanner, you work on System.in, then you close it. This cause that next Scanner operate on closed stream.
The solution is to create a single Scanner for InputStream.
Scanner scanner = new Scanner(System.in);
System.out.println("Give width\n");
double width = scanner.nextDouble();
System.out.println("Give period");
int period = scanner.nextInt();
System.out.println("\nInsert width peak to peak:");
double p2p = scanner.nextDouble();
This is only example that do not validate the user input.
public static void main(String [] args) throws InputMismatchException{
double width;
int period;
double Ppp;
Scanner in0 = new Scanner(System.in);
System.out.println("Give width\n");
// This will read the line, and parse the result as a double, this way you can insert a number and press enter
width = Double.parseDouble(in0.nextLine());
System.out.println("Give period");
period = Integer.parseInt(in0.nextLine());
System.out.println("\n");
System.out.println("Insert width peak to peak:");
ppp = Double.parseDouble(in0.nextLine());
in0.close();
}
Related
could you tell me is there any solution to keep Scanner alive after ctrl+d (eof)?
I have this
static public int getInt(String errorText){
Scanner scan = new Scanner(System.in);
while (!scan.hasNextInt()){
scan.next();
System.out.println(errorText);
}
return scan.nextInt();
}
And after ctrl+d i get NoSuchElementException
Then i try to section try-catch
static public int getInt(String errorText){
Scanner scan = new Scanner(System.in);
while (!scan.hasNextInt() ){
try{
scan.next();
}catch(NoSuchElementException e){
System.out.println("PrechwyciĆem!");
}
System.out.println(errorText);
}
return scan.nextInt();
}
and after that i get infinity loop
I have written a basic code to read different data types. But I can not enter the string as input. What am I missing ?
public class Main {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int integer = read.nextInt();
double Double = read.nextDouble();
String string = read.nextLine();
System.out.printf("String: %s\nDouble: %f\nInt: %d",string,Double,integer);
}
}
You must "eat up" the newline character left over from the double.
Scanner read = new Scanner(System.in);
int integer = read.nextInt();
double Double = read.nextDouble();
read.nextLine();
String string = read.nextLine();
System.out.printf("String: %s\nDouble: %f\nInt: %d",string,Double,integer);
The problem is that after nextDouble() there is still a newline character out there so the scanner reads the next line which has nothing in it...
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I've been trying to make a basic calculator, but the thing is every time I use if else, it either says I have to remove it due to syntax error or the green underline thing shows up and it says dead code. Could someone help me?
import java.util.Scanner;
public class additionCalculator {
public static void main(String[] args) {
int addresult;
int subtractresult;
int divideresult;
int multiplyresult;
String addition = null;
String subtraction = null;
String division = null;
String multiplication = null;
Scanner reader = new Scanner(System.in);
System.out.println("Do you want to do subtraction, division, multiplication, or addition?");
String i = reader.next();
if(i == addition) {
Scanner additioncalc = new Scanner (System.in);
System.out.println("Enter a number");
int add1 = reader.nextInt();
Scanner additioncalc2 = new Scanner (System.in);
System.out.println("Enter another number");
int add2 = reader.nextInt();
addresult = add1 + add2;
System.out.println("The sum is " +addresult);
} else if (i == multiplication) {
Scanner multiplicationcalc = new Scanner (System.in);
System.out.println("Enter a number");
int multiply1 = reader.nextInt();
Scanner multiplcationcalc2 = new Scanner (System.in);
System.out.println("Enter another number");
int multiply2 = reader.nextInt();
multiplyresult = multiply1 * multiply2;
System.out.println("The product is " +multiplyresult);
} else if (i == division) {
Scanner divisioncalc = new Scanner (System.in);
System.out.println("Enter a number");
int div1 = reader.nextInt();
Scanner divisioncalc2 = new Scanner (System.in);
System.out.println("Enter another number");
int div2 = reader.nextInt();
divideresult = div1 * div2;
System.out.println("The product is " +divideresult);
} else if (i == subtraction) {
Scanner subtractioncalc = new Scanner (System.in);
System.out.println("Enter a number");
int subtract1 = reader.nextInt();
Scanner subtractioncalc2 = new Scanner (System.in);
System.out.println("Enter another number");
int subtract2 = reader.nextInt();
subtractresult = subtract1 - subtract2;
System.out.println("The difference is " +subtractresult);
}
}
// Addition calculator
// Scanner reader = new Scanner(System.in);
// System.out.println("Please enter a number.");
// int i = reader.nextInt();
// Scanner reader2 = new Scanner(System.in);
// System.out.println("Please enter another number.");
// int j = reader2.nextInt();
// sum = i + j;
// System.out.println("The sum of the two numbers is " +sum);
}
You should be using string comparison function from java.lang.String.
Note:
== tests for reference equality.
.equals() tests for value equality.
I'm writing a menu-driven Java program to implement a Vigenere Cipher.
For the menu function, I wrote the following code:
public static int printmenu(){
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to the Vigenere Cipher!");
System.out.println("What would you like to do?");
System.out.println("1.Encrypt a message");
System.out.println("2.Decrypt a message");
System.out.println("Enter your choice:");
return scan.nextInt();
}
Now this does the job correctly, but I was criticized by my professor for not closing my scanner class object "scan".
So, I made the following edit to my code:
public static int printmenu(){
int a;
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to the Vigenere Cipher!");
System.out.println("What would you like to do?");
System.out.println("1.Encrypt a message");
System.out.println("2.Decrypt a message");
System.out.println("Enter your choice:");
a = scan.nextInt();
scan.close();
return a;
}
This, however, returns the following error:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at vigenere.Ciphermain.main(Ciphermain.java:30)
I don't understand what I'm doing wrong. Can anyone help me?
Edit: Here is the source code for the Ciphermain class:
public class Ciphermain extends JFrame{
public static int printmenu(){
int a;
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to the Vigenere Cipher!");
System.out.println("What would you like to do?");
System.out.println("1.Encrypt a message");
System.out.println("2.Decrypt a message");
System.out.println("Enter your choice:");
a = scan.nextInt();
scan.close();
return a;
}
public static void main(String[] args) {
String message, key;
int choice;
choice = printmenu();
Scanner scan = new Scanner(System.in);
if(choice == 1){
System.out.println("Enter the message to be encrypted:");
message = scan.nextLine();
System.out.println("Enter the secret key:");
key = scan.next();
Cipher ciph = new Cipher(key);
System.out.println("Encrypted message is: " + ciph.encrypt(message));
}
else{
System.out.println("Enter the message to be decrypted:");
message = scan.nextLine();
System.out.println("Enter the secret key:");
key = scan.next();
Cipher ciph = new Cipher(key);
System.out.println("Decrypted message is: " + ciph.decrypt(message));
}
scan.close();
}
}
Closing scanner will close underlying stream (System.in). So next time you create scanner on System.in which is closed, you will get such exception.
Create scanner at start of program, use it everywhere and close it at end of program.
Like:
static Scanner scan;
static int displayMenu(){
// display menu.
return scan.nextInt();
}
public static void main(String [] args){
scan = new Scanner(System.in);
// start work.
// end of work.
scan.close();
}
Check this: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#close()
I want to know how I could take keyboard input and save it as a variable so I can use it with the code below.
Code:
public void readMaze(){
Scanner reader = null;
try {
reader = new Scanner(new FileReader("Maze.txt"));
colSize = reader.nextInt();
rowSize = reader.nextInt();
finishRow = reader.nextInt();
finishCol = reader.nextInt();
startRow = reader.nextInt();
startCol = reader.nextInt();
Instead of having "Maze.txt" I want to have a variable there that can change every time I run the program so I won't have to keep editing the program when I want to use a different file.
You can capture the file name using your Scanner itself:
System.out.println("Please input the file name to use: ");
Scanner reader = new Scanner(System.in);
String fileName = reader.next();
Then proceed with your method as usual, reusing the same Scanner variable for a new Scanner object, this time passing filename you captured earlier:
try {
reader = new Scanner(new FileReader(fileName));
...
}
With this, you'll be able to dynamically change the filename while your program is running.
I would probably use command line arguments:
public static void main(String[] args)
{
final String mazeFilename = args[0]; // perhaps check if args.length > 0
...
}
then
java YourPrgm Maze.txt
You could try scanning them in through the console and changing them from Strings to ints.
public static void main(String[] args) {
int colSize, rowSize, finishRow, finishCol, startRow, startCol = 0;
// note, through console
Scanner in = new Scanner(System.in);
System.out.print("Enter colSize:");
colSize = Integer.parseInt(in.nextLine());
System.out.print("Enter rowSize:");
rowSize = Integer.parseInt(in.nextLine());
System.out.print("Enter finishRow:");
finishRow = Integer.parseInt(in.nextLine());
System.out.print("Enter finishCol:");
finishCol = Integer.parseInt(in.nextLine());
System.out.print("Enter startRow:");
startRow = Integer.parseInt(in.nextLine());
System.out.print("Enter startCol:");
startCol = Integer.parseInt(in.nextLine());
}
}