Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make a look-up table if a 2D array already has been given?
    primarykey
    data
    text
    <p>I have a 2D array which I name it as a look-up table as follows:</p> <pre><code>123 256 19 52369564875214 147 307 51 12654987625425 136 94 14 65913210210871 301 818 23 74120032154894 215 103 81 54680045569748 ... </code></pre> <p>... and so on (size of the array is 700x4)</p> <p>The aim of this work is to ask user to enter three numbers, say a, b, and c. This code should then look at the table (the array above) and return the corresponding value d, where d is extracted from the last column. For example if user enters (147,307,51), then the code should give the user 12654987625425. In fact as a function f(a,b,c)=f(147,307,51)=12654987625425.</p> <pre><code>package arrayread; import java.io.FileReader; import java.util.ArrayList; import java.util.Scanner; public class ArrayRead { // public static void main(String [] args) { ArrayRead ar = new ArrayRead(); ar.readArray("C:/Users/Desktop/tsp2.txt",","); String arr[][] = ar.getArray(); int rows = ar.getRows(); int cols = ar.getCols(); for (int r = 0; r &lt; rows; r++) { for (int c = 0; c &lt; cols; c++) { System.out.print(arr[r][c]+" "); } System.out.println(); } } private String arr[][]; private int rows, cols; String[][] getArray() { return arr; } int getRows() { return rows; } int getCols() { return cols; } // Read using DELIMITER TAB void readArray(String arrayName) { rows = 0; cols = 0; int r, c, numElements; String ipString, splitString[]; ArrayList &lt;String&gt;tmpArr = new ArrayList&lt;&gt;(); try { Scanner in = new Scanner(new FileReader(arrayName)); while(in.hasNext()) { ipString = in.nextLine(); splitString = ipString.split("\t"); if (rows==0) { cols = splitString.length; } for (c=0;c&lt;cols;c++) { tmpArr.add(splitString[c]); } rows++; } in.close(); arr = new String[rows][cols]; numElements = 0; for (r=0; r&lt;rows; r++) { for (c=0; c&lt;cols; c++) { arr[r][c] = tmpArr.get(numElements); numElements++; } } } catch (Exception e) { System.out.println("Error occurred!"); } } // Read using DELIMITER regex void readArray(String arrayName, String regex) { rows = 0; cols = 0; int r, c, numElements; String ipString, splitString[]; ArrayList &lt;String&gt;tmpArr = new ArrayList&lt;&gt;(); try { Scanner in = new Scanner(new FileReader(arrayName)); while(in.hasNext()) { ipString = in.nextLine(); splitString = ipString.split(regex); if (rows==0) { cols = splitString.length; } for (c=0;c&lt;cols;c++) { tmpArr.add(splitString[c]); } rows++; } in.close(); arr = new String[rows][cols]; numElements = 0; for (r=0; r&lt;rows; r++) { for (c=0; c&lt;cols; c++) { arr[r][c] = tmpArr.get(numElements); numElements++; } } } catch (Exception e) { System.out.println("Error occurred!"); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload