0

Can't figure out how to fix. I am very new to Java.

import java.util.Scanner;

public class Greetings {
    public static void main(String[] args) {
        System.out.println("Hello there, what is your name? ");
        Scanner input = new Scanner(System.in);
        String = input.nextLine();
        System.out.println("Well then, welcome to Java" + input);
    }
}
2
  • 1
    please declare variable in this line, String =input.nextLine(); because you need to store into a string type variable. String s=input.nextLine(); Commented Oct 16, 2015 at 5:39
  • @Unheilig Please avoid adding unrelated tags, thanks. Commented Oct 18, 2015 at 6:17

4 Answers 4

1

You never actually defined a String variable, which is why the compiler is complaining. Use the code below for maximum results:

public class Greetings {
    public static void main(String[] args) {
        System.out.println("Hello there, what is your name? ");
        Scanner input = new Scanner(System.in);
        String theInput = input.nextLine();
        System.out.println("Well then, welcome to Java" + theInput);
        if (input != null) {
            input.close();   // close the Scanner once finished with it
        }
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

now I am getting this error with your changes: 1 warning found: [line: 11] Warning: Resource leak: 'input' is never closed
Add if (input != null) { input.close(); } to the end of the main method. Ideally you'd handle this in a finally block but I don't think you need to bother with that stuff at this stage.
It's my pleasure (and obligation) to help you.
@TimBiegeleisen "... (and obligation) ..." - Really? That's going a bit far isn't it? Seriously, nobody is under any obligation whatsoever to answer StackOverflow questions. (Except, maybe, StackExchange employees ...)
@StephenC I have a serious SO addiction problem, so maybe this is just me. If I don't wake up with green in my inbox, I just don't feel right.
|
0

You didn't put the name of the String (name of a variable) before the second System Out.

Comments

0

The token String refers to the object type String in the java.lang package. You need to provide java with a variable name so it can know where to grab that specific object from in memory if you request it to do so later on. You've already used the correct syntax for creating the Scanner object so just apply that to the String object:

String nextLine = input.nextLine();

and when referencing this object:

System.out.println("Well then, welcome to Java" + nextLine);

2 Comments

with this and similar changes I now get this error: 1 warning found: [line: 11] Warning: Resource leak: 'input' is never closed
Refer to my comment on Tim's answer.
0

please declare variable in this line, String =input.nextLine(); because you need to store into a string type variable. String s=input.nextLine();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.