0

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

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

Comments

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

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.