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
How to Cast an Object to an Int
Extract Source Code from .Jar File
How to Implement a Fsm - Finite State MAChine in Java
How to Use Concurrentlinkedqueue
Spring - Injecting a Dependency into a Servletcontextlistener
Tomcat - Maxthreads VS Maxconnections
Create Whole Path Automatically When Writing to a New File
Java Error: Cannot Make a Static Reference to the Non-Static Method
How Does the Jvm Ensure That System.Identityhashcode() Will Never Change
Write a Program That Will Surely Go into Deadlock
How to Convert Iterable to Collection
How to Pass Console Arguments to Application in Eclipse
The Most Sophisticated Way for Creating Comma-Separated Strings from a Collection/Array/List
How to Copy the Contents of One Arraylist into Another
Making a Log4J Console Appender Use Different Colors for Different Threads
Is 1/0 a Legal Java Expression