Java Substring match is failing - java

I am using a text file to read values from and loading this file into a buffered reader. thereafter i am reading the file line by line and checking if any of line contains one of my keywords (i already have them in a list of String).
However, even though the line contains the keyword i am looking for it does not detects it and gives it a Miss. Here is the code
for(int i=0;i<sortedKeywordList.size();i++)
{
String tempString=sortedKeywordList.get(i);
while(US.readLine()!=null)
{
String str=US.readLine();
//System.out.println(str);
if(str.contains(tempString)){
System.out.println("Contains: "+tempString);
}
else{
System.out.println("Miss");
}
}
}

For each keyword, you're iterating through your buffer using readLine(). So after your first keyword, you'll have exhausted your buffer reading and the next keyword test won't even execute since US.readLine() is giving you null. You're not re-initialising your reader.
So why not iterate through your file once (using your readLine() structure), and then for each line iterate through your keywords ?
EDIT: As Hunter as pointed out (above) you're also calling readLine() twice per loop. Once in your loop test and once to check each line for a keyword. I would first of all ensure you're reading the file correctly (simply by printing out each line as you read it)

You're calling US.readLine() twice!
Try instead:
String tempString;
String str;
for(...)
{
tempString = sortedKeywordList.get(i);
while((str = US.readLine()) != null)
{
...
}
}

You are calling US.readLine() once in the while loop entrance and again inside. This moves the input to the next line. Also, compare strings with .equals() and to check for substrings using .contains()

I'm seeing two major problems.
You've got your loops backwards.
The way you've written it, it looks at keyword1, and then looks through the whole input, checking for keyword1. Now, there's no more input, and it moves to keyword2, but there's no input left for it to check, so it quickly iterates through the rest of your keywords and quits.
You want to loop through the input, checking for each keyword, not through the keywords, checking each line of input.
while(input){
for each keyword {
...
You're calling .readLine() twice for each iteration, effectively skipping every other line.
Try storing the first line outside of the loop, checking for null in your loop condition, and then calling readLine juuust before the end of your loop.

The dataset in question would be helpful. Without it, a couple thoughts -
Verify the case of the sorted keywords matches case from the text file. If they are mismatched and you need to support case-insensitive matching, convert both strings to the same case (e.g., use toUpperCase()) then use the contains() call.
Verify no extra characters (like linefeeds/etc) appended the end of the sorted keyword.

Related

Explanation regarding skipping of alternate lines while reading from a file?

I just want to know, have I interpreted the code in right manner?
The file Bharath.txt has the following content:
Hello ! B . We created your file succesfully . We have updated you file , Sir ! Bharath Nikhil Friends Indeed
Consider the below code:
import java.util.Scanner;
import java.io.*;
class FilInpStr
{
public static void main(String arg[]) throws Exception
{
FileInputStream obj=new FileInputStream("Bharath.txt");
Scanner input=new Scanner(obj);
String n="";
while((n=input.nextLine())!=null)
{
System.out.println(n);
}
}
}
The above code works well this way. But when I do not consider storing input.nextLine() in a String, it begins to skip line alternatively. Is it that it is taking even the Enter from keyboard as input?
So, when I press Enter, it is passing the escape sequence too, as an input, which is why it is skipping one and printing the other?
And another doubt was regarding the while loop: is that one line wasted while comparing whether it is null or not?
Bharath
Nikhil
Friends Indeed
So, when I quote:
while((input.nextLine()) != null)
{
System.out.println(input.nextLine());
}
It will check whether Bharath==null or not, and if not, it will only then print Nikhil? And post Nikhil, it will skip next line, because it takes Enter as an escape sequence.
Please, correct me! I might have mixed up two different approaches. A beginner hence.
Also, do mention in distinction the difference between passing the input.nextLine() into a String, and as standalone code.
But , when I do not consider storing " input.nextLine()" in a String , it begins to skip line alternatively . Is it , that , it is taking even the ENTER from keyboard as input ?
Well no, it's just reading the next line every time you call input.nextLine(). That shouldn't be surprising. Ignore escape sequences etc. For the sake of simplicity, assume that input.nextLine() is just reading from a list of strings somewhere... it has a cursor showing where on the list it is. Every time you call input.nextLine(), it returns the current line and moves the cursor down.
If you call input.nextLine() twice in each iteration (once in the while condition and once inside the body) then you'll only execute half as many iterations as if you call it once per iteration (as per your original code).
input.nextLine()
Does two things :
Return the value of the current line
Advance the cursor past the current line
Scanner.nextLine()
Advances this scanner past the current line and returns the input that
was skipped.
So, if you don't store the result of input.nextLine(), it will be lost.
Each call to input.nextLine() advances past the current line, so each call will read a different part of the input.
The issue in the second snippet isn't the storing, but the fact you call nextLine() twice in each iteration. First, you call it in the while loop - the line is read and is then ignored (i.e., not saved anywhere), but the internal cursor of the input stream is advanced to the next line. Then, in the println line, you read another line and return it to System.out.println, effectively having all the odd-numbered lines just ignored.

How to remove first string and comma from a text file

!
I have a text, the content looks like [1,'I am java, and I am happy, I am.....'], I want to remove the first integer and the comma. When I was run the code above, but the result start with last comma: I am......
If you only want to remove commas from a String, you can use String.replaceAll(",",""); If you want to replace them by spaces, use String.replaceAll(","," "):
while ((line = br.readLine()) != null) {
contents.append(line.replaceAll(","," ");
}
Also in your code you seem to split the input, but don't use the result of this operation.
You need to use the indexOfReturns the index within this string of the first occurrence of the specified character, starting the search at the specified index..
lastIndexOf Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index.
System.out.print(s.substring(s.indexOf(",")+1));
Use this following code as:
System.out.println(line.substring(2));
sub string takes the beginning index as a parameter and splits the string from that index to till the end.
Note that you are using lastIndexOf(). Use indexOf() to get the first index as shown below.
System.out.println(test.substring(line.indexOf(',')+1));
I'm taking your String literially, but you could use String#replaceFirst, for example...
String text = "[1,'I am java, and I am happy, I am.....']";
text = text.replaceFirst("\\[\\d,", "[");
System.out.println(text);
Which outputs...
['I am java, and I am happy, I am.....']
If you want to update the file, you are either going to have to read all the lines into some kind of List (modifying them as you please) and once finished, write the List back to the file (after you've closed it after reading it).
Alternatively, you could write each updated line to a second file, once you're finished, close both files, delete the first and rename the second back in it's place...
Try This code:
String[] s=line.splite(",");
String m="";
for(int i=1;i<s.length;i++)
{
String m=m+s[i];
}
br.append(m);
String input = "[1,'I am java, and I am happy, I am.....']";
//Getting String after first comma
String output = StringUtils.substringAfter(input, ",");
System.out.println("Output:"+output);
//replacing commas;
System.out.println("Final o/p:"+StringUtils.replace(output, ",",""));
You can use methods in StringUtils Class for string manipulations. For using StringUtils methods, you need to import apache-commons-lang.jar file. Using this API you can manipulate many String related methods. For more details, you can see the link
http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html

How to keep spaces where they were after modifying string

So I have to get words from a text file, change them, and put them into a new text file.
The problem I'm having is, lets say the first line of the file is
hello my name is bob
the modified result should be:
ellohay myay amenay isay bobay
but instead, the result ends up being
ellomynameisbobhay
so scanner has .nextLine() but I want to have a method that is .nextWord() or something, so that it will recognize something as a word until it has a space after it. how can I create this?
nextLine() gives you the whole line.
What you should use is just next(), that will give you the next word.
Also see String.split() or StringTokenizer if you wanted to post-process whole lines. It sound s as though in your situation just using the scanner is fine, but I though i'd mention it because I assumed you'd have just used those methods if you knew about them.

Java Scanner - How to return to the last word after using next()

I currently have a If statement that checks to see if the next word in a document equals a certain length. After it returns true, I want it to add that word to an array or string etc. My problem is once I use the next() command, it proceeds to the next value in the document even though I used it in the condition of the If statement. I am somewhat new to Java so I hope this simple click makes sense.
Scanner in1 = new Scanner(inputFile);
String inputString;
while(in1.hasNextLine()){
if(in1.next().length() == 6){
inputString = in1.next();
}
}
Basically I have a file that contains several words, all different lengths on multiple lines. I want to catch the word that has 6 characters in it. The actual while loop and file is more complex, the problem is that it jumps to the next word after it evaluates the If statement. So if it detects that the next word has 6 characters, it loads the following word that comes after it into the variable. Any help or advice is welcome!
You can split this loop up into two pieces:
Get the next token and store it, and
Use that cached token.
For example:
while(in1.hasNextLine()) {
String current = in1.next();
if(current.length() == 6) {
inputString = current;
}
}
This is a standard technique when using iterators or scanners for precisely the reason you've noted - the next() method can't be called multiple times without producing different values.
As an aside, other programming languages have designed their iterators so that getting the value being iterated over and advancing to the next location are separate steps. Notably, the C++ STL considers these to be different operations.
Hope this helps!
Capture the value of next() into a variable, do the compare, set inputString to that variable. If the length isn't correct, whatever comes next can use the same variable, avoiding additional the extra next(). Not sure if this does what you need it to.

BufferedReader problem in Java

me and my buddy are working on a program for our Object Oriented Programming course at college. We are trying to write text into a file as a database for information. The problem is that when we try to read the corresponding lines with BufferedReader we can't seem to figure out how to read the correct lines. The only functions avaliable seem to be read(), which reads a character only. readLine() reads only a line(not the line we want it to read. skip() only skips a number of characters specified. Anyone have an idea how we could tell the program what line we want to read? Our method getAnswer() with the argument int rowNumber is the one we are trying to do:
Superclass: http://pastebin.com/d2d9ac07f
Subclass is irrelevant(mostly because we haven't written it yet).
Of course it is Java we are working with.
Thanks beforehand.
You will have to use readLine(), do this in a loop, count the number of lines you've already read until you've reached the line number that you want to process.
There is no method in BufferedReader or other standard library class that will read line number N for you automatically.
Use the Buffered Readers .readLine(); method until you get to the data you need. Throw away everything you don't and then store the data you do need. Granted this isn't effiecent it should get your job done.
readLine() in Java simply reads from the buffer until it comes upon a newline character, so there would really be no way for you to specify which line should be read from a file because there is no way for Java to know exactly how long each line is.
This reason is also why it's difficult to use skip() to jump to a particular line.
It might be better for you to loop through lines using readLine(), then when your counter is where you'd like it to be, begin processing.
String line = myBufferedReader.readLine();
for(int i = 1; i < whichLine && line != null; i++){
line = myBufferedReader.readLine();
}
/* do something */

Categories