• Post Reply Bookmark Topic Watch Topic
  • New Topic

String inputs and outputs  RSS feed

 
Carlos Trillo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a new Java learner. This is my first asignment:

Write an application that asks the user to enter his/her first name, last name, birthday, and where you born (all fields type String) and prints their information to the console. Use the techniques discuss in class. The data must be encapsulated. The program must be coded in Notpad++ and compiled in the Command Prompt.

Output should be like this:

Welcome!

What is your first name? Carlos

What is your last name? De La Torre

When is your birthday? 08/12/1979

Where did you born? Puerto Rico


First Name: Carlos

Last Name: De La Torre

Birthday : 08/12/1979

Born in : Puerto Rico

Can you tell me what is wrong with my code. I need help. This is what I have so far :

 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carlos Trillo wrote:Can you tell me what is wrong with my code.

What makes you think there is something wrong with it ? TellTheDetails

Also when posting code it's good to UseCodeTags. It makes the code much easier to read.

Edit: Okay. From a quick look at your code, I'm guessing the problem is that it is not printing out the data after the user has entered it.
Assuming that's the case, all you need to do is add a printInfo method that contains a number of System.out.println calls to print the data and then call this method from your main method.
I also think you need a constructor with no parameters. There's no point in using the constructor that takes name, lastName, birthday and birthPlace parameters because you are going to ask for those.

Edit 2: Apart from that the code looks pretty good for a first attempt.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can add code tags as Joanne suggested, and have tried to correct the indentation, but it is difficult because you appear to be mixing spaces and tabs: look at this.

And welcome again
 
Carlos Trillo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Already coded the constructor with no parameters and the printInfo method as advised. Program still not running...
I attached the errors printed by the command prompt.

Capture.JPG
[Thumbnail for Capture.JPG]
Errors output
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is somewhere around where the printInfo method starts, according to the error message. To be more precise, it's where the askInfo method ends.

In the future, please do not post screenshots of error messages - copy and post the actual text of the messages. That makes it possible to quote them, and to search for them.
 
Carlos Trillo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have reduced the errors to three. This is the message displayed by the command prompt (The code follows):


MyInfo.java:107: error: cannot find symbol
MyInfo.obj = new MyInfo();
^
symbol: variable obj
location: class MyInfo
MyInfo.java:108: error: cannot find symbol
obj.askInfo();
^
symbol: variable obj
location: class MyInfo
MyInfo.java:109: error: cannot find symbol
obj.printInfo();
^
symbol: variable obj
location: class MyInfo
3 errors


 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to replace a single dot character (".") with a space. I'll let you figure out where.
 
Carlos Trillo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally the program compiled without any errors. The file .class was created. But when I try to execute the program,
This is the message the Command Prompt displays:


C:\Users\Carlos\Desktop>javac MyInfo.java

C:\Users\Carlos\Desktop>java MyInfo
Exception in thread "main" java.lang.NoClassDefFoundError: MyInfo (wrong name: m
yinfo/MyInfo)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't quite get the code tags right, but I have corrected them. Push the button on your last post and you can see how they work.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have compiled the file in the myInfo package, the .class file must be in the folder called myInfo.
You start the app from the parent folder of myInfo by writing

java myInfo.MyInfo

I think it would be a good idea to have package names and class names very different from each other. Read about package names in the Java Tutorials.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!