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

Eric Tang
PLUS
Eric Tang
Courses Plus Student 1,300 Points

error '{' is expected

Hi , im trying to a code to prompt the users and get an answer in return but it keeps showing error . Could any of you check to see whats wrong.

import java.util.*; public class Introductions() { public static void main(Strings [] args) { Scanner keyboard = new Scanner(System.in); System.out.println("What is you name?"); String name = keyboard.nextLine(); System.out.println("Your name is %s",name);

}

}

6 Answers

kalyada leosrisook
PLUS
kalyada leosrisook
Courses Plus Student 17,855 Points

You can't use formatter '%s' with println. You gotta use it with System.out.printf.

Eric Tang
PLUS
Eric Tang
Courses Plus Student 1,300 Points

Hi kalyada,

Thx for the quick reply . I've tried to change it from println to printf but that doens't seem to be the problem as it still says the same thing. It says error: '{' expected and has an arrow pointing to the parenthesis next to the class name Introductions

kalyada leosrisook
kalyada leosrisook
Courses Plus Student 17,855 Points

oh, I see the problem now. You are not suppose to add any '()' on the class name. Just let the class name be

public class Introduction { //code here. } :)

Eric Tang
PLUS
Eric Tang
Courses Plus Student 1,300 Points

I deleted the parenthesis and now it says error: cannot find symbo

kalyada leosrisook
kalyada leosrisook
Courses Plus Student 17,855 Points

Okay, sorry, the code you posted was pretty hard to read so I've missed some of the mistakes. another mistake I just saw is : 'public static void main (Strings [] args)' you added 's' after the String .. the compiler doesn't recognise 'Strings'.

Eric Tang
PLUS
Eric Tang
Courses Plus Student 1,300 Points

Yea sorry about that. I dont really know how to post it properly so i just copy and paste. I've tried to remove s after string but it still says expected '{' . How do i post the code properly, maybe then you could see clearly what the problem is

kalyada leosrisook
kalyada leosrisook
Courses Plus Student 17,855 Points

did it say which line the error is in? when I wrote you code in my IDE (the fixed version) it works fine.

 import java.util.*;
    public class Introductions
        {
            public static void main(String [] args)
                {
                        Scanner keyboard=new Scanner(System.in);
                    System.out.println("What is you name?");
                    String name=keyboard.nextLine();
                    System.out.printf("Your name is %s",name);
                }
        }

ps. you use this symbol (```) before and after your codes . you can read more in the "Markdown Cheatsheet" below the comment.. :)

Eric Tang
PLUS
Eric Tang
Courses Plus Student 1,300 Points

It still the class name line. Hmmm, could it be because I am using the workshop?

Eric Tang
PLUS
Eric Tang
Courses Plus Student 1,300 Points

Great news. It works now.... Thats really strange. Don't really know what happened, but thx so much kalyada. You've helped heaps. :)

kalyada leosrisook
kalyada leosrisook
Courses Plus Student 17,855 Points

No worries. It could be because you forget to save the file after making changes to it? xD It happens a lot to me haha