Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Calculating Price with Command Line - Please Help!

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Calculating Price with Command Line - Please Help!

    I'm trying to write a fairly simple program to calculate the price of some items. Basically, someone could type these items into the command line and it would be able to add them up and output the total. I seem to be going in circles and can't figure out this command line stuff/interacting with loops and so forth. I'm really buggard, so if someone could give me a hand I would really appreciate it.



  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Calculating Price with Command Line - Please Help!

    Show what you have so far, what your problems are, what exceptions are being thrown etc then we can progress from there.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calculating Price with Command Line - Please Help!

    public static void main(String[] args) {
    Store one = new Store();

    try {

    }
    catch(ArrayIndexOutOfBoundsException e){
    System.out.println("Error. You must enter command line");
    System.out.println(e.getMessage());
    }
    catch (NumberFormatException e){
    System.out.println("Error. First argument must be an integer");
    }
    finally {
    System.out.println("Program has ended");
    }


    public class Store {

    private String price;
    private String time;

    public void calculatePrice(String[] args){


    int [] prices = new int[]{5, 15, 20};

    int service a = 20;
    int service b = 15;
    int service c = 5;

    for(int i=0; i < prices.length ; i++)
    price = price + prices[i];
    }

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Calculating Price with Command Line - Please Help!

    Next time you post, please use code tags around your code, such as ones in my signature.
    For now Ill repost your code in a readable form.
    public class Store {
     
        private String price;
        private String time;
     
        public void calculatePrice(String[] args) {
            int[] prices = new int[]{5, 15, 20};
     
            int service a = 20;
            int service b = 15;
            int service c = 5;
     
            for (int i = 0; i < prices.length; i++) {
                price = price + prices[i];
            }
        }
     
        public static void main(String[] args) {
            Store one = new Store();
     
            try {
            } catch (ArrayIndexOutOfBoundsException e) {
                System.out.println("Error. You must enter command line");
                System.out.println(e.getMessage());
            } catch (NumberFormatException e) {
                System.out.println("Error. First argument must be an integer");
            } finally {
                System.out.println("Program has ended");
            }
        }
    }

    Ok, to start off... int service a = 20; is wrong as service a is the identifier of your variable and having a space between service and a is illegal.
    An example of a legal identifer would be serviceA.

    To read user input from the console, you should take a look at the Scanner class.
    Scanner (Java 2 Platform SE 5.0)
    For a neat little guide on how to use Scanner, take a look at this
    How-To-Guide
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. command line arguments
    By rizla in forum Member Introductions
    Replies: 3
    Last Post: December 12th, 2010, 11:14 PM
  2. How do i encrypt my password at command line
    By AHOT in forum Java Theory & Questions
    Replies: 5
    Last Post: October 13th, 2010, 03:13 PM
  3. A problem with command line compilation
    By goodguy in forum Java Theory & Questions
    Replies: 4
    Last Post: August 2nd, 2010, 10:58 AM
  4. [SOLVED] Command Line Argument Help
    By EmSaint in forum Loops & Control Statements
    Replies: 2
    Last Post: January 28th, 2010, 10:55 AM
  5. Multi-Valued Command Line Arguments
    By joey86 in forum Java Theory & Questions
    Replies: 1
    Last Post: December 29th, 2009, 11:19 AM