Not a statement java ошибка что значит

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's user avatar

Shar1er80

8,9912 gold badges20 silver badges28 bronze badges

asked Jul 6, 2018 at 15:38

Goku's user avatar

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.'s user avatar

Matthew S.Matthew S.

3111 gold badge9 silver badges22 bronze badges

Hi I am new to java programming. I want to write a java program that gets the first name and the surname initials of the user. My code is:

public class MyProgram {
public void start() {
    String name = getNameFromUser();
    char firstNameInitials = getInitials(name);
    char surnameInitials = getInitials(name);
    printInitials(firstNameInitials, surnameInitials);
}
private String getNameFromUser() {
    System.out.print("Please enter your name: ");
    String name = Keyboard.readInput();
    return name;
}
private char getInitials(String name) {
    char firstNameInitials = name.charAt(0);
    int indexOfSpace = name.indexOf(" ");
    char surnameInitials = name.charAt(indexOfSpace + 1);

    return firstNameInitials; surnameInitials;
}

private void printInitials(String firstNameInitials, String surnameInitials){
    System.out.print("your initials are: " + firstNameInitials +". " + surnameInitials +".");
}
}

i am getting an error message, i dont know what it means:

Error: not a statement.

The error seems to be at line 18, at the return statement.
I have tried to fix it but have failed. Any suggestions how can I fix this please?

Ramaraj Karuppusamy's user avatar

asked Jun 1, 2013 at 8:35

Adi's user avatar

1

return firstNameInitials; surnameInitials;

you can not return multiple char like this. Instead use a char array to return

For example you can return like this

private char[] getInitials(String name) {
    char firstNameInitials = name.charAt(0);
    int indexOfSpace = name.indexOf(" ");
    char surnameInitials = name.charAt(indexOfSpace + 1);

    char[] result  = {firstNameInitials, surnameInitials};
    return result;
}

And call it like

char[] a = getInitials(name);
char firstNameInitials = a[0];
char surnameInitials = a[1];

answered Jun 1, 2013 at 8:36

stinepike's user avatar

stinepikestinepike

53.9k14 gold badges92 silver badges112 bronze badges

0

You can return only one value at a time.You can not return more than one value

Create a class that holds multiple values you need. In your method, return an object that’s an instance of that class.

This way, you still return one object. In Java, you cannot return more than one object, whatever that may be.

public class TestVO {

    private char irstNameInitials;
    private char surnameInitials;
    public char getIrstNameInitials() {
        return irstNameInitials;
    }
    public void setIrstNameInitials(char irstNameInitials) {
        this.irstNameInitials = irstNameInitials;
    }
    public char getSurnameInitials() {
        return surnameInitials;
    }
    public void setSurnameInitials(char surnameInitials) {
        this.surnameInitials = surnameInitials;
    }    
}


private TestVOgetInitials(String name) {
char firstNameInitials = name.charAt(0);
int indexOfSpace = name.indexOf(" ");
char surnameInitials = name.charAt(indexOfSpace + 1);

   TestVO vo = new TestVO();
   vo.setIrstNameInitials(firstNameInitials);
    vo.setSurnameInitials(surnameInitials);

return vo; ;

}

answered Jun 1, 2013 at 8:36

PSR's user avatar

PSRPSR

39.5k41 gold badges110 silver badges150 bronze badges

4

error:

return firstNameInitials; surnameInitials;

return only one value;

 printInitials(firstNameInitials, surnameInitials);

either convert the function to take char as parameter or change the arguments to string.

answered Jun 1, 2013 at 8:40

Dineshkumar's user avatar

DineshkumarDineshkumar

4,1675 gold badges28 silver badges43 bronze badges

Please try this. You made some simple mistakes.

public class Testprg
{
   public void start()
   {
    String name = getNameFromUser();
    char firstNameInitials = getFirstNameInitials(name);
    char surnameInitials = getSurNameInitials(name);
    printInitials(firstNameInitials, surnameInitials);
   }

   private String getNameFromUser()
   {
    System.out.print("Please enter your name: ");
    String name = Keyboard.readInput();
    return name;
   }

   private char getFirstNameInitials(String name)
   {
    char firstNameInitials = name.charAt(0);

    return firstNameInitials;
   }

   private char getSurNameInitials(String name)
   {
    int indexOfSpace = name.indexOf(" ");
    char surnameInitials = name.charAt(indexOfSpace + 1);

    return surnameInitials;
   }

   private void printInitials(char firstNameInitials, char surnameInitials)
   {
    System.out.print("your initials are: " + firstNameInitials + ". " + surnameInitials + ".");
   }
}

answered Jun 1, 2013 at 9:19

Ramaraj Karuppusamy's user avatar

public class ShowCurrentTime
{
    public static void main (String [] args)
    {
        //obtain the total milliseconds since Midnight, Sept 1, 2016
        long totalMilliseconds - System.currentTimeMillis();

        //total seconds
        long totalSeconds = totalMilliseconds / 1000;

        long currentSeconds = (int) (totalSeconds) % 60;

        //obtain total minutes
        long totalMinutes = totalSeconds / 60;

        //obtain current minute
        long currentMinute = (int) (totalMinutes) % 60;

        long toalHours = totalMinute / 60;

        long currentHour = (int) (totalHours % 24);

        System.out.println ("Current time is " + currenHour + ":" 
                                + currentMinute + " " + " " + currentSeconds 
                                + "GMT");
    }
}

slfan's user avatar

slfan

8,920115 gold badges65 silver badges78 bronze badges

answered Oct 1, 2016 at 22:53

Madi's user avatar

1

If you are getting the following error(s) at compilation time –

which may be accompanied by –

Error: ';' expected 
OR
Error: ')' 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);

Digiprove sealCopyright © 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);

Jason Anders

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! :dizzy:

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() ничего не возвращает, поэтому и была ошибка.

Операнд — элемент данных, над которым производятся машинные операции.

Понравилась статья? Поделить с друзьями:
  • Not a legal oleaut date ошибка
  • Not a group by expression oracle ошибка
  • Not a git repository ошибка
  • Nosuchelementexception selenium вывод ошибки
  • Norton ошибка 3048 3 что это