Read Next Word in Java

Read next word in java

You do not necessarily have to split the line because java.util.Scanner's default delimiter is whitespace.

You can just create a new Scanner object within your while statement.

    Scanner sc2 = null;
try {
sc2 = new Scanner(new File("translate.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (sc2.hasNextLine()) {
Scanner s2 = new Scanner(sc2.nextLine());
while (s2.hasNext()) {
String s = s2.next();
System.out.println(s);
}
}

Scanner.next() taking in more than 1 word in loop

You can use nextLine method of Scanner to read input as String and then later convert it like you want. You can do something like this.

import java.util.*;

class GFG {
public static void main (String[] args) {
int d = configureDifficulty();

System.out.println(d);
}

public static int configureDifficulty() {

String level = "1";
System.out.println("At what difficulty would you like to play at?");
System.out.println("Type 1 for easy, 2 for medium, 3 for hard.");

Scanner in = new Scanner(System.in);

while (true) {

level = in.nextLine();
try {
return Integer.parseInt(level);
}
catch (NumberFormatException ex) {
System.out.println("Try again");
}
}

}
}

How to read a single word (or line) from a text file Java?

To read lines from a text file, you can use this (uses try-with-resources):

String line;

try (
InputStream fis = new FileInputStream("the_file_name");
InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
BufferedReader br = new BufferedReader(isr);
) {
while ((line = br.readLine()) != null) {
// Do your thing with line
}
}

More compact, less-readable version of the same thing:

String line;

try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("the_file_name"), Charset.forName("UTF-8")))) {
while ((line = br.readLine()) != null) {
// Do your thing with line
}
}

To chunk a line into individual words, you can use String.split:

while ((line = br.readLine()) != null) {
String[] words = line.split(" ");
// Now you have a String array containing each word in the current line
}

list all next word of matching word in java

You're close. Instead of only checking m.find() once with a ternary if-statement, you should wrap it into a loop. Something like this:

Pattern p = Pattern.compile(matchWord + "\\W+(\\w+)");
Matcher m = p.matcher(str);
List<String> words = new ArrayList<>();
while(m.find()){
words.add(m.group(1));
}
return words;

Try it online.

Read words using scanner in Java

Reading input from the command line with the scanner can be done by doing the following

Scanner s = new Scanner(System.in);
System.out.print("Input name, age, address, city: ");
String input = s.next();

extracting the word next to a specific word in a text file using java

public class Searchright {
public static void main(String[] args) throws IOException {

Scanner s = null;
String str;

try {
s = new Scanner(new BufferedReader(new FileReader("doc.txt")));
do{
//while (s.hasNext()) {
str=s.next();
if((s.hasNext(("xxx"))||s.hasNext(("X.X.X”))))
{
//System.out.println(s.next()+" "+s.next() );
//System.out.println(s.next());
System.out.println(str);
//s.next();
}

//System.out.println(s.next());

}while(s.hasNext());
}

finally{
if (s != null) {
//s.close();
}
}
}
}

remove s.next() before do loop and before while block inside do loop.
It escape words two times in one loop.Thus causing missing word.

Read file by word, Scanner

I would do it like this assuming your code works

List<List<String>> temps = new ArrayList<>();
Scanner dataScanner = new Scanner(dataFile);

while (dataScanner.hasNextLine()) {
String[] data = dataScanner.nextLine().split(" ");
temps.add(new ArrayList<>(Arrays.asList(data[0],data[1]));
}

This takes the current line and splits it at a space character.
Afterwards it creates a list with the two elements and adds it to your temps list



Related Topics



Leave a reply



Submit