Question:
Need some help with this java code?
Tyman23
2014-01-22 16:09:16 UTC
The class should read lines from stdin (System.in) and print to stdout (System.out) any line that
contains the text given in the first command line argument. If no command line argument is
given, it should print every line. I'm trying to figure out how to compare the user's input(search term) to stdin. What am I doing wrong?



import java.util.Scanner;
import java.lang.String;

public class LineFilter {

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String newLine = keyboard.next();
if (keyboard == "") {
while (keyboard.hasNext()) {
System.out.println(System.in); }
} else {
while (keyboard.hasNext()) {
if (newLine.contains(System.in) {
System.out.println(keyboard.nextLine());
}
}
}
}
}
Three answers:
Leo D
2014-01-22 16:30:56 UTC
You said "any line that contains the text given in the first command line argument". The phrasing is a little confusing, but let's say that the run command looks like this



java LineFilter your



And the user writes:

Friends, Romans, Countrymen:

Lend me your ears.



Should this match the second line? Or will it only match a line that says "your". Should it the match be a word or will any literal word part do? Will "java LineFilter end" match both lines? What about case?



Okay, so here we go:

- If you're comparing that two strings have to be equal, you can use Object.equals.

- You can trim spacing by using String.trim.

- If you're searching that only a word part has to be in the string, use String.contains. indexOf will also work, but contains is more specific to that point.

- You can ignore case by using String.equalsIgnoreCase, but if you're working specifically with a word part, you will have to change both strings to lower case using String.toLowerCase.

- If the substring has to be a whole word, we are now moving into the world of regular expressions, you will have to surround the substring with "\b" which marks a word boundary.



I hope this helps :) Here are some APIs to help you.



With a little more information, I will be able to help you a lot more. Examples of what would count as matches would be greatly appreciated, thanks :)
Bob
2014-01-23 00:32:44 UTC
I think you'd find it a lot easier if you used the Scanner class to read from System.in and give you one line at a time in a String. Use hasNextLine() and nextLine().



I think your test for a command line argument should be:

if (newLine.contains (args[0])
James Bond
2014-01-23 00:25:57 UTC
import java.util.Scanner;

import java.lang.String;



public class LineFilter {



public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in);

String newLine = keyboard.next();

if (args[0] == "")

{

while (keyboard.hasNext()) {

System.out.println(keyboard.next()); }

}



else

{

while (keyboard.hasNext()) {

newLine=keyboard.next();

if (newLine.contains(args[0]) {

System.out.println(newLine);

}

}

}

}

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...