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 trialTony K
Courses Plus Student 442 PointsNeed 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
Steve Hunter
57,712 PointsI 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.
Steve Hunter
57,712 PointsHi Tony,
What's the issue you're having and what are you trying to solve?
Steve.
Tony K
Courses Plus Student 442 Pointsohh..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 !
Steve Hunter
57,712 PointsYour English is fine!
What errors are you getting?
Steve Hunter
57,712 PointsDid you get this working, Tony?
Steve.
Tony K
Courses Plus Student 442 Pointsjava.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)
Steve Hunter
57,712 PointsJava is a reserved word. You can't call your package that, or your class.
Try changing the names and rebuilding it.
Steve.
Tony K
Courses Plus Student 442 PointsYeah Sir, It worked fine thank you @Steve
Steve Hunter
57,712 PointsNot a problem - glad to help out.
Steve.
Lewis Cowles
74,902 PointsLewis Cowles
74,902 PointsExcuse me, but removing the class is not the answer!
As mentioned below java is a reserved identifier, it would be like using any keyword such as public, or static, etc
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsHi 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
74,902 PointsLewis Cowles
74,902 Pointswell both the package and the class are using reserved word
java
so it's applicable to both mateSteve Hunter
57,712 PointsSteve Hunter
57,712 PointsIndeed - 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.