My problem is that when I try to gather the parts of an array, the part of my code that decides what to keep seems to be making a mistake, though I can't fathom how it is making that mistake. My code is posted below. You should see the parts of my code that I am talking about, but if you don't understand, please ask!

static void remove(String str) {
		String[] keep = new String[getEntries()];
		String[] removed = new String[getEntries()];
		// Go through the file, and only add the non-str ones to the keep array.
		String test = "none";
		int j = 0;// the spot in the array of keep. When something is NOT added,
					// i is advanced, while j is not.
		int k = 0;//removed's j.
		for (int i = 0; i < (getEntries() - 1)/*
											 * To convert to an array friendly
											 * form
											 */; i++) {
			test = getLine(i);
			if (!test.equals(str)) {// DO add
				keep[j] = test;
				test = "none";
				j++;
			} else {
				removed[k] = test;
				test = "none";
				k++;
				// j doesn't increment.
			}
		}
		// Write all of the keeps to file.
		for(int i=0;i<k;i++)
			System.out.println("Removed: "+removed[i]);
		System.out.println("End of removed.\n\n");
		System.out.println("Saved:");
		for (int i = 0; i < j; i++) {
			if (i == 0) {//Works
				PrintWriter pw = null;
				try {
					pw = new PrintWriter(new FileWriter(base
							+ "apps/install.txt"));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					pw.print(keep[0]);
				} catch (NullPointerException e) {
					e.printStackTrace();
					System.out
							.println("NullPointer. Probably happened because of the above try/catch statments");
				}
				continue;
			}
			add(keep[i]);// write them to the new file.
		}
		System.out.println("End of Saved.\n\n");
	}

This is my remove(String str) method. There is also an add method, which does the opposite to what this code does. Base, the variable, is a string, containing the location of the file that I want to write to.

My idea is to scan through a file, and take out the parts that match the parameter, str.

Thanks in advance,
Jack.

Recommended Answers

All 2 Replies

can you show us an example where your code doesn't work as you thinked?
I didn't see any error in this segment of code.

part of my code that decides what to keep seems to be making a mistake

Have you tried debugging your code?
If you do not have an interactive debugger, add lots of printlns to your code to show the values of variables as they change and are used and to show the execution flow.

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.