wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes What is wrong with my java code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is wrong with my java code?" Watch "What is wrong with my java code?" New topic
Author

What is wrong with my java code?

bue curt
Greenhorn

Joined: Dec 25, 2009
Posts: 14
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

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Dec 25, 2009
Posts: 21
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");


Thanks & Regards,
Praveen
bue curt
Greenhorn

Joined: Dec 25, 2009
Posts: 14
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

Joined: Dec 25, 2009
Posts: 21
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

Joined: Oct 13, 2005
Posts: 39478
    
  28
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

Joined: Dec 25, 2009
Posts: 14
yes I did compiled it. The program is working now,

Thank you
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Well done getting it to work

How did you sort it out?
Kingsley Pouponneau
Greenhorn

Joined: Dec 27, 2009
Posts: 1
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



[Download HelloWorldApp1.bmp] Download

Praveen Rajendran
Greenhorn

Joined: Dec 25, 2009
Posts: 21
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is wrong with my java code?