0

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[]

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;
1
  • 1
    Why do you declare the array as String if it is int? (scratching head) Commented Nov 14, 2011 at 2:14

5 Answers 5

1

You need quotes around your strings:

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

or you need to declare it int[][]

private int[][] expenseName = {clothes, tuition};
Sign up to request clarification or add additional context in comments.

1 Comment

I don't think that your first snippet is what the OP wants. But even if it is, you have a slight mistake: You are declaring an array of array of strings, but only providing an array of strings.
0

As the error message clearly states, you can't put an int[] into an array of String[]s.

Comments

0

Change

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

to

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

Comments

0

In Java, you cannot use an array of int as an array of 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.

Comments

0

You have to convert the int[] to String[], for example like this.

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();
    }
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.