So I am trying to parse text files that could be in the form
words words words werdz words
or
words
words
words
words
words
Because of this, I decided to use Scanner instead of BufferedReader, and I'm not very experienced with using Scanner. I am trying to read a file and save to a Collection. Here is the main code and the supplementary methods.
main:
public static void main(String[] args) throws IOException {
LinkedList<String> unmodDict = readDict(new File(args[0]));
String[] unsorted = readUnsorted(new File(args[1]));
...
}
readDict()
public static LinkedList<String> readDict(File file) throws IOException {
//BufferedReader reader = new BufferedReader(new FileReader(file));
Scanner s = new Scanner(new FileReader(file));
String line = null;
LinkedList<String> ll = new LinkedList<>();
int count = 0;
while(s.hasNext()) {
ll.add(s.next());
}
// this loop seems to run finitely.
s.close();
return ll;
}
readUnsorted()
public static String[] readUnsorted(File file) throws IOException {
//BufferedReader reader = new BufferedReader(new FileReader(file));
Scanner reader = new Scanner(new FileReader(file));
int count = 0;
while (reader.hasNext()) {
count++;
}
reader.close();
// The above loop is running infinitely.
Scanner reader2 = new Scanner(new FileReader(file));
String line2 = null;
String[] unsortedWerdz = new String[count];
int i = 0;
while (reader2.hasNext()) {
unsortedWerdz[i] = reader2.next();
i++;
}
reader2.close();
return unsortedWerdz;
}
For some reason, the first while loop in the readUnsorted method is running infinitely but I can't see why since the first loop in the readDict method seems to run fine. Could someone advise me on what to do?
Thanks
It run's forever, since you check if there's a next String avaiable, but you don't retrieve it!
You need to get it via next() like this:
while (reader.hasNext()) {
reader.next();
count++;
}
Otherwise the Scanner will always point at the same (the first) element it reads and always report: Yes, there's another token in here!
Related
I'm just trying to do an exercise where I have to read a particular file called test.txt in the following format:
Sampletest 4
What I want to do is that I want to store the text part in one variable and the number in another. I am still a beginner so I had to google quite a bit to find something that would at-least work, here what I got so far.
public static void main(String[] args) throws Exception{
try {
FileReader fr = new FileReader("test.txt");
BufferedReader br = new BufferedReader(fr);
String str;
while((str = br.readLine()) != null) {
System.out.println(str);
}
br.close();
} catch(IOException e) {
System.out.println("File not found");
}
Use a Scanner, which makes reading your file way easier than DIY code:
try (Scanner scanner = new Scanner(new FileInputStream("test.txt"));) {
while(scanner.hasNextLine()) {
String name = scanner.next();
int number = scanner.nextInt();
scanner.nextLine(); // clears newlines from the buffer
System.out.println(str + " and " + number);
}
} catch(IOException e) {
System.out.println("File not found");
}
Note the use of the try-with-resources syntax, which closes the scanner automatically when the try is exited, usable because Scanner implements Closeable.
You just need:
String[] parts = str.split(" ");
And parts[0] is the text (sampletest)
And parts[1] is the number 4
It seems like you are reading the whole file content (from test.txt file) line by line, so you need two separate List objects to store the numeric and non-numeric lines as shown below:
String str;
List<Integer> numericValues = new ArrayList<>();//stores numeric lines
List<String> nonNumericValues = new ArrayList<>();//stores non-numeric lines
while((str = br.readLine()) != null) {
if(str.matches("\\d+")) {//check line is numeric
numericValues.add(str);//store to numericList
} else {
nonNumericValues.add(str);//store to nonNumericValues List
}
}
If you are sure the format is always for each line in the file.
String str;
List<Integer> intvalues = new ArrayList<Integer>();
List<String> charvalues = new ArrayList<String>();
try{
BufferedReader br = new BufferedReader(new FileReader("test.txt"));
while((str = br.readLine()) != null) {
String[] parts = str.split(" ");
charvalues.add(parts[0]);
intvalues.add(new Integer(parts[0]));
}
}catch(IOException ioer) {
ioer.printStackTrace();
}
You can use java utilities Files#lines()
Then you can do something like this. Use String#split() to parse each line with a regular expression, in this example i use a comma.
public static void main(String[] args) throws IOException {
try (Stream<String> lines = Files.lines(Paths.get("yourPath"))) {
lines.map(Representation::new).forEach(System.out::println);
}
}
static class Representation{
final String stringPart;
final Integer intPart;
Representation(String line){
String[] splitted = line.split(",");
this.stringPart = splitted[0];
this.intPart = Integer.parseInt(splitted[1]);
}
}
I am working in creating inverted index for list of words in java. Basically it creates a list for each word contains the document index that word appear on associated with frequency of word in that document, the desired output should be like this:
[word1:[FileNo:frequency],[FileNo:frequency],[FileNo:frequency],word2:[FileNo:frequency],[FileNo:frequency]...etc]
Here is the code:
package assigenment2;
import java.io.*;
import java.util.*;
public class invertedIndex {
public static Map<String, Map<Integer,Integer>> wordTodocumentMap;
public static BufferedReader buffer;
public static BufferedReader br;
public static BufferedReader reader;
public static List<String> files = new ArrayList<String>();
public static List<String>[] tokens;
public static void main(String[] args) throws IOException {
//read the token file and store the token in list
String tokensPath="/Users/Manal/Documents/workspace/Information Retrieval/tokens.txt";
int k=0;
String[] tokens = new String[8500];
String sCurrentLine;
try
{
FileReader fr=new FileReader(tokensPath);
BufferedReader br= new BufferedReader(fr);
while ((sCurrentLine = br.readLine()) != null)
{
tokens[k]=sCurrentLine;
k++;
}
System.out.println("the number of token are:"+k+" words");
br.close();
}
catch(Exception ex)
{System.out.println(ex);}
Until there it works correctly, I believe that the problem is in the manipulating the nested map in the following part:
TreeMap<Integer,Integer> documentToCount = new TreeMap<Integer,Integer>();
//read files
System.out.print("Enter the path of files you want to process:\n");
Scanner InputPath = new Scanner(System.in);
String cranfield = InputPath.nextLine();
File cranfieldFiles = new File(cranfield);
for (File file: cranfieldFiles.listFiles())
{
int fileno = files.indexOf(file.getPath());
if (fileno == -1) //the current file isn't in the files list \
{
files.add(file.getPath());// add file to the files list
fileno = files.size() - 1;//the index of file will start from 0 to size-1
}
int frequency = 0;
BufferedReader reader = new BufferedReader(new FileReader(file));
for (String line = reader.readLine(); line != null; line = reader.readLine())
{
for (String _word : line.split(" "))
{
String word = _word.toLowerCase();
if (Arrays.asList(tokens).contains(word))
if (wordTodocumentMap.get(word) == null)//check whether word is new word
{
documentToCount = new TreeMap<Integer,Integer>();
wordTodocumentMap.put(word, documentToCount);
}
documentToCount.put(fileno, frequency+1);//add the location and frequency
}
}
}
reader.close();
}
}
The error I get is:
Exception in thread "main" java.lang.NullPointerException
at assigenment2.invertedIndex.main(invertedIndex.java:65)
You’re never instantiating wordTodocumentMap, so it remains null throughout. Therefore the line if (wordTodocumentMap.get(word) == null)//check whether word is new word throws a NullPointerException when you do .get(), that is, before you have anything to compare to null. One possible solution is to instantiate the map in the declaration:
public static Map<String, Map<Integer,Integer>> wordTodocumentMap = new HashMap<>();
There may be other problems in your code, but this should get you a step further.
I need to add the contents of every line (single word) of an user input text file into a separate element in an array.
*I know an ArrayList is a better data structure for this problem but I am limited to using only an array.
Here is my code:
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a file name: ");
System.out.flush();
String filename = scanner.nextLine();
File file = new File(filename);
FileReader reader = new FileReader(file);
try (BufferedReader buffReader = new BufferedReader(reader)) {
String line;
int i=0;
String[] words = new String[10];
while((line = buffReader.readLine()) != null) {
words[i]=buffReader.readLine();
System.out.println(words[i]);
i++;
}
}
catch(IOException e){
System.out.println(e);
}
}
The input file is simply:
Pans
Pots
opt
sit
it's
snap
Program output is below. It seems to be skipping every other line.
Pots
sit
snap
You are reading two lines per while loop iteration, one in the while condition, and the other in the first line of the loop body. The result is that each iteration consumes two lines, and only the second of the two is printed.
Eliminate the second call in the loop body, so that you have one iteration of the loop (and one print statement) per line.
Change the while loop as shown below. If while loop condition check, you are reading the line then again reading the line in first line in your while loop. So you are reading two lines and printing only one line.
while((line = buffReader.readLine()) != null) {
System.out.println(line );
i++;
}
while((line = buffReader.readLine()) != null)
That line read and consumed the first input line and that is why you are not seeing Pans and the even indexed inputs.
Can you use an ArrayList than just convert it to an Array using the toArray method? What are your limitations and why?
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a file name: ");
System.out.flush();
String filename = scanner.nextLine();
File file = new File(filename);
FileReader reader = new FileReader(file);
try (BufferedReader buffReader = new BufferedReader(reader)) {
String line;
int i=0;
String[] words = new String[10];
//modify line = buffReader.readLine()
while((words[i]=buffReader.readLine()) != null) {
//modify //words[i]=buffReader.readLine();
//words[i]=buffReader.readLine();
System.out.println(words[i]);
i++;
}
}
catch(IOException e){
System.out.println(e);
}
}
Help guys, i've just seen this example in the web. i would like to use this to print exactly the contents of a text file in the same format containing new lines, but it just prints out the first line. thanks
import java.util.*;
import java.io.*;
public class Program
{
public static void main(String[] args)throws Exception
{
Scanner scanner = new Scanner(new FileReader("B:\\input.txt"));
String str = scanner.nextLine();
// Convert the above string to a char array.
char[] arr = str.toCharArray();
// Display the contents of the char array.
System.out.println(arr);
}
}
public class Program {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(new FileReader("B:\\input.txt"));
String str;
while ((str = scanner.nextLine()) != null)
// No need to convert to char array before printing
System.out.println(str);
}
}
The nextLine() method provides only one line, you must call it until have a null ( ~ C's EOF )
Try this.. To read the whole file as it is.....
File f = new File("B:\\input.txt");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String s = null;
while ((s = br.readLine()) != null) {
// Do whatever u want to do with the content of the file,eg print it on console using SysOut...etc
}
br.close();
But still if you want to use Scanner then try this....
while ( scan.hasNextLine() ) {
str = scan.nextLine();
char[] arr = str.toCharArray();
}
I have a java problem. I am trying to read a txt file which has a variable number of integers per line, and for each line I need to sum every second integer! I am using scanner to read integers, but can't work out when a line is done. Can anyone help pls?
have a look at the BufferedReader class for reading a textfile and at the StringTokenizer class for splitting each line into strings.
String input;
BufferedReader br = new BufferedReader(new FileReader("foo.txt"));
while ((input = br.readLine()) != null) {
input = input.trim();
StringTokenizer str = new StringTokenizer(input);
String text = str.nextToken(); //get your integers from this string
}
If I were you, I'd probably use FileUtils class from Apache Commons IO. The method readLines(File file) returns a List of Strings, one for each line. Then you can simply handle one line at a time.
Something like this:
File file = new File("test.txt");
List<String> lines = FileUtils.readLines(file);
for (String line : lines) {
// handle one line
}
(Unfortunately Commons IO doesn't support generics, so the there would be an unchecked assignment warning when assigning to List<String>. To remedy that use either #SuppressWarnings, or just an untyped List and casting to Strings.)
This is, perhaps, an example of a situation where one can apply "know and use the libraries" and skip writing some lower-level boilerplate code altogether.
or scrape from commons the essentials to both learn good technique and skip the jar:
import java.io.*;
import java.util.*;
class Test
{
public static void main(final String[] args)
{
File file = new File("Test.java");
BufferedReader buffreader = null;
String line = "";
ArrayList<String> list = new ArrayList<String>();
try
{
buffreader = new BufferedReader( new FileReader(file) );
line = buffreader.readLine();
while (line != null)
{
line = buffreader.readLine();
//do something with line or:
list.add(line);
}
} catch (IOException ioe)
{
// ignore
} finally
{
try
{
if (buffreader != null)
{
buffreader.close();
}
} catch (IOException ioe)
{
// ignore
}
}
//do something with list
for (String text : list)
{
// handle one line
System.out.println(text);
}
}
}
This is the solution that I would use.
import java.util.ArrayList;
import java.util.Scanner;
public class Solution1 {
public static void main(String[] args) throws IOException{
String nameFile;
File file;
Scanner keyboard = new Scanner(System.in);
int total = 0;
System.out.println("What is the name of the file");
nameFile = keyboard.nextLine();
file = new File(nameFile);
if(!file.exists()){
System.out.println("File does not exit");
System.exit(0);
}
Scanner reader = new Scanner(file);
while(reader.hasNext()){
String fileData = reader.nextLine();
for(int i = 0; i < fileData.length(); i++){
if(Character.isDigit(fileData.charAt(i))){
total = total + Integer.parseInt(fileData.charAt(i)+"");
}
}
System.out.println(total + " \n");
}
}
}