Unexpected error in simple Java program [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm learning Java, and I wanted to make a very basic calculator. But looks like I got a problem right a way!
Here is the code:
import java.util.Scanner;
public class apples{
public static void main(String args[]){
int test = 6;
if(test != 9){
System.out.println("Yes");
}else{
System.out.println("No");
}
}
}
So in Eclipse I tried to run this but the problem is it does not work and show this error:
Exception in thread "main" java.lang.Error: Unresolved compilation
problem:
at apples.main(apples.java:4)

Your second last bracket has some invisible character that you can see a red point(very thin) and if you delete the same your program is perfectly ok.
Incorrect Code having special character in it
import java.util.Scanner;
public class AdvanceCollection {
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
double fnum, snum, answer;
System.out.println("Enter first number: ");
fnum = scan.nextDouble();
System.out.println("Enter second number: ");
snum = scan.nextDouble();
answer = fnum + snum;
System.out.println(answer);
}
}
Correct code with out that issue
import java.util.Scanner;
public class AdvanceCollection {
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
double fnum, snum, answer;
System.out.println("Enter first number: ");
fnum = scan.nextDouble();
System.out.println("Enter second number: ");
snum = scan.nextDouble();
answer = fnum + snum;
System.out.println(answer);
}
}

Change your character at the end to }.

There is no compilation error, just noticed last } is having some invisible character with it. delete it and type again }. Hopefully it should work.
It works for me.

Related

Dividing Floats in Java [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
First day learning Java and I came across this problem: I need to divide 2 floats(given from command line input). I am getting an error every time I input my 2 floats. I included my code down below
import java.util.Scanner;
public class doubledivision
{
public static void main(String [] args)
{
float num1, num2;
Scanner in = new Scanner(System.in);
System.out.print("Please enter two floating point numbers: ");
num1 = in.nextInt();
num2 = in.nextInt();
float result = num1 / num2;
System.out.println(result);
}
}
If you want to read a number of type float from the user, you need to use :
in.nextFloat()
because this in.nextInt() will return a value of type int.
Also you need to consider some things :
You need to clear the scanner since you want 2 floating numbers , why ? because if the user enters :
>>> 1.0 2.0
The space will make your scanner assigne num1 with 1.0 and num2 with 2.0.
So if you want to ask the user 2 times use :
num1 = in.nextFloat();
in.nextLine(); // to refresh
num2 = in.nextFloat();
If the user enters zero (any number) divided by 0 will blow up​ your app , so add a check :
if(num2==0){
System.out.println("NaN");
}
else {
float result = num1 / num2;
System.out.println(result);
}

Java Scanner from second method [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am having problem to invoke Scanner class from second method in main method. My code is this:
import java.util.Scanner;
public class Main{
static void checkAge(int age){
Scanner new_age = new Scanner(System.in);
age = new_age.nextInt();
System.out.println("Enter your age");
if(age < 18){
System.out.println("You are a minor");
} else {
System.out.println("You are of apropriate age");
}
}
public static void main(String[] args){
checkAge();
}
}
I get an error:
Main.java:18: error: '.class' expected
checkAge(Scanner.new_age(int));
^ 1 error
Call you function with arguments : checkAge(18); or checkAge(15);as you checkAge() requires parameters to check the conditions.

illegal start of expression in line 22 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am a beginner,I'm having problem with my very first code.I just trying to solve coupling shaft alignment.
It says, "illegal start of expression".Can someone help me with this problem? thanks
Here's my code:
import java.util.Scanner;
public class Front {
public static void main(String []args){
Scanner input = new Scanner(System.in);
Double A = input.nextDouble();
Double B = input.nextDouble();
Double C = input.nextDouble();
Double S = input.nextDouble();
Double M = input.nextDouble();
Double F = B / A * (S-M)/2 - S/2;
System.out.println("The front foot is: " + F +);
}
}
You missed an operand after your last + in System.out.println("The front foot is: " + F +);
Another option is to use formatted output:
System.out.printf("The front foot is: %f\n", F.floatValue());

Java simple calculator Error [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am very new to java and i'm trying to make a basic calculator. There is another question on this but I changed the things that fixed it for him but it still doesn't work. Here's my code:
import java.util.Scanner;
class HelloWorld{
public static void main(String args[])
int num1;
int num2;
String op;
Scanner input = new Scanner(System.in);
System.out.println("Enter your first number");
num1 = input.nextInt();
System.out.println("Enter your second number");
num2 = input.nextInt();
System.out.println("Enter the operation");
op = input.nextLine();
if (op.equals("*")){
System.out.println("The answer is: " + (num2 * num1));
}
if (op.equals("/")){
System.out.println("The answer is: " + (num2 / num1));
}
if (op.equals("+")){
System.out.println("The answer is: " + (num2 + num1));
}
if (op.equals("-")){
System.out.println("The answer is: " + (num2 - num1));
}
}
}
The error says:
Enter your first number
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at HelloWorld.main(HelloWorld.java:12)
I can't seem to find whats wrong (I have its something simple that i missed)
What are you entering?
Enter your first number
10
Enter your second number
20
Enter the operation
*
The answer is: 200
I'm not sure your program would even compile the way you have it, but here's one problem:
You didn't open your main method with an open curly brace.
Change this:
public static void main(String args[])
to this:
public static void main(String args[]) {
When I tested your code, it terminated after Enter the operation. However, when I changed this line:
op = input.nextLine();
to this:
op = input.next();
It worked perfectly.
Here is an example of the console input/output:
Enter your first number
10
Enter your second number
20
Enter the operation
*
The answer is: 200
Only thing I see wrong is public **satic** void main(String args[]) Other than that it's doing what it should...
Whenever, you read any token from Scanner. You have the facility to check whether it really exists with hasNext() methods. So, you can do
For checking ints :
if(input.hasNextInt())
input.nextInt()
For checking lines :
if(input.hasNextLine())
input.nextLine()
This will get rid of the NoSuchElementException
Update:
System.out.println("Enter the operation");
op = input.next(); //changing it to next() fixed it
Output
run:
Enter your first number
2
Enter your second number
3
Enter the operation
*
The answer is: 6
BUILD SUCCESSFUL (total time: 6 seconds)

Java Scanner Input Errors [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am making a simple Java game called 21 sticks. In the beginning, I want to ask the user his/her name, so I can refer to them by their name for the rest of the game. However, when I use my code(below), first input has the eclipse red squiggly line under it, meaning error. This is not compiling. I have tried to Google the way to fix this, or if there is a better way to do it, but I have fond nothing. I have tried changing input to inputMap, but then the nextLine doesn't work. I would like to know why this is showing up as an error, and if there is a better way to ask for the user's name.
my code:
import java.util.Scanner;
public class TwentyOneSticks {
public static void main(String[] args) {
System.out.println("What is your name?")
String userName = input.nextLine()
int numofSticks = 21;
Scanner input = new Scanner(System.in);
Scanner take = new Scanner(System.in);
System.out.println("Would you like to go first? (Y/N)");
String goFirst = input.nextLine();
try
import java.util.Scanner;
public class TwentyOneSticks {
public static void main(String[] args) {
System.out.println("What is your name?");
Scanner input = new Scanner(System.in); <--- moved it here
String userName = input.nextLine();
int numofSticks = 21;
//Scanner take = new Scanner(System.in); <-- what is use of this?
System.out.println("Would you like to go first? (Y/N)");
String goFirst = input.nextLine();
You have to declare (and initialise) a variable before using it. Also some semicolons were missing.

Categories