Java Java Basics Perfecting the Prototype Parsing Integers

Laurence Cimosz
Laurence Cimosz
6,372 Points

this is not the task, it's the work space prototype. why is the prototype not working

this is not the task, it's the code to the work space prototype. why is the prototype not working. how can I attach my work space code to a post? thank you

ParsingInts.java
import java.io.Console;

public class TreeStory {

    public static void main(String[] args) {
        Console console = System.console();
        /*  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!
        */
int age = 12;
if (age < 13) {
//Insert exit code
console.printf("Sorry you must be at lest 13 to use this program.\n");
System.exit(0); 
}  
String name = console.readLine("Enter a name:  ");
String adjective = console.readLine("Enter an adjective:  ");
String noun = console.readLine("Enter a noun:  ");
String adverb = console.readLine("Enter an adverb:  ");
String verb = console.readLine("Enter a verb ending with -ing:  ");

console.printf("Your TreeStory:\n--------------\n");
console.printf("%s is a %s %s.  ", name, adjective, noun);
console.printf("They are always %s %s.\n", adverb, verb);      


}

1 Answer

Antonio De Rose
Antonio De Rose
20,866 Points

could you please try this, I do not have an answer why the console readLine, always gives an error, it does gives me too, but I am seeing it as an issue for so many, and the recommended class is scanner, try the below code, which I have shaped, as per to your requirement, and as per to fit into java terms, where there will be no errors

package com.example.java;

import java.text.NumberFormat;
import java.util.Locale;
import java.io.Console;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        //Console console = System.console();

        Scanner s = 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!
        */
        int age = 16;
        if (age < 13) {
        //Insert exit code
            System.out.println("Sorry you must be at lest 13 to use this program.\n");
            System.exit(0);
        }

        System.out.println("Enter a name:  ");
        String name = s.nextLine();
        System.out.println("Enter an adjective:  ");
        String adjective = s.nextLine();
        System.out.println("Enter a noun:  ");
        String noun = s.nextLine();
        System.out.println("Enter an adverb:  ");
        String adverb = s.nextLine();
        System.out.println("Enter a verb ending with -ing:  ");
        String verb = s.nextLine();

        System.out.println("Your TreeStory:\n--------------\n");
        String result = String.format(name, adjective, noun, "\"%s is a %s %s.  \", ");
        String result1 = String.format("They are always %s %s.\n", adverb, verb);
        System.out.println(result);
        System.out.println(result1);
    }
}