简体   繁体   English

创建int数组的字符串数组时出现问题

[英]Trouble with creating a string array of int arrays

I need to create an array of integers contained within an array of words. 我需要创建一个包含在单词数组中的整数数组。 I keep getting an error 我一直在收到错误

cannot convert from int[] to java.lang.String[] 无法从int[]转换为java.lang.String[]

where it says 在哪里说

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

This is my code 这是我的代码

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;

You need quotes around your strings: 你需要在你的字符串周围引用:

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

or you need to declare it int[][] 或者你需要声明它[] []

private int[][] expenseName = {clothes, tuition};

正如错误消息明确指出的那样,你不能将int[]放入String[]的数组中。

Change 更改

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

to

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

In Java, you cannot use an array of int as an array of String. 在Java中,您不能将int数组用作String数组。

That will just not work, like the error says. 这不会起作用,就像错误说的那样。

I guess that you must be familiar with some language where there is no type checking and you can do whatever you want. 我想你必须熟悉一些没有类型检查的语言,你可以随心所欲。 But this is Java. 但这是Java。

You have to convert the int[] to String[] , for example like this. 你必须将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();
    }
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM