Raito
Raito

Reputation: 3

How to change String to Integer in array

Array ticket[x][2] is String, and ticket[x][1] is String too. The array ticket[x][1] is {"1", "2", "3"}. How to fill ticket[x][2]? I want in integer. In my code is still error using Integer.parseInt().

String[][] ticket = new String[8][3];

for (int x = 0; x < limit; x++) {
    if(ticket[x][0].equals("festival")) {
        ticket[x][2] = Integer.parseInt(ticket[x][1]) * 250000;
    }
}

Upvotes: 0

Views: 51

Answers (2)

Saranga  kapilarathna
Saranga kapilarathna

Reputation: 644

 public class StringToIntegerArray {
       public static void main(String args[]) {
          String [] str = {"123", "345", "437", "894"};
          int size = str.length;
          int [] arr = new int [size];
          for(int i=0; i<size; i++) {
             arr[i] = Integer.parseInt(str[i]);
          }
          System.out.println(Arrays.toString(arr));
       }
    }

Result:

[123, 345, 437, 894]

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201409

Because ticket[x][2] is a String (you are trying to assign an int).

ticket[x][2] = "" + (Integer.parseInt(ticket[x][1]) * 250000);

or

ticket[x][2] = String.valueOf(Integer.parseInt(ticket[x][1]) * 250000);

Upvotes: 2

Related Questions