Reputation: 3
So, brand new to this site, but looks promising..
Anyways, to business, I'm working on this bit of code and I'm trying to return a string using a scanner. I've also tried using a BufferedReader, but it still returns null.
public String scanFile(String lineStarter) throws IOException {
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) {
String[] lineInfo = sc.nextLine().toString().split("//|");
if(lineStarter.equalsIgnoreCase(lineInfo[0])){
return sc.toString();
}
}
//this is what keeps on returning
return null;
}
The text file is formated like this:
starwars|cool|Actor|general|google|low
starw|cool|Actor|general|google|low
hark|dude|this|I|don't|know
So ya, it keeps on returning the last line that I don't want to return...
I guess, if you really want it, I call the method while opening a new file I guess
private void doubleClicked(java.awt.event.MouseEvent evt) {
if(evt.getClickCount()==2){
try {
String s = (String) jList1.getSelectedValue();
System.out.println(s);
new DisplayMovie(scanFile(s)).setVisible(true);
} catch (IOException ex) {
System.out.println("An Error has occured");
}
}
}
I hope I explained it well enouph, any suggestions will hopefully help
Upvotes: 0
Views: 1458
Reputation: 208984
Your split is incorrect.
.split("//|");
The forward slash is not an escape. You need backslash
.split("\\|");
With the first code, it will not split correctly, therefore you will get no matches and your method will always return null unless you pass the entire line as a string to the method
Upvotes: 1