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);
}
}
Related
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);
}
}
I have a simple program that asks for the user's name. I want the space where the user enters his/her name to be next to the line that says "Name: " rather than below it. I know this is a very basic question, but I've already searched through the Java documentation, as well as questions already asked on stackoverflow and I still can't find the answer.
import java.util.Scanner;
public class Main {
private String name;
private Scanner sc = new Scanner(System.in);
public void enterName() {
System.out.println("Please enter your name");
System.out.println("Name: ");
name = sc.nextLine();
}
public static void main(String[] args) {
Main main = new Main();
main.enterName();
}
}
Use print instead of println:
System.out.print("Name: ");
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.
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);
}
}
I am new to java and netbeans. I am trying to write a program that requires user input. This is my code:
public class Arrays {
public static void main(String[] args){
}
private double[] readNumbers(){
final Input in = new Input();
System.out.print("How many numbers will you enter?: ");
final int count = in.nextInt();
final double[] list = new double[count];
for (int i = 0; i < count; ++i){
System.out.print("Enter next number: ");
list[i] = in.nextDouble();
}
return list;
}
}
In line final Input in - new Input(); Netbeans underlines Input saying that it cannot find symbol. However I practically copied this code from the textbook so I don't understand what the problem is. I thought maybe I needed to import java.io, but that did not solve the problem. Really sorry if this is a stupid question, but any help would be really appreciated.
Thank you!
Looks like your text book has some class definition that you forgot to import to your project.
If you like to , change your code like,
final Input in = new Input();
to
final Scanner in = new Scanner(System.in);
If you don't want to change your code, then look few pages up and down the pages from where you got this code, you should see Class named Input , some what similar to this :
class Input{
public int nextInt(){
Scanner sc=new Scanner(System.in);
return sc.nextInt();
}
public double nextDouble(){
Scanner sc=new Scanner(System.in);
return sc.nextDouble();
}
}
Which is basically, an extra unnecessary work.
Include that in your project, and it should run fine.
Your code attempts to create an instance of class Input, but you don't include code for class Input. Resolve that (try the previous page in the book!) and your code will probably work.
Input may be a wrapper class for java.util.Scanner:
You could replace:
final Input in = new Input();
with
final Scanner in = new Scanner(System.in);