Java Java Basics Getting Started with Java Strings and Variables

I am doing the code exactly as Craig is describing, and it is giving me error messages and not showing me what I typed.

Picked up JAVA_TOOL_OPTIONS: -Xmx128m
Picked up _JAVA_OPTIONS: -Xmx128m
error: Class names, 'Introductions', are only accepted if annotation processing is explicitly requested
1 error

4 Answers

Jennifer Nordell
STAFF
Jennifer Nordell
Treehouse Staff

Hi there! I took a look at your code and ran it in workspaces. Unfortunately, it's not exactly like Craig's. There are a couple of lines that are causing errors and one line where I think the output is not what you intended. The first line is on line 8. And it is this:

Introductions.java:8: error: incompatible types: String cannot be converted to boolean           
  String firstName = console.readline("What is your name?  ");   

You wrote:

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

But there is a capitalization error on readline. You meant to write:

String firstName = console.readLine("What is your name?  ");  // note the upper case "L" here

Once I save the file, compile, and run it again, I get a new error (a little scarier) this time on line 10:

Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '%s'       
        at java.util.Formatter.format(Formatter.java:2519)                                       
        at java.util.Formatter.format(Formatter.java:2455)                                       
        at java.io.Console.format(Console.java:170)                                              
        at java.io.Console.printf(Console.java:209)                                              
        at Introductions.main(Introductions.java:10)

I know that looks scary but here's what it's trying to tell us. You specified in your string the %s token to tell it to format the string using a variable name. But from Java's perspective, you never told it which variable to use. This is because you misplaced your ending quotation mark.

You wrote:

console.printf("Hello, my name is %s\n, firstName");

But you meant to write:

console.printf("Hello, my name is %s\n", firstName); // note that I moved the quotation mark before the comma

The last problem won't actually produce an error, but I believe the result is unintended. Regardless of what was typed in, the string "Sam is learning how to write java" and a new line character. But if I entered "Charlie", I'd likely want it to print out "Charlie is learning how to write java", right?

So where you wrote:

console.printf("Sam is learning how to write java\n", firstName);

I believe you meant to write:

console.printf("%s is learning how to write java\n", firstName);  // note that I replaced Sam with %s

I suggest you look at Craig's full code at 2:11 of this video.

Hang in there! You're doing great! These small details take a good deal of practice to spot. If you weren't detail-oriented before, you will be. But the first part of learning anything is knowing how to ask the question, which you did brilliantly! We got a ton of helpful information from you, so thank you for that.

Hope this helps! :sparkles:

Jennifer Nordell
STAFF
Jennifer Nordell
Treehouse Staff

Hi there, Charlie Tombras ! The first two lines of that aren't actually error messages, but rather informational messages about the memory options set on the workspace's JVM. The last line, though is an actual error message. This message generally happens when you use javac but then accidentally omit the .java extension from the file name while issuing the command.

For example, you should type:

javac Introductions.java

I would expect that error if you typed:

javac Introductions

Can you confirm that you're typing the first command and not the second command?

Looking forward to hearing back! :sparkles:

This is an example of another instance I encountered with this:

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?  ");
      //thisIsAnExampleOfCamelCasing
        console.printf("Hello, my name is %s\n, firstName");
        console.printf("Sam is learning how to write java\n", firstName);
  }
}

And then my output at java.util.Formatter.format(Formatter.java:2455)
at java.io.Console.format(Console.java:170)
at java.io.Console.printf(Console.java:209)
at Introductions.main(Introductions.java:10)

I am typing in javac Introductions.java, followed by java Introductions, exactly as it shows on the video. In this case, it should be spitting out the result, but it is not.

Also, the issue is happening within the Treehouse console.