Reading an integer from the command prompt in Java using Scanner - java

I can't figure out what I'm doing wrong here.
My code:
import java.util.Scanner;
public class test
{
public static void main(String args[])
{
Scanner input_scanner = new Scanner(System.in);
System.out.printf("\nEnter number:");
int num_shapes = input_scanner.nextInt();
System.out.printf("\n%d", num_shapes);
}
}
Each time the program is run, I enter an integer, press the enter key, am taken to a new line, enter a new integer, and hit the enter key again. The first integer is then displayed.
How can I get it to display the first integer directly after it is entered, without having to enter a second integer?
I've tried it with and without
input_scanner.nextLine();
following the line containing 'nextInt()', but get the same thing either way.
Any help in resolving this problem is greatly appreciated.

this works perfectly
import java.util.Scanner;
public class mainclass{
public static void main(String args[])
{
Scanner input_scanner = new Scanner(System.in);
System.out.println("Enter number:");
int num_shapes = input_scanner.nextInt();
System.out.println(num_shapes);
}
}

Related

Java user input from class

i'm curious why isn't this working? every time the class gets called it output right away both enter user and enter pass and doesn't let me enter them one by one. I just want the code to take the user input one by one getting called from a class or whatever suggestion you might have.
thanks
output
Enter 1
1
Enter user:
Enter pass:
a
public void takeinfo() {
String a,b;
System.out.println("Enter user: ");
a=input.nextLine();
System.out.println("Enter pass: ");
b=input.nextLine();
}
public static void main(String[] args)throws Exception {
int choice;
choice=input.nextInt();
if(choice==1) {
order.takeinfo();
order.login();
}
else if(choice==2){
order.register();
}
exit();
}
Is this the answer you want?
import java.util.Scanner;
Scanner input = new Scanner(System.in);

Scanner variable cannot be resolved

In my program, the user will be asked to input 3 integers. The integers will then be read using the Scanner class and listed back to the user.
This is my code:
import java.util.Scanner;
public class Echoer
{
public static void main(String[] args)
{
/* The Data Below Will Read The Numbers Input Into The Prompt*/
Scanner input = new Scanner(System.in);
System.out.println("Please Enter Three Integers: ");
int number;
number = input.nextInt();
Scan.close();
System.out.println("Thanks. The Numbers You Entered Are: " + number);
}
}
This is the error it returns:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Scan cannot be resolved
Why does it return this error? How can I fix this issue?
In your code, you never defined what Scan was. Use input.close() rather than Scan.close().
Scan cannot be resolved
means that you never defined Scan. This is because you said Scan.close(). You need to change it to input.close() because input is the name of the instance of the Scanner class.
As others pointed out, you have to close input instead of Scan as shown below.
import java.util.Scanner;
public class Echoer
{
public static void main(String[] args)
{
/* The Data Below Will Read The Numbers Input Into The Prompt*/
Scanner input = new Scanner(System.in);
System.out.println("Please Enter Three Integers: ");
int number;
number = input.nextInt();
input.close();
System.out.println("Thanks. The Numbers You Entered Are: "+number);
}
}

Whats the difference between two multiple user input programs? [duplicate]

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 6 years ago.
import java.util.Scanner;
public class HelloWorld{
public static void main(String args[]){
Scanner dd = new Scanner(System.in);
System.out.println("Enter name");
String b = dd.nextLine();
System.out.println("Enter num");
int num = dd.nextInt();
}
}
And
import java.util.Scanner;
public class HelloWorld{
public static void main(String args[]){
Scanner dd = new Scanner(System.in);
System.out.println("Enter num");
int num = dd.nextInt();
System.out.println("Enter name");
String b = dd.nextLine();
}
}
Why the latter doesn't work peoperly(doesn't let me enter name), while the first one does?
I have made up a new version without that annoying "Scanner scan = new Scanner".
And what about this solution? What cons may it have?
import java.util.Scanner;
public class HelloWorld{
public static void main(String args[]){
System.out.println("Enter num");
int i = new Scanner(System.in).nextInt();
System.out.println("Enter name");
String b = new Scanner(System.in).nextLine();
}
}
The second program expects an Int first and then a name. And hence might be erroring out when a name is entered.
In the second case, nextInt() does not scan over the newline character that the user inputs when pressing the Return key.
In the first case, nextLine() is encountered first so the problem doesn't manifest itself.
The moral of the story is to always use nextLine() and parse the resulting string accordingly. Use something like Integer#parseInt to convert a string to an integer.

Java : Could not load or find main class TrudiHw

I keep getting this error when trying to run my basic java code. It compiles with no errors at all but for some reason the program wont actually run. I am using textpad. Here is my code.
/*
*TrudiHw.java
*Trudi Farrell
*19/10/15
*/
import java.util.Scanner;
public class TrudiHw{
public static void main (String args[]){
int input;
int answer;
Scanner keyboard;
keyboard = new Scanner(System.in);
System.out.println("How many students are attendting the trip?");
input = keyboard.nextInt();
answer = input/5;
System.out.println("The amount of cars you will need is " +answer);
}
}

My code isn't working

I am a beginner programmer and i am trying a program for my father.
import java.util.*;
import java.lang.*;
import java.io.*;
class Employee
{
String m1,m2,m3,m4,m5,m6,m7;
void main()
{
Scanner w=new Scanner(System.in);
Scanner n=new Scanner(System.in);
System.out.println("Please enter your name ");
String name=w.nextLine();
System.out.println("Please choose your client");
System.out.println("1 - XXXXXX");
int client=n.nextInt();
m1=name;//Storing name
if(client==1)//If statement storing client
{
m2="XXXXXX";
}
else
{
System.out.println("You have entered a wrong choice");
return;
}
String msg=m1+"\t"+m2;
System.out.println(msg);
}
}
This Code will give the output "as you have entered a wrong choice'"
It jumps to elsse statement. What is the error and is there an easier way to run this program. Thanks
Could yo please inform me on my error as
Ok try this code:
import java.util.Scanner;
public class Try
{
static String m1,m2,m3,m4,m5,m6,m7;
public static void main(String[] args)
{
Scanner w=new Scanner(System.in);
System.out.println("Please enter your name ");
String name=w.nextLine();
System.out.println("Please choose your client");
System.out.println("1 - XXXXXX");
int client=w.nextInt();
m1=name;//Storing name
if(client==1)//If statement storing client
{
m2="XXXXXX";
}
else
{
System.out.println("You have entered a wrong choice");
return;
}
String msg=m1+"\t"+m2;
System.out.println(msg);
}
}
You have missed you main method signature. In Java there is a specification of main method. Your main method should be like
public static void main(String []args){
}
In your case you main method should be
public static void main(String args[]) {
String m1, m2, m3, m4, m5, m6, m7;
Scanner w = new Scanner(System.in);
Scanner n = new Scanner(System.in);
System.out.println("Please enter your name ");
String name = w.nextLine();
System.out.println("Please choose your client");
System.out.println("1 - XXXXXX");
int client = n.nextInt();
m1 = name;//Storing name
if (client == 1)//If statement storing client
{
m2 = "XXXXXX";
} else {
System.out.println("You have entered a wrong choice");
return;
}
String msg = m1 + "\t" + m2;
System.out.println(msg);
}
Your problem are the 2 scanners.
Because a scanner work with an iterator, that keep the position inside the given inputstream (in this case), when you instantiate the 2 scanners, they both set their iterator at the same position into the stream, then you use "w.nextLine();", and the first scanner advances trough the stream returning the first line, as you wish, but the second scanner, that you haven't used, is still at the beginning of the stream, so basically when you use n.nextInt();, the scanner tries to parse your name as int, and it's strange that it doesn't throws an InputMismatchException, as it should do ("https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt%28%29").
Rework your code as #Sarthak Mittal suggested and it should work.
PS: keep in mind indentation, it's important, really
First:
void main()
There is no such thing in Java. It should be,
public static void main(String[] args)
To know the meanings of public, static, String[] args read this: Explanation of 'String args[]' and static in 'public static void main(String[] args)'
Secondly,
int client = n.nextInt();
The value inside client depends on your input. If you input 2 or 3 instead of 1, your code'll definitely go to the else part. So make sure your input is 1.
Thirdly,
Get rid of the extra scanner. You need only one.
The rest of your code is ok.

Categories

Resources