Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is wrong with my java code?

 
bue curt
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.*;

public class NickelDollar {
public static void main(String[] args){

BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));


String num = "";
System.out.println("How much Nickel do you have?" );
System.out.println("How much Pennies do you have?" );

try
{
num = BR.readLinne();
int nickel = Interger.parseInt(num);
num = BR.readLinne();
int pennies = Interger.parseInt(num);
int dollar = 0;
int x = (nickel*5)+ pennies;
dollar = x/100;

System.out.println("You have" + x);


}
catch(IOException e) {
Sytem.out.println("Error");
}

}
}
}



I am new to programming, and I am making this ajava program.
The goal of the program is to convert the Inputed Nickel and pennies into dollar and cents.
And how could I make the output of dollar into something like this:
10 dollars and 4 cents.
Instead of just
10 dollars.

Any help will be appreciated, thanks

 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bue curt wrote:



I am new to programming, and I am making this ajava program.
The goal of the program is to convert the Inputed Nickel and pennies into dollar and cents.
And how could I make the output of dollar into something like this:
10 dollars and 4 cents.
Instead of just
10 dollars.

Any help will be appreciated, thanks


Hello and welcome to JavaRanch!

You'll want to use the mod operator, %, I think to figure out your cents. Give it a try and see how it works.

Also, please check out the FAQ on how to ask questions here as it will tell you how to use code tags so that your code retains its formatting and stays beautiful.
 
Praveen Rajendran
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can define one more int variable and assign it the value of reminder of your dollar convertion (ie cents).
eg: int cents = x%100;
Also for printing dollars and cents you would have to print your dollar variable (not x as x gives the whole value in cents alone) and the cents variable as mentioned below.
System.out.println(dollar+" dollars and"+cents+" cents");
 
bue curt
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My problem now is the program would not run, here is the error message,

java.lang.NoClassDefFoundError: javaapplication1/Main
Caused by: java.lang.ClassNotFoundException: javaapplication1.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:303)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316)
Could not find the main class: javaapplication1.Main. Program will exit.
Exception in thread "main"
Java Result: 1
BUILD SUCCESSFUL (total time: 5 seconds)


Thanks




edit:
Its running now, thanks
 
Praveen Rajendran
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please check if your classpath is set appropriately. The error is due to the fact that your class file is not present in the classpath.

Also i have reformatted your program as it had couple of mistakes in java key words like Integer and System.out.println().

You can try usig the below code:

 
Campbell Ritchie
Sheriff
Pie
Posts: 49432
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no end of mis-information about classpaths on the internet. Most of the time you want to leave the system classpath unchanged.

If you are a beginner having difficulty finding class files, then avoid using BufferedReaders which require exception handling as well. Do one thing at a time, not several things.
Get something to work which simply prints a message and see how you compile that before you try the complications of your Nickel class.

Did you compile the .java file from the "current directory" in your command line/terminal? If so, try the dir (DOS/Windows) or ls (Linux etc) commands to see whether you have that .class file there at all.
Have you compiled the Main file at all?
 
bue curt
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes I did compiled it. The program is working now,

Thank you
 
Campbell Ritchie
Sheriff
Pie
Posts: 49432
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done getting it to work

How did you sort it out?
 
Kingsley Pouponneau
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Well done getting it to work

How did you sort it out?


Sir, I would like to Know too. javascript:emoticon(''); I done all that you asked.javascript:emoticon(''); See capture as attachment.
I am a NOOB to Java.
PLEASE HELP

regards

Filename: HelloWorldApp1.bmp
Description:
File size: 404 Kbytes
[Download HelloWorldApp1.bmp] Download Attachment
 
Praveen Rajendran
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kingsley,

I guess it is better to have your question as a seperate thread and not to combine with this one. Also I believe there are lot of other threads with the same question. Recommend you to read through them also.

From your screenshot attached what I can tell you is to check if the CLASSPATH variable has the directory location of your class file ie C:\src.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic