Search String in file [duplicate] - java

This question already has answers here:
Creating a Java Program to Search a File for a Specific Word
(3 answers)
Closed 5 years ago.
I am trying to count the number of times a specific string appears into a file.
This is the code I am using:
while (scanner.hasNext()) {
String nextToken = scanner.next();
if (nextToken.equalsIgnoreCase(wordidnamee1))
count++;
}
This code only counts the number of time the string appears 'clean', but if it is attached to another word or followed by a colon it is not counted.
How can I solve this problem ?

Use contains()
while (scanner.hasNext()) {
String nextToken = scanner.next();
if (nextToken.contains(wordidnamee1))
count++;
}
For Non Case Sensitive match:
while (scanner.hasNext()) {
String nextToken = scanner.next();
if (nextToken.toLowerCase().contains(wordidnamee1.toLowerCase()))
count++;
}

Related

How to read only first expression of my file and move to another line [duplicate]

This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 3 years ago.
I have a file that looks like follows:
What I need is to read only names (first column) and output it.
while (sc.hasNext())
System.out.println(sc.next());
Another solution I tried is:
while (sc.hasNextLine())
System.out.println(sc.nextLine());
The ones that are above obviously don't work. I am stuck and don't know what to do. I was trying to google my problem but couldn't find the answer.
while(sc.hasNextLine()) {
StringBuilder toPrint = new StringBuilder();
String line = sc.nextLine();
for(int i = 0; i < line.length() && line.charAt(i) != ' '; i ++) {
toPrint.append(line.charAt(i));
}
System.out.println(toPrint.toString());
}
The reason I don't use String.split() is because that does lot's of unnecessary work, as it splits the entire string, and you only want the first word.

Java Equivalent to fscanf in C [duplicate]

This question already has answers here:
Is there an equivalent method to C's scanf in Java?
(7 answers)
what is the Java equivalent of sscanf for parsing values from a string using a known pattern?
(8 answers)
Closed 4 years ago.
Hi I i'm trying to do a line by line reading in of a file in Java. For example I'd like to be able to do something like
//c code
while( fscanf(ptr, "%s %s %s",string1,string2,string3) == 3)
printf("%s %s %s",string1,string2,string3);
Where I scan in every line in the file individually and store it in a variable.
I have started out by making a file object and finding the file. However I haven't found a function to be able to read input.
Use line oriented input, split the lines and print the contents. That is, read each line, check if you have three tokens (end if you don't) and print them. Like,
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] tokens = line.split(" ");
if (tokens.length != 3) {
break;
}
System.out.printf("%s %s %s%n", tokens[0], tokens[1], tokens[2]);
}
Change System.in to a File to read from a file.

how to modify string with scanner? [duplicate]

This question already has an answer here:
How to use java.util.Scanner to correctly read user input from System.in and act on it?
(1 answer)
Closed 5 years ago.
I already have a string let's say
String abc = "Stack";
Now, I want to modify this string using scanner class only ! So, user should to able to see existing string on console and then he can edit that string.
I tried this, but then it just replaces the existing string.
System.out.println("Edit the below string");
System.out.println(abc);
abc = scanner.nextLine();
How would I achieve it ?
Well the issue is you shouldn't with just a String. Your problem is the reason StringBuffer was created. Ideal solution:
StringBuffer buffer = new StringBuffer("Stack");
while (scanner.hasNextLine()){
buffer.append(scanner.nextLine());
System.out.println(buffer.toString());
}
String abc = buffer.toString(); //if you really need to save it to variable 'abc' for some reason.
Here is a terrible example if you hate your computer and can only user a String.
String abc = "Stack";
while (scanner.hasNextLine()){
abc += scanner.nextLine();
System.out.println(abc);
}
The above is a terrible solution for memory reason.
I hope this helps

Java .split() by character with spaces in string [duplicate]

This question already has answers here:
What's the difference between next() and nextLine() methods from Scanner class?
(15 answers)
Closed 5 years ago.
I am trying to read a line from a text file in java. I get a java.lang.ArrayIndexOutOfBoundsException: 1 exception.
Here is my code:
try {
Scanner kb = new Scanner(new FileReader(fileName));
String line = "";
int count = 0;
while (kb.hasNext()) {
line = kb.next();
String[] temp = line.split("#");
System.out.println(temp[1]);
Wedding tempWed = new Wedding(temp[0], temp[1], temp[2], temp[3], Integer.parseInt(temp[4]));
test[count] = tempWed;
count++;
}
} catch (FileNotFoundException ex) {
}
This is the line in the textfile:
Chiquita Sanford#Magee Sosa#2016-11-05#Garden#84
I need to split by the "#", and this partly works.
Java throws the exception when I try to access the element at position 1.
I think this is because there is a space between the first name and the surname, because when I System.out.println(temp[0]) it displays "Chiquita" and not "Chiquita Sanford".
Does Java have some restriction on splitting when there are multiple words in the first array index.
You have to use the nextLine method to read the full line. next will read until the first token ("Chiquita" in your case because its followed by a space character and is interpreted as a delimiter). So change this line:
line = kb.next();
with this:
line = kb.nextLine();
You are using kb.next() that will return the next word not the next line, for this use kb.nextLine() similar issue with kb.hasNext() needs to be kb.hasNextLine()

Scanner loop that is supposed to return each String, [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
I've got a problem with looping a scanner, wich should return EVERY string given as an input unless String != "end". Here is what I've done so far
private static String fetchString() {
Scanner scanner = new Scanner(System.in);
String stringElement = "";
System.out.println("Write a string");
while(scanner.hasNextLine()) {
stringElement = scanner.next();
if(stringElement == "end") {
break;
}
}
return stringElement;
}
result:
Write a string
abc
abc
abc
end
end
Loop, somehow, doesn't understand if(stringElement == "end"), it still wants new word. I can't get it. Where am I making a mistake?
First change, stringElement = scanner.next(); to stringElement = scanner.nextLine();, second change if(stringElement == "end") to if(stringElement.equals("end"))

Categories