Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help! 2 dimensional array problem 1

Status
Not open for further replies.

KenWan

Programmer
Feb 27, 2004
14
GB
I don't know how to enter my values into a 2 dimensional array using for loops then getting it the values for 1 column into a string[] type.

I got as far as setting up 2 arrays
Code:
String[][] stringList2 = new String[list2.size()][list2.size()];
but I want to enter a String name in the first column and a string value in the second column. Then using JSP code do something like :
Code:
String[] years  = filemanager.getList("Years.List");
to get at the inputs fora drop list.

Main problem put the code into a 2 dim array, then getting it out on either column.
I have read the api for array, vector, enumeration and string but still don't know how to code it or why my code doesn't stand up. Please help.
 
try this :

Code:
String one = "one";
String two = "two";

String[][] arr = new String[5][2];

arr[0][0] = one;
arr[0][1] = two;

one = "three";
two = "four";

arr[1][0] = one;
arr[1][1] = two;

... and so on
 
im getting something out, but how do I get one column of String out?
 
is my code right?

Code:
public String[][] simpleFileReader(String filename)
  throws FileNotFoundException, IOException {
				
  BufferedReader inFile = new BufferedReader(
	new FileReader(pathSettings + filename) );

	Vector list2 = new Vector();
	Vector list3 = new Vector();
	String line2, iKey;
	int iValue =0;
	String iLine;
		
	while (inFile.ready()) {
	  line2 = inFile.readLine();
	  if (line2.length() != 0){
	    int end = line2.indexOf(';'); 
	    if (end < 0) {
	      int left1  = line2.indexOf('[');
	      int right1 = line2.indexOf(']',left1);
	      int left2  = line2.indexOf('[',right1);
	      int right2 = line2.indexOf(']',left2);
	      iLine = line2.substring(left2 + 1,right2);
iKey =  line2.substring(left1 + 1, right1);
	      list2.add(iKey);
	      list3.add(iLine);
	    }
	}
   }
   inFile.close();
   String[][] stringList2 = new String[list2.size()]
[list2.size()];
	int i = 0;
	int k = 0;
	for (Enumeration e = list2.elements();e.hasMoreElements() ; k++)  {
	stringList2[i][k] = (String) e.nextElement();	 
	for (Enumeration f = list3.elements(); f.hasMoreElements() ; i++){
	stringList2[i][k] = (String) f.nextElement();}	 
		}
	 System.err.println(stringList2[i][k]);
	return stringList2;
		
}
 
well i guess that completly wrong but i did manage to output an object for some reason :) now im stuck with no idea of how to get the key and value to stay together. Anyway thanks for your help Sedj :)
 
KenWan, I think you are over-complicating things here ...

Try the code below, I think you'll find it a little simpler :

input file example :

Code:
[aaa Key][aaa Value]
[bbb Key] [bbb Value]
[ccc Key][ccc Value]
[ddd Key] [ddd Value]

and the code :

Code:
import java.io.*;
import java.util.*;


public class Test {

	public static void main(String args[]) throws IOException {
		String [][] myArr = new Test().parseFile();
	}

	public String[][] parseFile() throws IOException {
		BufferedReader br = new BufferedReader(new FileReader("example.txt"));
		String line = "";
		ArrayList al = new ArrayList();

		// Loop the file
		while ((line = br.readLine()) != null) {
			System.err.println("Parsing line : " +line);
			StringTokenizer st = new StringTokenizer(line, "[]");

			String keyValue = "";
			// Not using SDK 1.4, need to use StringTokenizers to get rid of spaces
			while (st.hasMoreTokens()) {
				String part = st.nextToken();
				if (part.trim().length() != 0) {
					keyValue += part +"|";
				}
			}

			al.add(keyValue);

		}

		// And finally create out 2 dimensional array ...

		String[][] arr = new String[al.size()][2];
		for (int i = 0; i < al.size(); i++) {
			// Now break the key Value string into separate key and value
			StringTokenizer st = new StringTokenizer((String)al.get(i), "|");

			String key = st.nextToken();
			String value = st.nextToken();
			System.err.println("Getting : key(" +key +"),  value(" +value +")");

			arr[i][0] = key;
			arr[i][1] = value;
		}

		return arr;
	}

}
 
Whoo hoo I got it to store properly :) here's the code in the end.
Code:
  public String[][] simpleFileReader(String filename)
			throws FileNotFoundException, IOException {
				
		BufferedReader inFile = new BufferedReader(
									new FileReader(pathSettings + filename) );
		ArrayList al = new ArrayList();
		String line2;
		
		while ((line2 = inFile.readLine()) !=null) {
	      if (line2.length() != 0){   //Ignores blank spaces
		    int end = line2.indexOf(';');  //Ignores  ';' comments
			if (end < 0) {
			  System.err.println("Parsing line : " +line2);
			  StringTokenizer st = new StringTokenizer(line2, "[]");
			  String keyValue = "";
			  while (st.hasMoreTokens()) {
				String part = st.nextToken();
				if (part.trim().length() != 0) {
					keyValue += part +"|";
				}
			  }
			al.add(keyValue);
		   	}
		  }
		}
		
		inFile.close();
		
		String[][] stringList2 = new String[al.size()][2];
		
		// and add each item in the array list to the stringList[][]
		for (int i = 0; i < al.size(); i++) {
			
			StringTokenizer st = new StringTokenizer((String)al.get(i), "|");
			String iKey = st.nextToken();
			String iValue = st.nextToken();
			System.err.println("Getting : key(" +iKey +"),  value(" +iValue +")");
			stringList2[i][0] = iKey;
			stringList2[i][1] = iValue;
		}
		// now return the string[][] we have just built
		return stringList2;
	}
hmm you're right Sedj, I did over do it with the complication, sorry.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top