I am currently using StringTokennizer class to split a String into different token as by defined delimiter..
public class App {
public static void main(String[] args) {
String str = "This is String , split by StringTokenizer, created by Neera";
StringTokenizer st = new StringTokenizer(str);
System.out.println("---- Split by comma ',' ------");
StringTokenizer st2 = new StringTokenizer(str, ",");
while (st2.hasMoreElements()) {
System.out.println(st2.nextElement());
}
}
}
My query is that can same thing can also be achieved through scanner class also ...!! Is it the right approach to use the scanner class since I was reading The Scanner class allows you to tokenize data from within a loop, which allows you to stop whenever you want to... I have tried the following thing, but it doesn't work ...please advise me ..!!!
public class App1 {
public static void main(String[] args)
{
Scanner scanner = new Scanner("This is String , split by StringTokenizer, created by Neera").useDelimiter(", ");
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
}
}
Use scanner.next() to return the next token instead of scanner.nextLine(), which returns the next full line (and your input only has one). And of course you'll want to use hasNext() in your while instead of hasNextLine().
In response to your comment:
Your code has a syntax mistake, which I originally took for a typo and corrected in the question. You're writing:
while (scanner.hasNext()) ;
System.out.println(scanner.next());
Which properly formatted should tell you what's really happening:
while (scanner.hasNext())
; // empty statement
System.out.println(scanner.next());
It should be:
while (scanner.hasNext()) {
System.out.println(scanner.next());
}
Related
I would like to input a copied text from a text processor or others.
Using nextLine() just introduces the first line and it doesn't let me use StringBuffer too. I haven't found anything to solve my problem.
This is my code:
public static void main (String args[]) {
Scanner keyboard= new Scanner(System.in);
StringBuffer lines= new StringBuffer();
String line;
System.out.println("texto:");
line= keyboard.nextLine();
//lines= keyboard.nextLine(); //this doesn´t work
System.out.println(lines);
}
Here is an example of what I would like to do:
I copy this text from a text file:
ksjhbgkkg
sjdjjnsfj
sdfjfjjjk
Then, I paste it on the cmd (I use Geany).
I would like to be able to get a StringBuffer or similar (something I can manipulate) like this:
StringBuffer x = "ksjhbgkkgsjdjjnsfjsdfjfjjjk"
Thanks!
Try using something like:
while(keyboard.hasNextLine()) {
line = keyboard.nextLine();
}
You could then store those lines. (e.g. an array/ArrayList).
You can append the keyboard.nextLine() to your stringBuffer like so:
lines.append(keyboard.nextLine());
StringBuffer will accept a String to be appended so this should fit your purposes.
You could use this with the while loop as shown by #Cache which would give something like:
while (keyboard.hasNextLine()) {
lines.append(keyboard.nextLine());
}
#Cache Staheli has the right approach. To elaborate on how you can put the keyboard input into your StringBuffer, consider this:
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
StringBuffer lines= new StringBuffer();
String line;
System.out.println("texto:");
while(keyboard.hasNextLine() ) { // while there are more lines to read
line = keyboard.nextLine(); // read the next line
if(line.equals("")) { // if the user entered nothing (i.e. just pressed Enter)
break; // break out of the input loop
}
lines.append(line); // otherwise append the line to the StringBuffer
}
System.out.println(lines); // print the lines that were entered
keyboard.close(); // and close the Scanner
}
I'd like to read the "text8" corpus in Java and reformat some words. The problem is, in this 100MB corpus all words are on one line. So if I try to load it with BufferedReader and readLine, it takes away too much space at once and can't handle it to separate all the words in one list/array.
So my question: Is it possible in Java to read instead of line by line a corpus, to read it word by word? So for example because all words are on one line, to read for example 100 words per iteration?
you can try using Scanner and set the delimiter to whatever suits you:
Scanner input=new Scanner(myFile);
input.useDelimiter(" +"); //delimitor is one or more spaces
while(input.hasNext()){
System.out.println(input.next());
}
I would suggest you to use the "Character stream" with FileReader
Here is the example code from http://www.tutorialspoint.com/java/java_files_io.htm
import java.io.*;
public class CopyFile {
public static void main(String args[]) throws IOException
{
FileReader in = null;
FileWriter out = null;
try {
in = new FileReader("input.txt");
out = new FileWriter("output.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
}finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
It reads 16 bit Unicode characters. This way it doesnt matter if your text is in one whole line.
Since you're trying to search word by word, you can easy read till you stumble upon a space and there's your word.
Use the next method of java.util.Scanner
The next method finds and returns the next complete token from this scanner. A
complete token is preceded and followed by input that matches the
delimiter pattern. This method may block while waiting for input to
scan, even if a previous invocation of Scanner.hasNext returned true.
Example:
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
String a = sc.next();
String b = sc.next();
System.out.println("First Word: "+a);
System.out.println("Second Word: "+b);
sc.close();
}
Input :
Hello Stackoverflow
Output :
First Word: Hello
Second Word: Stackoverflow
In your case use Scanner for reading the file and then use scannerobject.next() method for reading each token(word)
try(FileInputStream fis = new FileInputStream("Example.docx")) {
ZipSecureFile.setMinInflateRatio(0.009);
XWPFDocument file = new XWPFDocument(OPCPackage.open(fis));
ext = new XWPFWordExtractor(file);
Scanner scanner = new Scanner(ext.getText());
while(scanner.hasNextLine()) {
String[] value = scanner.nextLine().split(" ");
for(String v:value) {
System.out.println(v);
}
}
}catch(Exception e) {
System.out.println(e);
}
I have a large text file which I want to separate into different strings using the delimiter !- (Each string is multiple lines).
I then want to discard all the other strings that do not contain:
=========== ALL OBJECTS IN CLASS: FENESTRATIONSURFACE:DETAILED ===========
So far I've got this and its not outputting anything (it complies but no output to console).I'm new to programming and I'm not making much progress after researching this for sometime so any suggestions or pointers would be most appreciated thanks!
public class Main {
public static void main(String[] args) throws FileNotFoundException {
File file = new File("C:/Users/Anton/Pictures/1 x geotransform0.5m shading.txt");
Scanner scan = new Scanner(file);
while(scan.hasNextLine()){
StringTokenizer st = new StringTokenizer(scan.nextLine(),"!-");
if(st.equals(" =========== ALL OBJECTS IN CLASS: FENESTRATIONSURFACE:DETAILED ===========")) {
System.out.print(st);
}
}
scan.close();
}
}
You should consider reading the java doc page to StringTokenizer: http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html
There two different problems in your code:
Because your desired strings are more than one line out of a file, you first have to add them up (into a String) and then work with it via StringTokenizer to separate again.
You have to compare the StringTokenizer.nextToken() to your check String not the whole StringTokenizer. StringTokenizer.nextToken() then gives you the next String separated through !-.
The following code should work:
public class Main {
public static void main(String[] args) throws FileNotFoundException {
File file = new File("C:/Users/Anton/Pictures/1 x geotransform0.5m shading.txt");
Scanner scan = new Scanner(file);
//Scanning
//first scan the whole file into a string (because a sting can have more than one line)
String temp = "";
while(scan.hasNextLine()){
temp = scan.nextLine();
}
//now add the string to tokeinzer
StringTokenizer st = new StringTokenizer(temp,"!-");
//now give output
while(st.hasMoreTokens()){
String temp2 = st.nextToken();
if(temp2.equals(" =========== ALL OBJECTS IN CLASS: FENESTRATIONSURFACE:DETAILED ===========")) {
System.out.print(temp2);
}
}
scan.close();
}
}
}
remove the if condition and add the below while condition
while (st.hasMoreElements()) {
System.out.println(st.nextElement());
}
because stringtokenizer splits the text based on token. so your if never becomes true.
try this...........
while(scan.hasNextLine()){
StringTokenizer st = new StringTokenizer(scan.nextLine(),"!-");
while(st.hasMoreTokens()){
String stt = st.nextElement().toString();
if(stt.equals("=========== ALL OBJECTS IN CLASS: FENESTRATIONSURFACE:DETAILED ===========")) {
System.out.print(stt);
}
}
}
you comparing string with stringtokenizer object not value....
im doing a test about findInLine object but its not working and i dont know why.
this is the code:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("enter string: ");
String a = null;
String pattern ="(,)";
if (input.findInLine(pattern) != null){
a = input.nextLine();
}
System.out.println(a);
enter string: (9,9) <---------- that is what i wrote
this is the output: 9)
what i need to do if i want that the variable a will get all the string that i wrote like this: a = (9,9) and not a = 9)
Whatever I understood. You want to input some string and if that string gets matches to your pattern you need that to be shown in console. This will give you correct output.
import java.util.Scanner;
public class InputScan {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String a;
System.out.print("enter string: ");
String pattern = "\\(\\d+,\\d+\\)"; // Its regex
// For white spaces as you commented use following regex
// String pattern = "\\([\\s+]?\\d+[\\s+]?,[\\s+]?\\d+[\\s+]?\\)";
if ((a = input.findInLine(pattern)) != null){
System.out.println(a);
}
}
}
Java Regex Tutorial
Scanner findInLine()
Input:
(9,9)
Output :
(9,9)
You need to escape your brackets in the regex. Now the regex matches the comma.
Moreover, you should realize that Scanner.findInLine() also advances on the input.
Try
String pattern = "\\([0-9]*,[0-9]*\\)";
String found = input.findInLine(pattern);
System.out.println(found);
to verify this.
I want to separate the elements of a text file into different arrays based of whether or not the line contains a question mark. Here is as far as I got.
Scanner inScan = new Scanner(System.in);
String file_name;
System.out.print("What is the full file path name?\n>>");
file_name = inScan.next();
Scanner fScan = new Scanner(new File(file_name));
ArrayList<String> Questions = new ArrayList();
ArrayList<String> Other = new ArrayList();
while (fScan.hasNextLine())
{
if(fScan.nextLine.indexOf("?"))
{
Questions.add(fScan.nextLine());
}
Other.add(fScan.nextLine());
}
Quite a few issues there
nextLine() actually returns the next line and moves on the scanner, so you'll need to read once instead
indexOf returns an int, not a boolean, I'm guessing you're more use to C++? You can use any of the following instead:
indexOf("?") >=0
contains("?")
matches("\?") etc.
please follow the java ways and use camelCase for vars...
Code
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(new File("foo.txt"));
List<String> questions = new ArrayList<String>();
List<String> other = new ArrayList<String>();
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.contains("?")) {
questions.add(line);
} else {
other.add(line);
}
}
System.out.println(questions);
System.out.println(other);
}
foo.txt
line without question mark
line with question mark?
another line