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 9 of 9

Thread: How to change my code?

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How to change my code?

    This is the question I was trying to answer:
    Courier service. You have a courier service where the different routes are fixed. Create an
    application (for example using array) where you can include (a fixed size) price table for different
    distances your company carries packages. First ask for the user to input daily prices for all the
    different routes. Then ask the user to input a departing and a destination for one delivery. Finally
    provide the user with the correct price for the route.

    The code I've got is below and it is working, but I want it to be that the user can input the daily prices (as asked in the exercise). I tried it with replacing the myNumbers part by

    {
    Scanner sc = new Scanner(System.in);
    int myNumbers[] = new int[5];


    for (int i=0;i<myNumbers.length;i++) {
    System.out.println("Give a number");
    myNumbers[i]=sc.nextInt();
    }
    }

    That doesn´t work unfortunately. What should I do? Could anyone please help me? Thank you in advance!


    import java.util.*;
    public class CourierService {
    public static int getPrice (int location, int destination) {

    int [] [] myNumbers = {{0,10,33,99,11}, {10,0,88,55,11}, {33,88,0,66,11},
    {99,55,66,0,11}};

    int price = myNumbers[location] [destination];
    return(price);
    }

    public static void main(String[] args ) {
    int location;
    int destination;
    int price;
    Scanner skanneri = new Scanner(System.in);
    // read in users location
    System.out.println("Welcome to the price calculator");
    System.out.println("What is your location?");
    System.out.println("0. Center ");
    System.out.println("1. Kaisaniemi ");
    System.out.println("2. Kamppi");
    System.out.println("3. Eira ");
    System.out.println("4. Kallio ");
    System.out.println(" ");
    System.out.println("Write number 0 to 4 ");
    location=skanneri.nextInt();
    // read in users destination
    System.out.println("Welcome to the price calculator");
    System.out.println("What is your location?");
    System.out.println("0. Center ");
    System.out.println("1. Kaisaniemi ");
    System.out.println("2. Kamppi");
    System.out.println("3. Eira ");
    System.out.println("4. Kallio ");
    System.out.println(" ");
    System.out.println("Write number 0 to 4 ");
    destination=skanneri.nextInt();

    //find out the price and display it
    price=getPrice(location, destination); //send to the getPrice
    System.out.println(" This delivery costs "+price+" euros");
    }
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: How to change my code?

    myNumbers[i]=sc.nextInt();
    You have declared myNumbers as a 2D array but are trying to use it like a 1D array.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to change my code?

    Then I thought I could use this:

    {

    int [] [] myNumbers = {
    {new int[5]}, {new int[5]}, {new int[5]}, {new int[5]}, {new int[5]}
    };

    Scanner sc = new Scanner(System.in);
    for (int i=0;i<myNumbers.length;i++) {
    System.out.println("Give a number");
    myNumbers[i]=sc.nextInt();
    }
    }

    And it tells me incompatible types, why?

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: How to change my code?

    I just told you why. You have declared myNumbers as a 2D array.

    Simple answer: you need two indices to access an element (array[x][y]).
    Long answer: a 2D array is a 1D array of 1D arrays. So each element of the array is an array itself. So saying array[x] you are acessing an array not an int. Hence the incompatible types.
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to change my code?

    I really appreciate the help, but I still don't get it. I'm a very beginner in Java, sorry. I thought I have to change the int's after new in Array, but then it tells me: cannot find symbol - class array. Can you try to help me again?

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: How to change my code?

    "array" was my general variable name for an array in my example. You of course would use the name of your variable. Common sense!
    Improving the world one idiot at a time!

  7. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to change my code?

    Got it working now, but one more question. I want it to first ask for the prices and then for the location and destination. How can I get it working like that?
    Here is my code:

    import java.util.*;
    public class CourierService {
    public static int getPrice (int location, int destination) {

    int[][] myNumbers = new int[5][5];
    Scanner skanneri = new Scanner(System.in);
    System.out.println("A price table looks like the example below: ");
    System.out.println(" Center Kaisaniemi Kamppi Eira Kallio ");
    System.out.println("Center 10 20 30 40 50 ");
    System.out.println("Kaisaniemi 20 10 40 30 30 ");
    System.out.println("Kamppi 30 40 10 20 60 ");
    System.out.println("Eira 40 30 20 10 50 ");
    System.out.println("Kallio 50 30 60 50 10 ");
    System.out.println("Now enter the daily prices for the different routes: ");
    System.out.println("Start providing all the numbers for Center and then move on to Kaisaniemi and further: ");
    for (int i=0; i<5; i++)
    for (int j=0; j<5; j++)
    myNumbers[i][j] = skanneri.nextInt();
    int price = myNumbers[location][destination];
    return(price);
    }

    public static void main(String[] args ) {
    int location;
    int destination;
    int price;
    Scanner skanneri = new Scanner(System.in);
    // read in users location
    System.out.println("Welcome to the price calculator");
    System.out.println("What is your location?");
    System.out.println("0. Center ");
    System.out.println("1. Kaisaniemi ");
    System.out.println("2. Kamppi");
    System.out.println("3. Eira ");
    System.out.println("4. Kallio ");
    System.out.println(" ");
    System.out.println("Write number 0 to 4 ");
    location=skanneri.nextInt();
    // read in users destination
    System.out.println("What is your destination?");
    System.out.println("0. Center ");
    System.out.println("1. Kaisaniemi ");
    System.out.println("2. Kamppi");
    System.out.println("3. Eira ");
    System.out.println("4. Kallio ");
    System.out.println(" ");
    System.out.println("Write number 0 to 4 ");
    destination=skanneri.nextInt();

    //find out the price and display it
    price=getPrice(location, destination); //send to the getPrice
    System.out.println(" This delivery costs "+price+" euros");
    }
    }

  8. #8
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: How to change my code?

    Please edit your code and wrap code tags around your code so we can read it. Do something like:

    [code=java]
    < your code goes here>
    [/code]

  9. #9
    Junior Member
    Join Date
    Nov 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to change my code?

    Here you go:

    /**
     * The program provides the user with the prices for a courrier service.
     */
     
    import java.util.*;
    public class CourierService {
        public static int getPrice (int location, int destination) {
     
            int[][] myNumbers = new int[5][5];
            Scanner skanneri = new Scanner(System.in);
            System.out.println("A price table looks like the example below: ");
            System.out.println("           Center Kaisaniemi Kamppi Eira Kallio ");
            System.out.println("Center       10       20       30    40    50 ");
            System.out.println("Kaisaniemi   20       10       40    30    30 ");
            System.out.println("Kamppi       30       40       10    20    60 ");
            System.out.println("Eira         40       30       20    10    50 ");
            System.out.println("Kallio       50       30       60    50    10 ");
            System.out.println("Now enter the daily prices for the different routes: ");
            System.out.println("Start providing all the numbers for Center and then move on to Kaisaniemi and further: ");
            for (int i=0; i<5; i++)
            for (int j=0; j<5; j++)
            myNumbers[i][j] = skanneri.nextInt();
            int price = myNumbers[location][destination];
            return(price);
        }
     
        public static void main(String[] args ) {
            int location;
            int destination;
            int price;
            Scanner skanneri = new Scanner(System.in);
            // read in users location
            System.out.println("Welcome to the price calculator");
            System.out.println("What is your location?");
            System.out.println("0. Center ");
            System.out.println("1. Kaisaniemi ");
            System.out.println("2. Kamppi");
            System.out.println("3. Eira ");
            System.out.println("4. Kallio ");
            System.out.println(" ");
            System.out.println("Write number 0 to 4 ");
            location=skanneri.nextInt();
            // read in users destination
            System.out.println("What is your destination?");
            System.out.println("0. Center ");
            System.out.println("1. Kaisaniemi ");
            System.out.println("2. Kamppi");
            System.out.println("3. Eira ");
            System.out.println("4. Kallio ");
            System.out.println(" ");
            System.out.println("Write number 0 to 4 ");
            destination=skanneri.nextInt();
     
            //find out the price and display it
            price=getPrice(location, destination); //send to the getPrice
            System.out.println(" This delivery costs "+price+" euros");
        }
    }

Similar Threads

  1. Other ways i can change this code
    By tylerk888 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 14th, 2012, 10:23 AM
  2. change java code project to exe file
    By mostafa_mohamed in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 5th, 2012, 10:55 AM
  3. How to change an Imagepath
    By pottsiex5 in forum AWT / Java Swing
    Replies: 3
    Last Post: October 9th, 2011, 05:25 PM
  4. [SOLVED] Can't get boolean to change!!
    By Day2Day in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 2nd, 2010, 07:20 PM
  5. Change to JOptionPane
    By Liuric in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 1st, 2010, 12:07 AM