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);
}
}
Related
I have a text file list of thousands of String (3272) and I want to put them each into a slot of an Array so that I can use them to be sorted out. I have the sorting part done I just need help putting each line of word into an array. This is what I have tried but it only prints the last item from the text file.
public static void main(String[] args) throws IOException
{
FileReader fileText = new FileReader("test.txt");
BufferedReader scan = new BufferedReader (fileText);
String line;
String[] word = new String[3272];
Comparator<String> com = new ComImpl();
while((line = scan.readLine()) != null)
{
for(int i = 0; i < word.length; i++)
{
word[i] = line;
}
}
Arrays.parallelSort(word, com);
for(String i: word)
{
System.out.println(i);
}
}
Each time you read a line, you assign it to all of the elements of word. This is why word only ends up with the last line of the file.
Replace the while loop with the following code.
int next = 0;
while ((line = scan.readLine()) != null) word[next++] = line;
Try this.
Files.readAllLines(Paths.get("test.txt"))
.parallelStream()
.sorted(new ComImpl())
.forEach(System.out::println);
I was just wondering if there is a way to loop through a text file until a particular string is found.
For example, say you have a text file with the following in it:
banana
apple
grapes
melon
orange
cherries
strawberry chocolate vanilla
I basically want to write a program that loops through the input file until it gets to a particular string the user specifies and then stores the next line in an array list. So, basically say if I imputed cherries I want it to store strawberry, chocolate, vanilla in an array list. For the life of me I cannot figure out how to do this though, so anything would be appreciated. What I have so far is below.
public static void main(String[] args) throws IOException {
String line;
ArrayList<String> names = new ArrayList<>();
Scanner in = new Scanner(System.in);
System.out.print("Enter the input file: ");
String input = in.next();
FileReader file = new FileReader(input);
BufferedReader reader = new BufferedReader(file);
System.out.print("What fruit do you want: ");
String fruit = in.next();
line = reader.readLine();
while ((line != null)) {
if(line.equals(fruit){
}
}
Just loop through the input until the searched line is encountered and store every line found afterwards in the list.
String line;
while((line = reader.readLine()) != null)
if(line.equals(fruit))
break;
ArrayList<String> lines = new ArrayList<>();
while((line = reader.readLine()) != null)
lines.add(line);
if I understand your question correctly, I have an idea that you can start from
Scanner scanner = new Scanner(file);
List<String> list = new ArrayList<String>();
while (scanner.hasNextLine()) { // read the entire file into list but it consumes time especially if the file is big (not perfect choice)
list.add(scanner.nextLine());
}
now what you can do
// add boolean to announce the occurrance of the word
boolean found = false;
for(String word: list){ // then you have a greater control over it to search
if(word.equals(fruit)){
found = true;
}
if (found) {
// start taking the rest of the array into a new array or whatever you want to do
}
}
You could use a boolean to indicate when the value is found and when it is true you can add the line in the List :
boolean isFound = false;
String line;
while ((line=reader.readLine())!= null) {
if(!isFound && line.equals(fruit){
isFound = true;
}
else if (isFound){
names.add(line);
}
}
Create flag isFound. Set it to true when you find the string.
It will then process on the next loop and set to false like so.
bool isFound=false;
while ((line != null)) {
if(isFound==true){
names.add(string)
isFound=false;
}
if(line.equals(fruit){
isFound=true;
}
}
check it out:
public static void main(String[] args) throws IOException {
String line;
List<String> names = new ArrayList<>();
Scanner in = new Scanner(System.in);
System.out.print("Enter the input file: ");
String input = in.next();
FileReader file = new FileReader(input);
BufferedReader reader = new BufferedReader(file);
System.out.print("What fruit do you want: ");
String fruit = in.next();
boolean found=false;
while ((line = reader.readLine()) != null){
if (line.equals(fruit) || found) {
names.add(line);
found=true;
}
}
}
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]);
}
}
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!
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");
}
}
}