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

Need help in Java Code ? getting error !

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package java;

import java.util.Scanner;

public class Java {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner userInput = new Scanner(System.in);

        String name;
        System.out.print("What is Your Name : ");
        name = userInput.next();   
    }    
}

5 Answers

I just ran this in IntelliJ IDEA:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner userInput = new Scanner(System.in);

        String name;
        System.out.print("What is Your Name : ");
        name = userInput.next();
        System.out.printf("Your name is %s", name);
    }
}

Works fine with no package and a renamed class.

Steve.

Lewis Cowles
Lewis Cowles
74,902 Points

Excuse me, but removing the class is not the answer!

Prohibited package name: java

As mentioned below java is a reserved identifier, it would be like using any keyword such as public, or static, etc

Hi Lewis,

Which class got removed?

I suggested renaming the class as the initial code sample had called it Java which, as you say, is a reserved word. Is that what you are referring to?

Cheers,

Steve.

Lewis Cowles
Lewis Cowles
74,902 Points

well both the package and the class are using reserved word java so it's applicable to both mate :wink:

Indeed - I didn't know if this was command line or IDE-based. IntelliJ didn't insist on a package so my code snippet didn't have that and I renamed the class. I think that got Tony up and running with whatever he wanted to do.

Hi Tony,

What's the issue you're having and what are you trying to solve?

Steve.

ohh..sorry i forgot .. sir, i just want to accept input from user using the scanner class... but when i do.. i'am getting errors on netbeans ! Sorry for Bad English !

Your English is fine!

What errors are you getting?

Did you get this working, Tony?

Steve.

java.lang.SecurityException: Prohibited package name: java at java.lang.ClassLoader.preDefineClass(ClassLoader.java:658) at java.lang.ClassLoader.defineClass(ClassLoader.java:794) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:449) at java.net.URLClassLoader.access$100(URLClassLoader.java:71) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:425) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:358) at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482) Exception in thread "main" C:\Users\Sach\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)

Java is a reserved word. You can't call your package that, or your class.

Try changing the names and rebuilding it.

Steve.

Yeah Sir, It worked fine thank you @Steve

Not a problem - glad to help out.

Steve.