Katydid
Katydid

Reputation: 351

java declaring and instantiating a two-dimensional array with given information

Beginner with Java be patient - don't see my problem anywhere else - input is usually read in or random or from a for loop - mine is already given;

How to declare and instantiate a Two-dimensional Array when the information for the rows and columns are given to you; I have 5 names and five numbers attached to each one;

This tells me: Type mismathch, can't convert from int to String String [] [] name = {{"Mary", 50}, {"John", 76}, {"Paul", 99}, {"Peter", 360}, {"Joan", 67}};

String [] [] name = new String [5] [5];

But how do I make name[0] [0] become Mary 50?

Upvotes: 0

Views: 146

Answers (2)

TimoStaudinger
TimoStaudinger

Reputation: 42460

You can only add Strings to your String array.

The numbers you are trying to add are not Strings, but integers.

Try this:

String[][] name = {
    {"Mary", "50"},
    {"John", "76"},
    {"Paul", "99"},
    {"Peter", "360"},
    {"Joan", "67"}
};

If you would like to do calculations with the numbers and they need to be numeric, then you will have to use a different data structure.

Upvotes: 1

user5812699
user5812699

Reputation:

well if you will not make any computation over the integers, you can store them also in string format like this:

String [] [] name = {
{"Mary", "50"}, 
{"John", "76"}, 
{"Paul", "99"}, 
{"Peter", "360"}, 
{"Joan", "67"}
};

Upvotes: 0

Related Questions