Results 1 to 12 of 12
Thread: Java help
- 06-17-2010, 02:37 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 4
- Rep Power
- 0
Java help
Hi
I was just wondering if anyone could help me with abit of coding.. i now the basic and have a task which i have to hand into school tmrw and cant seem to get my head around it. Im not sure were ive gone wrong. the point of this program is to allow me to enter a number : say 5, then i have to enter 5 differnet numbers which will be stored into an array.. but i cant seem to get it rite..
If anybody could help me that would be great.. :)
Thanks
Java Code:package Prices; import javax.swing.JOptionPane; import java.util.Arrays; import java.text.*; import java.util.*; public class prices { public static void main(String[] args) { String input; String message; DecimalFormat showPrice = new DecimalFormat("$,###.00"); input = JOptionPane.showInputDialog("Enter the amount of prices you are going to enter!?"); int size = Integer.parseInt(input); double price[] = new double[size]; for(int i = 0; i < size ; i++){ input = JOptionPane.showInputDialog("Enter the price!?" + (i+1)); price[i] = Double.parseDouble(input); } for(int i = 0; i < size ; i++){ } Arrays.sort(price); JOptionPane.showMessageDialog(null,"Your price is \n" + showPrice.format(price[0])); } }
- 06-17-2010, 02:43 PM #2
What is the problem that you are getting?
http://www.tyroceur.co.cc ------ If my post was helpful, REP it ;)First, solve the problem. Then, write the code.
- 06-17-2010, 02:51 PM #3
Member
- Join Date
- Jun 2010
- Posts
- 4
- Rep Power
- 0
tyroceur the code has no problems with it, but when i run it in netbeans, nothing happens.. ive tryed reinstalling netbeans and i get the same problem.. im quite sure my coding right but im not exactly sure wats the problem..
- 06-17-2010, 02:59 PM #4
I find that hard to believe. What are you doing to make something happen?nothing happens
Is there a console that displays the call to the java program with your class file?
- 06-17-2010, 03:00 PM #5
Yep.... your coding does seem fine to me and it should also have no problem running in NetBeans also.
http://www.tyroceur.co.cc ------ If my post was helpful, REP it ;)First, solve the problem. Then, write the code.
- 06-17-2010, 03:06 PM #6
Member
- Join Date
- Jun 2010
- Posts
- 4
- Rep Power
- 0
Yep i figured it out.. since im using a laptop and im the screen resolution is out of wack it wasnt showing me theoutput window.. But the code is still incorrect:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Prices;
import javax.swing.JOptionPane;
import java.util.Arrays;
import java.text.*;
import java.util.*;
public class prices {
public static void main(String[] args) {
String input;
String message;
DecimalFormat showPrice = new DecimalFormat("$,###.00");
input = JOptionPane.showInputDialog("Enter the amount of prices you are going to enter!?");
int size = Integer.parseInt(input);
double price[] = new double[size];
for(int i = 0; i < size ; i++){
input = JOptionPane.showInputDialog("Enter the price!?" + (i+1));
price[i] = Double.parseDouble(input);
}
for(int i = 0; i < size ; i++){
}
Arrays.sort(price);
JOptionPane.showMessageDialog(null,"Your price is \n" + showPrice.format(price[0]));
}
}
- 06-17-2010, 03:08 PM #7
Code looks absolutely fine..... If you are having any problems then post them hereBut the code is still incorrect:
Regardshttp://www.tyroceur.co.cc ------ If my post was helpful, REP it ;)First, solve the problem. Then, write the code.
- 06-17-2010, 03:08 PM #8
Member
- Join Date
- Jun 2010
- Posts
- 4
- Rep Power
- 0
This is the error i get
run:
java.lang.NoClassDefFoundError: prices/prices (wrong name: Prices/prices)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader. java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java :616)
at java.security.SecureClassLoader.defineClass(Secure ClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader .java:283)
at java.net.URLClassLoader.access$000(URLClassLoader. java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java: 197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 48)
Could not find the main class: prices.prices. Program will exit.
Exception in thread "main" Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
- 06-17-2010, 03:08 PM #9
Where? Why do you think so? Please explain.the code is still incorrect:
NoClassDefFoundError: prices/prices (wrong name: Prices/prices)
Java is case sensitive
- 06-17-2010, 03:11 PM #10
you are using the wrong package name in your code.....
write
Java Code:package prices;
http://www.tyroceur.co.cc ------ If my post was helpful, REP it ;)First, solve the problem. Then, write the code.
- 06-17-2010, 03:57 PM #11
because you sort your array the output prince[0] prints always the smallest price. i could run your code successfully. try it out:
Java Code:package prices; import javax.swing.JOptionPane; import java.util.Arrays; import java.text.*; import java.util.*; public class prices { public static void main(String[] args) { String input; String message; int sum = 0; DecimalFormat showPrice = new DecimalFormat("$,###.00"); input = JOptionPane .showInputDialog("Enter the amount of prices you are going to enter!?"); int size = Integer.parseInt(input); double price[] = new double[size]; for (int i = 0; i < size; i++) { input = JOptionPane.showInputDialog("Enter the prices!?" + (i + 1)); price[i] = Double.parseDouble(input); } for (int i = 0; i < size; i++) { sum += price[i]; } Arrays.sort(price); JOptionPane.showMessageDialog(null, "Your prices is \n" + showPrice.format(price[0])); } }
as you can see, i used the package name prices and the class name prices, even if by convention the classnames begins with a capital letter.
- 06-30-2010, 11:35 AM #12
No wrong with the code. As I can see, it's what you want program to do is not clear.
Arrays.sort(price); -> it sorts the array ascending order(smaller to bigger) &
you get smallest price as output, look at the last line -
JOptionPane.showMessageDialog(null,"Your price is \n" + showPrice.format(price[0]))


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks