I am new to java programming. I tried hello world program, but I got an error «not a statement«. Whereas when I copy, paste the hello world program from the internet, my program compiled. This is the program I used. What is meant by «not a statement«, please explain why I got this error and what is meant by it and what should I look for when I get this error in the future. Thanks!
public class hello
{
public static void main(String args[]) {
System.out.println(“hello world”);
}
}
My errors:-
hello.java:8: error: illegal character: 'u201c'
System.out.println(“hello world”);
^
hello.java:8: error: ';' expected
System.out.println(“hello world”);
^
hello.java:8: error: illegal character: 'u201d'
System.out.println(“hello world”);
^
hello.java:8: error: not a statement
System.out.println(“hello world”);
^
4 errors
Shar1er80
8,9912 gold badges20 silver badges28 bronze badges
asked Jul 6, 2018 at 15:38
3
You code cannot contain smart quotes like your used in your «Hello World». I replaced your smart/fancy quotes with the correct kind.
public class hello
{
public static void main(String args[]) {
System.out.println("hello world");
}
}
answered Jul 6, 2018 at 15:47
Matthew S.Matthew S.
3111 gold badge9 silver badges22 bronze badges
public class Hello
{
public static void main(String args[])
{
int i = 1;
for(i; ;i++ )
{
System.out.println(i);
}
}
}
I would like to understand why above code is giving error as:
not a statement for(i ; ; i++)
dbush
204k21 gold badges217 silver badges271 bronze badges
asked Feb 18, 2015 at 15:31
3
Because the raw i
in the first position of your for
is not a statement. You can declare and initialize variables in a for
loop in Java. So, I think you wanted something like
// int i = 1;
for(int i = 1; ;i++ )
{
System.out.println(i);
}
If you need to access i
after your loop you could also use
int i;
for(i = 1; ; i++)
{
System.out.println(i);
}
Or even
int i = 1;
for(; ; i++)
{
System.out.println(i);
}
This is covered by JLS-14.4. The for
Statement which says (in part)
A for statement is executed by first executing the ForInit code:
If the ForInit code is a list of statement expressions (§14.8), the expressions are evaluated in sequence from left to right; their values, if any, are discarded.
answered Feb 18, 2015 at 15:37
Elliott FrischElliott Frisch
197k20 gold badges156 silver badges248 bronze badges
The lone i at the start of the for statement doesn’t make any sense — it’s not a statement. Typically the variable of the for loop is initialized in the for statement as such:
for(int i = 1;; i++) {
System.out.println(i);
}
This will loop forever though, as there is no test to break out of the for loop.
answered Feb 18, 2015 at 15:42
Change your for loop to:
for(; ;i++ )
It will loop infinitely printing i. Your i is not of boolean type which you could have place in condition of for loop and for loop has format like:
for (init statement; condition; post looping)
So in your init statement you just had i which is not a valid statement and hence you get error from compiler.
answered Feb 18, 2015 at 15:34
SMASMA
36.2k8 gold badges49 silver badges73 bronze badges
If you are getting the following error(s) at compilation time –
which may be accompanied by –
Error: ';' expected
ORError: ')' expected
Then there are two possible reasons for these compiler errors to happen –
Possible Reason 1: Applicable in case of Java 8 lambda expressions – When trying to assign a Java 8 Lambda ExpressionRead Lambda Expressions Tutorial to a Functional Interface
Click to Read Detailed Article on Functional Interfaces instance like this –
import java.util.function.Function;
public class ErrorExample{
public static void main(String args[]){
Function func= Integer i -> i.toString();
func.apply(10);
}
}
The lambda assignment statement above will give the compilation errors – Error: not a statement along with Error: ‘;’ expected
Solution: Enclose the statement Integer i
in parenthesis/circular brackets. This is because when specifying the type of an argument in lambda expressions it is mandatory to add parenthesis around the arguments.
The correct assignment statement without the compilation errors would then be
Function func= (Integer i) -> i.toString();
Possible Reason 2: Applicable to Java Code in General – Compiler expected a statement in that line but instead got something different. Lets see couple of examples to understand the scenarios in which this error could occur –
Example 1:
Incorrect: if (i == 1) "one";
The above statement will give a “Error: not a statement” compilation error because its actually not a proper statement. The corrected statement is –
Corrected: if(i==1) System.out.println("one");
Example 2:
Incorrect: System.out.println("The "+"value of ";+i+" is "+i+" and j is"+j);//i.j being integers
The above statement will give “Error: not a statement” along with “Error: ‘)’ expected” compilation errors because of the incorrectly added extra semicolon(;) after “value of ” in the above statement. If we remove this extra semicolon then the compiler errors are removed.
Corrected: System.out.println("The "+"value of "+i+" is "+i+" and j is"+j);
Copyright © 2014-2022 JavaBrahman.com, all rights reserved.
When you compile code in java, and you have an error, it show you the error. One of the errors is «not a statement».
IO.java
String firstName = console.readLine ("What is your first name?") ; String lastName = console.readLine ("What is your last name?"); console.printf = ("First name %s"); firstName; //The console.printf part has the error on it.
3 Answers
Shadd Anderson June 14, 2016 1:34am
When forming a printf statement, the format is («sentence with placeholders«,objects to replace placeholders);
In other words, instead of closing the parentheses and separating «firstName» with a semicolon, simply place a comma after the quotes, then put firstName, and then close the parentheses.
console.printf("First name: %s",firstName);
MOD
Hey Christina,
There are a few things wrong with the printf
line:
-
console.printf
is a method and cannot have an equal sign. As you have it, it’s trying to assign the string as a variable to the method, which can’t be done. -
The
firstName
variable needs to be inside of the parenthesis in order for it to be attached to the placeholder. -
You have a semi-colon where a comma should be.
Below is the correct line of code for you to review. I hope it makes sense.
console.printf("First name: %s", firstName);
Keep Coding!
Philip Gales June 14, 2016 1:38am
line 3 you used firstName;. This not a statement even though it is well-dressed. Below is the code you need for the challenge, make sure you understand line 3 as I have fixed it. I assume you placed the firstName outside of the last closing parenthesis because it told you (when you use console.printf = (); it will not let you use the formatted %s properly and will show random errors).
String firstName = console.readLine ("What is your first name?") ; String lastName = console.readLine ("What is your last name?"); console.printf("First name: %s", firstName); console.printf("Last name: %s", lastName);
String text = "Vova", myText = "Vova";
System.out.print(text.equals(myText) ? "Переменные схожи" : "Переменные различаются");
Тернарный оператор
?:
в Java единственный оператор, который принимает три операнды.
логическоеВыржанеие ? выражение1: выражение2
Первый операнд должен быть логическим выражением. Второй и третий операнды — любым выражением, которое возвращает любое значение.
System.out.print()
ничего не возвращает, поэтому и была ошибка.
Операнд — элемент данных, над которым производятся машинные операции.