Проблема с созданием строкового массива массивов int - PullRequest
0 голосов
/ 14 ноября 2011

Мне нужно создать массив целых чисел, содержащихся в массиве слов. Я получаю сообщение об ошибке

не может конвертировать из int[] в java.lang.String[]

где написано

private String[][] expenseName = {clothes, tuition, transportation, food, housing, books};

Это мой код

public class Budget{

///////////////fields////////////////


int expenseAmount[] = {1000, 2000, 3000, 4000, 5000, 6000};
int monthNumber[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};


int clothes[]= {100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210};
int tuition[] = {200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200};
int transportation[]={100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210};
int food[]={80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80};
int housing[]={150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150};
int books[]= {200, 0, 0, 0, 0, 0, 0, 300, 0, 0, 0, 0};
int i=0; // this is arbitrary. Never hard code numbers unless that number is never going to change. in that case you make a variable and define it.
int month_num;
private String[][] expenseName = {clothes, tuition, transportation, food, housing, books};

/////////constructors///////////////
public Budget() {}

public Budget(int name) {this.expenseName[i][i] = name;}

public Budget(String name, int clothes, int tuition, int transportation, int food, int housing, int books)
{
this.expenseName[i] = name;
this.clothes[i] = clothes;
this.tuition[i] = tuition;
this.transportation[i] = transportation;
this.food[i] = food;
this.housing[i] = housing;
this.books[i] = books;
this.monthNumber[i] = month_num;
}


/////////////methods///////////
public String getexpenseName() {return expenseName[i][i];}

public int getclothes() {return clothes[i];}//change the I
public int gettuition() {return tuition[i];}
public int gettransporation() {return transportation[i];}
public int getfood() {return food[i];}
public int gethousing() {return housing[i];}
public int books() {return books[i];}
public int getmonthNumber() {return monthNumber[i];}//change the i

public void setExpenseName(String name)
{
this.expenseName[i][i] = name;
}

public boolean setexpenseAmount(int num)
{
if (this.expenseAmount[i] == 0)
{this.expenseAmount[i] = num;
return true;
}
else
return false;

Ответы [ 5 ]

2 голосов
/ 14 ноября 2011

Вам нужны кавычки вокруг ваших строк:

private String[] expenseName = {"clothes", "tuition", "etc"};

или вам нужно объявить это int [] []

private int[][] expenseName = {clothes, tuition};
0 голосов
/ 14 ноября 2011

Вы должны преобразовать int[] в String[], например, так:

public class ArrayIntToString {
public static void main(String[] args) {
    int[] numbers = { 1, 2, 3 };
    String[] numbersAsStrings = new String[numbers.length];
    int i = 0;
    for (Integer number : numbers) {
        numbersAsStrings[i++] = number.toString();
    }
}
}
0 голосов
/ 14 ноября 2011

В Java вы не можете использовать массив int как массив строк.

Это просто не сработает, как говорит ошибка.

Полагаю, вы должны быть знакомы с каким-то языком, где нет проверки типов, и вы можете делать все, что захотите. Но это Java.

0 голосов
/ 14 ноября 2011

Изменение

private String[][] expenseName = {clothes, tuition, transportation, food, housing, books};

до

private int[][] expenseName = {clothes, tuition, transportation, food, housing, books};
0 голосов
/ 14 ноября 2011

Как ясно показывает сообщение об ошибке , вы не можете поместить int[] в массив String[] s.

...