Re: trouble with FILE I/O
Here's the deal: posting elebenty gazillion lines of unformatted code and saying "it doesn't work" is going to get you zero replies. When posting code place [ code ] before and [ /code ] after (without the spaces) to preserve formatting. Post full error messages. Ask a specific question. It would help us if you could debug your code as much as possible to track down where the problem occurs. That way we don't have to do it. After all it is your code, the onus is upon you to fix it not us.
Re: trouble with FILE I/O
If your file I/O code works, comment it out for testing the censor method.
Manually load the Vectors with some data and just execute the censor method.
Your console I/O is not needed for the testing of the censor method and is not something I want to take time to do.
Make the test code so that it executes without any user intervention and shows the problem.
Re: trouble with FILE I/O
Sorry about the code up top.
Here is where the issue is, I'm trying to replace a word in a vector (output) with a string of asterisks of the same length IF the word in the vector output matches one of the words in the vector censor.
Here is the method I'm using:
Code:
public void censor(File textFile)
{
String aster = "";
String c1 = censor.toString();
String o1 = output.toString();
Scanner sc1 = new Scanner(c1);
Scanner so1 = new Scanner(o1);
while(sc1.hasNext())
{
String delWord = sc1.next();
while(so1.hasNext())
{
String word = so1.next();
int z = word.length();
int x = output.indexOf(word);
for(int i = 0; i<z; i++)
{
aster += "*";
}
System.out.print(aster);
output.set(x, aster);
}
}
}
Re: trouble with FILE I/O
Can you make a simple program that compiles and executes to show your problem?
Have the program completely self-contained and not requiring the reading of files.
You can use a Scanner constructor with Strings for input vs a file:
Scanner scnr = new Scanner("Line 1\nLine 2\n...<more lines as needed here>");
Re: trouble with FILE I/O
Alarm bells should ring whenever a parameter is never used in a method.
Re: trouble with FILE I/O
More alarm bells ring when a method/class/variable have the same name.
Re: trouble with FILE I/O
Alright I cut down the code to just show exactly what's happening and where I need some assistance.
I take a word from the censor vector and I try and find that word in the output vector, if there is a match I need to get the length of the word and create a string of asterisks the same length of the word. Once that loop finishes I replace the word in the output vector and that's where the thing crashes with an out of bounds exception.
java.lang.ArrayIndexOutOfBoundsException: -1 (line 46)
Code:
import java.util.*;
import java.io.*;
public class CensorApp
{
private String cmdLine;
private String cmd;
private Vector<String> output, censor;
public CensorApp() throws FileNotFoundException
{
output = new Vector<String>();
censor = new Vector<String>();
Scanner input = new Scanner(System.in);
censor.add("baa");
censor.add("any");
censor.add("three");
System.out.println(censor.toString());
output.add("baa baa black sheep, \n");
output.add("have you any wool, \n");
output.add("yes sir, yes sir, three bags full.");
System.out.println(output.toString());
String aster = "";
String c1 = censor.toString();
String o1 = output.toString();
Scanner sc1 = new Scanner(c1);
Scanner so1 = new Scanner(o1);
while(sc1.hasNext())
{
String delWord = sc1.next();
while(so1.hasNext())
{
String word = so1.next();
int z = word.length();
int x = output.indexOf(word);
for(int i = 0; i<z; i++)
{
aster += "*";
}
System.out.print(aster);
output.set(x, aster);
}
}
System.out.println(output.toString());
}
public static void main(String[] args) throws FileNotFoundException
{
CensorApp app = new CensorApp();
}
}
Re: trouble with FILE I/O
What is the value of x when statement 46 is executed? Add a println to show it.
How did it get that value?
Re: trouble with FILE I/O
Hmmm, I just saw it's x that's out of bounds. I don't know how it got that way, but I'll start the process right now.
Thanks for the heads up Norm.
Re: trouble with FILE I/O
Add more printlns to show the values of the variables you are working with.
Add id Strings to the printlns so you can easily see where it was printed:
Code:
System.out.println("censor=" + censor.toString());
Re: trouble with FILE I/O
I went through it with all the printlns real quick, but it's showing me that output.indexOf method for any of the words in the vector returns -1.
I'm pretty sure I'm using the indexOf method syntactically correct so I don't see why it would return -1 for a word that's in the vector.
Re: trouble with FILE I/O
Did you print out the values of ALL the variables you were using, not just x?
You need to SEE the values that the program is working with.
Re: trouble with FILE I/O
I posted printlns for all variables at the time they are called and each previous one every time a new variable is called.
It returned
Censor: [baa, any, three]
Output: [baa baa black sheep,
, have you any wool,
, yes sir, yes sir, three bags full.]
Aster :
cl: [baa, any, three]
o1 :[baa baa black sheep,
, have you any wool,
, yes sir, yes sir, three bags full.]
Aster
z: 4
cl :[baa, any, three]
o1 :[baa baa black sheep,
, have you any wool,
, yes sir, yes sir, three bags full.]
word :[baa
delWord :[baa,
Aster: ****
x: -1
z: 4
cl: [baa, any, three]
o1 : [baa baa black sheep,
, have you any wool,
, yes sir, yes sir, three bags full.]
word :[baa
delWord: [baa,
Would the delWord be screwing up because of the comma? If not, than it's definitely the indexOf method for output. It starts out as -1. All the other variables are working correctly so far as I can tell.
Re: trouble with FILE I/O
wait, the bracket is part of the baa and it's returning as part of the length. So there would be no index of that word because it's not part of the vector... But how do I remove it?
Re: trouble with FILE I/O
Quote:
Would the delWord be screwing up
I don't see where delWord is used in the code you posted. You can comment it out and not change what your code does.
What about the value of word? Does that look correct?
Re: trouble with FILE I/O
You are using the formatted String returned by the Vector class's toString() method.
You could use String's substring method to strip off the []s
Re: trouble with FILE I/O
I trimmed up the words, but the x is still returning as -1. Here's what I've changed of my code up above.
Code:
while(sc1.hasNext())
{
String delWord = sc1.next();
while(so1.hasNext())
{
String word = so1.next();
int z = word.length();
int windex = word.indexOf("[");
int dindex = delWord.indexOf("[");
word = word.substring(windex+1);
int d2index = delWord.indexOf(",");
delWord = delWord.substring(dindex+1, d2index);
System.out.println(d2index);
System.out.println(dindex);
System.out.println(delWord);
System.out.println(word);
if(word.equals(delWord))
{
int x = output.indexOf(word);
System.out.println(x);
for(int i = 0; i<z; i++)
{
aster += "*";
}
output.set(x, aster);
}
}
}
System.out.println(output.toString());
}
Re: trouble with FILE I/O
What does your debug print out show?
Re: trouble with FILE I/O