Re: How to change my code?
Code :
myNumbers[i]=sc.nextInt();
You have declared myNumbers as a 2D array but are trying to use it like a 1D array.
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?
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.
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?
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!
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");
}
}
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]
Re: How to change my code?
Here you go:
Code java:
/**
* 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");
}
}