Hi everyone im a bit confused with my work and i hope you can help me. i need to get some values from a text file and enter it into a 2D array ive done this part ok with the following code however in my file i have some other text which is just normal string and i dont want this to be in be in the array so before the numbers ive done [Set 1] and at the end of the numbers [End Set 1] is it possible to for me to get the text inbetween these two points. everytime i try something it seems to mess up the return map part can any one help me please.

public static int[][] Map(File f) throws IOException {
		
	ArrayList line = new ArrayList();
	BufferedReader br = new BufferedReader(new FileReader(f));
	String s = null;
	while ((s = br.readLine()) != null)
	line.add(s);
                    int[][] map = new int[line.size()][];
	for (int i = 0; i < map.length; i++) {
	s = (String) line.get(i);
	StringTokenizer st = new StringTokenizer(s, " ");
	int[] arr = new int[st.countTokens()];
	for (int j = 0; j < arr.length; j++)
	arr[j] = Integer.parseInt(st.nextToken());
	map[i] = arr;
	}
	return map;
	}

Here's what I understand from your post.
- You have a text file with some string data and some numeric data.
- The string data all comes first before the numbers
- The tag [Set 1] indicates the beginning of the numbers
- The tag [End Set 1] comes after the numbers
- You are only interested in reading the numbers between [Set 1] and [End Set 1], the rest of the text in the file can be ignored.

If I've gotten it right, I propose the following solution.
On line 6 your are reading lines from the file. On line 7 you are storing each line in an array list. This loop can be expanded so that you only store in your array list the lines containing numbers.

Create a boolean variable, say call it numberSection, and initialize it to false before the while loop on line 6. Put an open curly brace at the end of line 6 so you can have more stuff inside the loop.

Before adding s to the line array list, check if (s.equals("[Set 1]")) . If so, set numberSection to true. Next, check else if (s.equals("[End Set 1]")) . If so, put a break statement to get out of the loop. Finally, put else if (numberSection) and if so, put your code to add s to the line array list.

commented: Thanks for your help :D +3

Hey kramerd, you are a legend every time i tried adding if statement's i kept getting errors because it kept interfering with the return value Thank you very much for the help :D

Just another quickie is there any way i can look for different sets of numbers e.g. if theres set 1 and theres set 2 can i choose to have set 2 instead of set 1.
just to make it clear i have [set 1]. then [set string 1] which then has a set of strings then [set 2] and then [set string 2] etc etc. if i try having set two i get an error because of the set of strings is there anyway i can just skip over the sets of strings and only look at the [Set 1,2,3,4 etc]. sorry if that doesnt make sense.

Yes, there is a way to do it. Usually there are a few different ways to accomplish what you want. But I'm not sure I understand what you are asking.

can i choose to have set 2 instead of set 1.

Do you mean one time when you run the program you will want set 1, and another time you will want set 2? Or do you mean you need to read numbers from both sets 1 & 2 while skipping over the strings in between?

well one time i run the program i may want set one, another time i may want set 4 etc

OK. Then you can pass either the set number (1, 4, etc.) or the string ("[Set 1]", "[Set 4]"), etc. as a command line argument into your program to determine which part of the file to skip.

i was thinking of doing that but that isnt my problem. my text file currently has:

[Set 1]
0000
0000
0000
0000
[End Set 1]

[Set String 1]
"" "" "" ""
"" "" "" ""
"" "" "" ""
"" "" "" ""
[End String 1]

[Set 1]
1000
0200
0030
0004
[End Set 1]

etc

when i read the file it gets the first set fine. but as soon as it sees the Set string set it gives me an error even though i changed the code so it was looking fro Set 2. i ran the debugger and even tho i tell it not to start till it find set 2 it just does here is my current code.

public static int[][] getPuzzle(File f) throws IOException {

		ArrayList<String> line = new ArrayList<String>();

		BufferedReader br = new BufferedReader(new FileReader(f));
		boolean ReadNum = false;
		String s = null;

		while ((s = br.readLine()) != null)
			if (s.equals("[Set Puzzle 1]")) {
				ReadNum = true;
			} else if (s.equals("[End Puzzle 1]")) {
				br.close();
				break;
			} else if (ReadNum = true)
				line.add(s);
		int[][] map = new int[line.size()][];
		for (int i = 0; i < map.length; i++) {
			s = (String) line.get(i);
			StringTokenizer st = new StringTokenizer(s, " ");
			int[] arr = new int[st.countTokens()];
			for (int j = 0; j < arr.length; j++)
				arr[j] = Integer.parseInt(st.nextToken());
				map[i] = arr;
		}	
		return map;
	}

All help is really apperciated

Does the second set of numbers really start with [Set 1] or is it [Set 2]? What error do you get when it hits the string section?

I just found a subtle error on line 15: change = to ==.

it was called set 2 but line 15 was the issue lol once i changed that it was ok this is what happens when you dont have a break thanks alot kramerd you are a life saver

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.