Results 1 to 12 of 12

Thread: Java help

  1. #1
    Josha12 is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Smile 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]));
    
        }
    
    }

  2. #2
    tyroceur's Avatar
    tyroceur is offline Member
    Join Date
    May 2010
    Location
    Fast Growing City, INDIA
    Posts
    44
    Rep Power
    0

    Default

    What is the problem that you are getting?
    First, solve the problem. Then, write the code.
    http://www.tyroceur.co.cc ------ If my post was helpful, REP it ;)

  3. #3
    Josha12 is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Default

    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..

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    nothing happens
    I find that hard to believe. What are you doing to make something happen?
    Is there a console that displays the call to the java program with your class file?

  5. #5
    tyroceur's Avatar
    tyroceur is offline Member
    Join Date
    May 2010
    Location
    Fast Growing City, INDIA
    Posts
    44
    Rep Power
    0

    Default

    Yep.... your coding does seem fine to me and it should also have no problem running in NetBeans also.
    First, solve the problem. Then, write the code.
    http://www.tyroceur.co.cc ------ If my post was helpful, REP it ;)

  6. #6
    Josha12 is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Default

    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]));

    }

    }





  7. #7
    tyroceur's Avatar
    tyroceur is offline Member
    Join Date
    May 2010
    Location
    Fast Growing City, INDIA
    Posts
    44
    Rep Power
    0

    Default

    But the code is still incorrect:
    Code looks absolutely fine..... If you are having any problems then post them here

    Regards
    First, solve the problem. Then, write the code.
    http://www.tyroceur.co.cc ------ If my post was helpful, REP it ;)

  8. #8
    Josha12 is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Default

    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)

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    the code is still incorrect:
    Where? Why do you think so? Please explain.

    NoClassDefFoundError: prices/prices (wrong name: Prices/prices)

    Java is case sensitive

  10. #10
    tyroceur's Avatar
    tyroceur is offline Member
    Join Date
    May 2010
    Location
    Fast Growing City, INDIA
    Posts
    44
    Rep Power
    0

    Default

    you are using the wrong package name in your code.....

    write
    Java Code:
    package prices;
    First, solve the problem. Then, write the code.
    http://www.tyroceur.co.cc ------ If my post was helpful, REP it ;)

  11. #11
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by Josha12 View Post
    JOptionPane.showMessageDialog(null,"Your price is \n" + showPrice.format(price[0]));
    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.

  12. #12
    Prajin's Avatar
    Prajin is offline Senior Member
    Join Date
    Jun 2010
    Location
    Ktm, Nepal
    Posts
    120
    Rep Power
    0

    Default

    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]))

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •