Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Basics Perfecting the Prototype Censoring Words - Looping Until the Value Passes

subhash gc
subhash gc
1,851 Points

What is the difference between java.io.console and scanner. Looks like io console don't work in Eclipse.

I am trying to understand the console concept here. I understand it is a class and need to import the io console. What is the difference between console and scanner. Secondly, why we cannot use it in IDE like Eclipse.

Adam Fields
Adam Fields
Courses Plus Student 1,652 Points

If I am not mistaken, IDE Eclipse using something called javaw which console commands will not work with. Instead, you must use the Scanner class if you are using Eclipse. I have worked out the TreeStory example (Java Basics) from this course in Eclipse if you still need any help. I tried to snapshot my workspace but ran into problems so here is the raw code(sorry if the formatting is bad):

import java.util.Scanner;

public class EclipseTreeStory {

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    System.out.println("What is your first name? "); // prompts for first name input
    String firstName = scanner.nextLine();

    System.out.println("How old are you? "); // prompts for user age input
    int age = scanner.nextInt();
    // String ageAsString = Integer.toString(age); //Converts 'int age' to a String
    // variable
    if (age < 13 || age > 21) {
        System.out.println("You must be at least 13 to use this program. Exiting....");
        System.exit(0);
    }

    System.out.println("Enter and adjective: "); // prompts for adjective input
    String adjective = scanner.next();

    String noun;
    boolean isInvalidWord;
    // The do-while loop looks for illegal words to use...dork and jerk in this case
    do {
        System.out.println("Enter an noun: ");
        noun = scanner.next();
        isInvalidWord = noun.equalsIgnoreCase("dork") || noun.equalsIgnoreCase("jerk");
        if (isInvalidWord) {
            System.out.print("That language is not allowed.  Try again... \n \n");
        }
    } while (isInvalidWord);

    System.out.println("Enter a verb ending in -ing:  "); // prompts for verb input
    String verb = scanner.next();

    System.out.println("Enter an adverb: "); // prompts for adverb input
    String adverb = scanner.next();

    // Takes in the input variables and creates the final Tree Story output
    System.out.printf("Your TreeStory:\n---------------\n");
    System.out.printf("%s is a %s %s. ", firstName, adjective, noun);
    System.out.printf("They are always %s %s.\n", verb, adverb);

    scanner.close();
}

}

1 Answer

no it doesnt