Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

Tim Arwine
Tim Arwine
5,406 Points

I'm running workspaces code (Java Basics) in Intelli J, but getting nothing but errors. What am I missing?

I've installed Intelli J. It's working. But not with the code from our earlier course - Java Basics. I must be missing something simple. Please help.

It may be the Console object. I never have luck with that in my own ide. If you are using the Console object try using the Scanner object in replace of that. I can show you if you have never used it before- I think it works better anyway.

Tim Arwine
Tim Arwine
5,406 Points

Jeremy. Yes. Could you show me how to use the scanner object instead. Thank you!

anil rahman
anil rahman
7,786 Points

I showed you in my answer already, explaining everything.

Tim Arwine
Tim Arwine
5,406 Points

Thank you both (anil and Jeremy). The first part with the System.out.printf worked like a charm! Wonderful. I'm still confused on the second part. I am not sure where how (and where) to integrate the Scan object code you gave me into my own. I'm working on the video/lesson that deals with readLine. Here's the code I'm starting with for reference: import java.io.Console; public class Introductions {

public static void main(String[] args) {
    Console console = System.console();
    // Welcome to the Introductions program!  Your code goes below here
    String firstName = console.readLine("What is your name?  ");
   System.out.printf("Hi, My name is %s. I'm a programmer.\n", firstName);
   System.out.printf("%s is learning to program in Java. Yippeee!\n", firstName);

} }

10 Answers

import java.util.Scanner;

public static void main(String[] args) {

    //Console console = System.console();

    Scanner input = new Scanner(System.in);

    // Welcome to the Introductions program!  Your code goes below here

   //String firstName = console.readLine("What is your name?  ");

   System.out.println("What is your name?");
   String firstName = input.nextLine();
   System.out.printf("Hi, My name is %s. I'm a programmer.\n", firstName);
   System.out.printf("%s is learning to program in Java. Yippeee!\n", firstName);
anil rahman
anil rahman
7,786 Points

Yes Jeremy is correct when using the console object in ide's it usually brings back a nullPointerException.

So when using the printf method using console like this:

console.printf("name is %s",name);

Switch it to:

System.out.printf("name is %s",name);

To read data from user input use the Scanner object.

Scanner s = new Scanner(System.in); //the scanner object
String input = s.nextLine(); //read data entered and save in a variable

System.out.printf("You typed %s",input); //output to screen

The code I last wrote will work in your IDE, but not the code challenges, FYI.

Tim Arwine
Tim Arwine
5,406 Points

Jeremy, Thank you so much. Your code worked perfectly. Brilliant. I'm off and running! (And yes. For the challenges, I'll have to use the old code.)

anil rahman
anil rahman
7,786 Points
import java.util.Scanner;

public class MyStory {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    /*  Some terms:
        noun - Person, place or thing
        verb - An action
        adjective - A description used to modify or describe a noun
        Enter your amazing code here!
    */
     System.out.println("Enter your name:  "); //i have split these lines into two, prompt first and get input after
     String name = input.nextLine();
     System.out.println("Enter an adjective:  ");
     String adjective = input.nextLine();
     System.out.printf("%s is very %s", name, adjective);

  }
}

before you used this:

 String name = System.out.println("Enter your name:  "); //prompting and getting value
//but you can't do this with scanner
//i have split these lines into two,
System.out.println("Enter your name:  ");  //prompt first 
String name = input.nextLine(); //and get input after
Tim Arwine
Tim Arwine
5,406 Points

anil, THANK YOU SO MUCH. It works. I see. Your explanation really clarifies it. I was so confused. Anyway, bottom line. Different objects have VERY different behaviors. Thank you again.

Okay first import your Scanner object like this:

import java.util.Scanner;

Then create a Scanner object:

Scanner userInput = new Scanner(System.in);

Now you can use the object in conjunction with System.out to print to the console like this:

String word = userInput.nextLine();

System.out.println("The word or line stored in the variable is: " + word);

// or

System.out.printf("The word or line stored in the variable is:  %s ", word);

And you can even use .next() instead of .nextLine() if you prefer, depending on what the user is inputting.

anil had the code for it, just make sure that you import the Scanner class.

anil rahman
anil rahman
7,786 Points

i didn't bother adding the import because when using intellij the first thing it does is say i'll import all needed statements for you if you are missing it. No one will try add them in manually when using these types of ide's.

Good deal! If you have any more problems or questions let me know :)

Tim Arwine
Tim Arwine
5,406 Points

I definitely will. Thank you!

I just thought of something else that you might like to use in your ide.

Run this code in your ide:

import javax.swing.JOptionPane;

public class Example {

    public static void main(String[] args) {

       String firstName = JOptionPane.showInputDialog(null, "What is your first name?", "First Name",JOptionPane.PLAIN_MESSAGE);
       String lastName = JOptionPane.showInputDialog(null, "What is your last name?", "Last Name",JOptionPane.PLAIN_MESSAGE);
        JOptionPane.showMessageDialog(null, "Your name is: "+firstName+" "+lastName, "Your Name",JOptionPane.PLAIN_MESSAGE);
    }
}
Tim Arwine
Tim Arwine
5,406 Points

Jeremy,
Thank you very much. What or where exactly should I use this? Is this what we use instead of the readline method (to gather imput from user)? Because, once again, I am having trouble getting the code in the Java Basics examples to work in the ide. I'm on the section on user input: Here's what I have and I'm getting multiple errors galore. Please help. import java.util.Scanner;

public class MyStory {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    /*  Some terms:
        noun - Person, place or thing
        verb - An action
        adjective - A description used to modify or describe a noun
        Enter your amazing code here!
    */
     String name = System.out.println("Enter your name:  ");
     String adjective = System.out.println("Enter an adjective:  ");
     System.out.printf("%s is very %s", name, adjective);

}
}

Use anil's example, just remember that when using a scanner object that when you are getting input from the user you must use a System.out.println() statement to prompt the user for input before the line that stores the input. The Console.readLine() allows you to prompt the user and get input in one line but the Scanner object doesn't work like that- It requires two lines of code.

Tim Arwine
Tim Arwine
5,406 Points

Jeremy, THANK YOU. Both you guys really helped me. I never would have thought one (is it method or object?) behaves so different to another, but it is a great lesson for the future. Thank you again! I really appreciate you guys willingness to help me over the humps.

No problem Tim, glad to help :)