11861

Assign split string to parsed int

Question:

i'm getting this error and i can't quite figure out how to fix it.

<blockquote>

error: <br /> incompatible types <br />found : int <br />required: int[]

array[x] = Integer.parseInt(elements[0]);

</blockquote>

here is the code for my method. The file being used is a text file of 1000 numbers, 2 per line with 500 lines, separated by commas.

Example:1,2 <br />16,92 <br />109,7

the purpose of this block is to read all lines of the text file, and assign all numbers to the 2d integer array.

public static int[][] writeTypes(){ String position; String[] elements = new String[2]; int x; int y=1; int array[][] = new int[500][2]; File TypesFile = new File("Types.txt"); try { Scanner twoput = new Scanner(pkTypesFile); for(x = 0; twoput.hasNext(); x++){ position = twoput.nextLine(); elements = position.split(",", 2); array[x] = Integer.parseInt(elements[0]); array[x][y] = Integer.parseInt(elements[1]); System.out.println(array[x] + " " + array[x][y]); } } catch (Exception e) { System.err.format("Types File does not exist.\n"); } return array; }

Answer1:

You seem to be confused about indexing into multidimensional arrays. array is a 2-dimensional array, meaning it's an array of arrays. new int[500][2] creates an array of length 500, each element of which is an int[] of length 2, each element of which is an int. The expression array[x] selects one of the 500 int[] arrays of length 2. The compile error is saying you can't assign an int to an int[]. You need to provide another index to select one of the ints in the array denoted by array[x].

Specifically, you should change

array[x] = Integer.parseInt(elements[0]); array[x][y] = Integer.parseInt(elements[1]); System.out.println(array[x] + " " + array[x][y]);

to

array[x][0] = Integer.parseInt(elements[0]); array[x][1] = Integer.parseInt(elements[1]); System.out.println(array[x][0] + " " + array[x][1]);

The second index selects one of the ints in the int[], which you can then assign to.

Recommend

  • How do I store this JSON object as a cookie and than read it in vanilla javascript?
  • split string (e.g. with bash) but skip part of it
  • awk or sed CSV file manipulation
  • Regex: Replace every Comma with Tab Not within quotes
  • Is there any purpose for h2-h6 headings in HTML5?
  • Prolog Ambiguous Output
  • Mocha throws unexpected token error for ES6 object spread operator
  • Responsive left sidebar open close
  • Criterion causing memory consumption to explode, no CAFs in sight
  • Prevent page break in text block with iText, XMLWorker
  • C++ friend class std::vector
  • SyntaxError: (irb):26: both block arg and actual block given
  • Checking if an array in C is symmetric
  • Z3: Convert between FP and BitVector?
  • What's the purpose of QString?
  • Assign variable to the value in HTML
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • R - Combining Columns to String Based on Logical Match
  • Can I display google adwords (AdView) in javafx on android
  • Read text file and split every line in MSBuild
  • Counter field in MS Access, how to generate?
  • Validaiting emails with Net.Mail MailAddress
  • sending/ receiving email in Java
  • How can I use Kendo UI with Razor?
  • Redux, normalised entities and lodash merge
  • Convert array of 8 bytes to signed long in C++
  • Return words with double consecutive letters
  • how to add data labels for bar graph in matlab
  • Traverse Array and Display in markup
  • Cannot Parse HTML Data Using Android / JSOUP
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • SQL merge duplicate rows and join values that are different
  • JTable with a ScrollPane misbehaving
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • Why joiner is not used after Sequence generator or Update statergy
  • LevelDB C iterator
  • unknown Exception android
  • Can't mass-assign protected attributes when import data from csv file
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?