How to select a random word from a txt file? - java

I have a method that needs to select a random word from a txt file, but it only works some of the time.
The content of the file is as follows:
Broccoli
Tomato
Kiwi
Kale
Tomatillo
My code:
import java.util.Random;
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public String getRandomItem(){
Scanner fileIn = null;
String temp = "";
int r = randomGenerator.nextInt(5) + 1;
byte i = 0;
try {
fileIn = new Scanner(new FileInputStream("bundles.txt"));
} catch (FileNotFoundException e) {
System.out.println("File not found.");
System.exit(0);
}
while(i <= 5){
temp = fileIn.nextLine();
if(i == r){
break;
}
i++;
}
fileIn.close();
return temp;
}
Could someone please tell me where I am going wrong?

I would use Files.readAllLines(Path) to read all the lines once, and then get a single random word from that. Something like,
private static List<String> lines = null;
static {
try {
lines = Files.readAllLines(new File("bundles.txt").toPath());
} catch (IOException e) {
e.printStackTrace();
}
}
private Random rand = new Random();
public String getRandomItem() {
return lines.get(rand.nextInt(lines.size()));
}

Related

Why my Java program works perfectly in windows but it's a disaster in linux?

I wrote a program that reads a text file, deletes the requested string and rewrites it without the string. This program takes three arguments from the terminal: 1) the input file 2) the string 3) the output file.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
class wordfilter {
public static void main(String[] args) {
Scanner scanner = new Scanner("");
Scanner conteggio = new Scanner("");
int numel = 0;
File file = new File(args[0]); // Argomento 0: il file
try {
conteggio = new Scanner(file);
}
catch (FileNotFoundException e) {
System.out.println("File non trovato");
}
while (conteggio.hasNext()) {
numel++;
conteggio.next();
}
conteggio.close();
String[] lettura = new String[numel];
int i = 0;
try {
scanner = new Scanner(file);
}
catch (FileNotFoundException e) {
System.out.println("File non trovato");
}
while (scanner.hasNextLine()) {
lettura[i] = scanner.next();
i++;
}
System.out.println("Contarighe -> " + numel);
for (i = 0; i < lettura.length; i++) {
System.out.println("Elemento " + i + " - > " + lettura[i]);
}
scanner.close();
String escludi = args[1]; // Argomento 1: il filtro
String[] filtrato = rimuovi(escludi, lettura);
if (args.length == 3) stampaSuFile(filtrato, args[2]);
}
public static String[] rimuovi(String esclusione, String[] input) {
String[] nuovoV;
String escludi = esclusione;
int dim = 0;
for (int i = 0; i < input.length; i++) {
if (!input[i].equals(escludi))
dim++;
}
nuovoV = new String[dim];
int j = 0;
for (int i = 0; i < input.length; i++) {
if (!input[i].equals(escludi)) {
nuovoV[j] = input[i];
j++;
}
;
}
return nuovoV;
}
public static void stampaSuFile(String[] out, String path) {
String closingstring = "";
File destinazione = new File(path);
try {
destinazione.createNewFile();
} catch (IOException e) {
System.out.println("Errore creazione file");
}
try {
FileWriter writer = new FileWriter(destinazione);
for (int i = 0; i < out.length; i++)
writer.write(out[i] + (i == (out.length-1) ? closingstring : " "));
writer.close();
System.out.println("Scrittura eseguita correttamente");
} catch (IOException e) {
System.out.println("Errore scrittura file");
}
}
}
On Windows no problem, it works perfectly.
On Linux instead when i write something like java wordfilter in.txt word out.txt
I get
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1478)
at wordfilter.main(wordfilter.java:42)
What's the problem? It's because of some difference on linux?
You're mixing line and token based functions, :hasNextLine() and next(). If the input ends with a line feed (typical on Linux) hasNextLine returns true at the end of the file, but there is no next "item".
while (scanner.hasNextLine()) {
lettura[i] = scanner.next();
i++;
}
You should use either hasNext with next, or hasNextLine with nextLine, mixing them is confusing.
while (scanner.hasNext()) {
lettura[i] = scanner.next();
i++;
}
The input file ends in a newline on Linux. Therefore, there's another line, but it's empty. If you remove the final newline from the input, the program will start working normally.
Or, import the exception
import java.util.NoSuchElementException;
and ignore it int the code
while (scanner.hasNextLine()) {
System.out.println("" + i);
try {
lettura[i] = scanner.next();
} catch (NoSuchElementException e) {}
i++;
}

CodeEval Challenge Fizz Buzz

I am trying to learn Java. The other day I saw a website providing challenges to solve online. Here is the code project I choose: Fizz Buzz
This is where I am with the project:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import java.util.NoSuchElementException;
public class Main {
public static void main(String[] args) throws IOException {
File file = new File(args[0]);
openFile(file);
int[] line = new int[3];
while (nextLine()) {
try{
line = readLine();
String output = getLineOutput(line);
System.out.println(output);
}catch(NoSuchElementException e) { System.out.println("No such element exception"); }
}
}
static Scanner scan;
static void openFile(File file) {
try {
scan = new Scanner((file));
} catch (FileNotFoundException e) {
System.out.println("Could not find file");
}
}
static int[] readLine() {
int a = scan.nextInt();
int b = scan.nextInt();
int c = scan.nextInt();
int[] line;
line = new int[] { a, b, c };
return line;
}
static boolean nextLine() {
return scan.hasNextLine();
}
static String getLineOutput(int[] line) {
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= line[2]; i++)
if (i % line[0] == 0 && i % line[1] == 0) {
sb.append("FB ");
} else {
if (i % line[0] == 0) {
sb.append("F ");
}
if (i % line[1] == 0) {
sb.append("B ");
}
if (i % line[0] > 0 && i % line[1] > 0) {
sb.append(i + " ");
}
}
return sb.toString();
}
}
When I run the program in command prompt providing a path to a text file as the first argument my program seems to work fine. On CodeEval I get the following error:
CodeEval Error: Compilation was aborted after 10 seconds
Should I be accessing the file differently? Is there an exception I'm missing? None of my exceptions are prompting me.
In case this helps anyone in the future this code doesn't close the scanner. Unfortunately on CodeEval the code doesn't execute if this is the case.
Adding scan.close() at the end of main method (after while loop solved) the issue.
Edit: code difference
public static void main(String[] args) throws IOException {
File file = new File(args[0]);
openFile(file);
int[] line = new int[3];
while (nextLine()) {
try{
line = readLine();
String output = getLineOutput(line);
System.out.println(output);
}catch(NoSuchElementException e) { System.out.println("No such element exception"); }
}
scan.close();
}

Random selected strings

I have a text file and i am trying to convert every line into an ArrayList. Then i have to take a random line from this text.file and display it on new JOptionPane.
I am trying to implement it in the for-loop but it always appears only the first line from my text.file. Thank you very much and here is my code.
public void actionPerformed(ActionEvent e) {
ArrayList<String> allQuestions = new ArrayList<String>();
ArrayList<String> allRandomSelectedQuestions = new ArrayList<String>();
File file = new File("C:/Users/User/Documents/NetBeansProjects/SummerExamProject/src/Questions2.txt");
int numberOfRandomQuestions = 16;
try {
//Read line by line from the file
Scanner scan = new Scanner(file);
while (scan.hasNextLine()) {
String line = scan.nextLine();
// System.out.println(line);
JOptionPane.showMessageDialog(null, line.replace("/", "\n"));
scan.close();
allQuestions.add(line);
}
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
for(int i = 0; i < numberOfRandomQuestions; i++){
Random randNum = new Random();
int randQuestionIndex = randNum.nextInt(numberOfRandomQuestions);
String randomQuestion = allQuestions.get(randQuestionIndex);
allRandomSelectedQuestions.add(randomQuestion);
}
}
This line...
scan.close();
Is inside your while loop, so it closes the file after reading a line the first time it goes through the loop.
Moving it to after the loop (i.e. after the close culry-brace) ought to fix it.
The problem is this: scan.close();. You are closing your scanner in the same loop you are using for reading. Moving it outside the loop body should solve the problem.
Call close method after the while loop. What is happening that, you close after the first line. so the while loop stops after only one time.
while (scan.hasNextLine()) {
String line = scan.nextLine();
//System.out.println(line);
JOptionPane.showMessageDialog(null, line.replace("/", "\n"));
allQuestions.add(line);
}
scan.close();
Try this. Hope it helps.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class StackTest {
public static void main(String[] args) {
actionPerformed();
}
public static void actionPerformed() {
ArrayList<String> allQuestions = new ArrayList<String>();
File file = new File("D:/me/test.txt");
int numberOfRandomQuestions = 10;
try {
// Read line by line from the file
Scanner scan = new Scanner(file);
while (scan.hasNextLine()) {
String line = scan.nextLine();
allQuestions.add(line);
}
scan.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
for (int i = 0; i < numberOfRandomQuestions; i++) {
Random randNum = new Random();
int randQuestionIndex = randNum.nextInt(numberOfRandomQuestions);
System.out.println();
String randomQuestion = allQuestions.get(randQuestionIndex);
JOptionPane.showMessageDialog(null, randomQuestion.replace("/", "\n"));
}
}
}

Reverse lines in ArrayList Java

I'm working on a Java program in which I must read the contents of a file and then print each lines reverse. For example the text:
Public Class Helloprinter
Public static void
would print the following after running my reverse program:
retnirPolleh ssalc cilbup
diov citats cilbup
Here's what I got so far:
public static void main(String[] args) throws FileNotFoundException {
// Prompt for the input and output file names
ArrayList<String> list = new ArrayList<String>();
//String reverse = "";
Scanner console = new Scanner(System.in);
System.out.print("Input file: ");
String inputFileName = console.next();
System.out.print("Output file: ");
String outputFileName = console.next();
// Construct the Scanner and PrintWriter objects for reading and writing
File inputFile = new File(inputFileName);
Scanner in = new Scanner(inputFile);
PrintWriter out = new PrintWriter(outputFileName);
String aString = "";
while(in.hasNextLine())
{
String line = in.nextLine();
list.add(line);
}
in.close();
for(int i = 0; i <list.size(); i++)
{
aString = list.get(i);
aString = new StringBuffer(aString).reverse().toString();
out.printf("%s", " " + aString);
}
out.close();
}
}
EDIT:
With Robert's posting it helped put me in the right direction. The problem is that with that is that it doesn't keep the lines.
Public Class Helloprinter
Public static void
becomes after running my program:
retnirPolleh ssalc cilbup diov citats cilbup
it needs to keep the line layout the same. so it should be:
retnirPolleh ssalc cilbup
diov citats cilbup
Your problem is in the line
out.printf("%s", " " + aString);
This doesn't output a newline. I'm also not sure why you are sticking a space in there.
It should be either:
out.println( aString );
Or
out.printf("%s%n", aString);
In your last loop why don't you just iterate through the list backwards? So:
for(int i = 0; i <list.size(); i++)
Becomes:
for(int i = list.size() - 1; i >=0; i--)
It seems like you already know how to read a file, so then call this method for each line.
Note, this is recursion and it's probably not the most efficient but it's simple and it does what you want.
public String reverseString(final String s) {
if (s.length() == 0)
return s;
// move chahctrachter at current position and then put it at the end of the string.
return reverseString(s.substring(1)) + s.charAt(0);
}
Just use a string builder. You were on the right trail. Probably just needed a little help. There is no "one way" to do anything, but you could try something like this:
Note: Here is my output: retnirPolleh ssalc cilbup diov citats cilbup
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Scanner;
public class Reverse {
public static void main(String[] args) {
ArrayList<String> myReverseList = null;
System.out.println("Input file: \n");
Scanner input = new Scanner(System.in);
String fileName = input.nextLine();
System.out.println("Output file: \n");
String outputFileName = input.nextLine();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(fileName));
String text = null;
myReverseList = new ArrayList<String>();
StringBuilder sb = null;
try {
while ((text = br.readLine()) != null) {
sb = new StringBuilder();
for (int i = text.length() - 1; i >= 0; i--) {
sb.append(text.charAt(i));
}
myReverseList.add(sb.toString());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Writer writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outputFileName), "utf-8"));
for (String s : myReverseList) {
writer.write("" + s + "\n");
}
} catch (IOException ex) {
// report
} finally {
try {
writer.close();
} catch (Exception ex) {
}
}
}
}

error while getting data from file to array

I'm trying to get saved data in a text file to an array to use it in my code and then search this array for a string submitted from the user from the GUI , but for some reason I print out the data in the array it is all null. here's the code !!
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class IO {
File f = new File("DB.txt");
PrintWriter write;
Scanner input;
String[][] data;
String nameToSearch;
// search constructor
public IO(String name) {
super();
nameToSearch = name;
try {
input = new Scanner(f);
} catch (FileNotFoundException e) {
System.out.println("File not found please restart the program");
}
data = new String[linesCounter()][2];
int i = 0;
while (input.hasNext()) {
data[i][0] = input.nextLine();
data[i][1] = input.nextLine();
i++;
}
}
public IO(String name, String number) {
try {
write = new PrintWriter(new FileWriter(f, true));
} catch (IOException e) {
System.out.println("Error");
}
write.println(name);
write.println(number);
write.close();
}
int linesCounter() {
try {
input = new Scanner(f);
} catch (FileNotFoundException e) {
System.out.println("File not found please restart the program");
}
int counter = 0;
while (input.hasNext()) {
input.nextLine();
counter++;
}
return counter / 2;
}
int contactFinder() {
int i = 0;
while (input.hasNext()) {
if (data[i][0].equalsIgnoreCase(nameToSearch))
return i;
i++;
}
return -1;
}
String nameGetter() {
return data[contactFinder()][0];
}
String numGetter() {
return data[contactFinder()][1];
}
}
It looks like you read all the lines in from the file to count how many lines there are, and then when you go to read the data, you're starting from where you left off, which would be the end of the file.
It's also worth noting that you can use commons-io FileUtils to easily read all the lines from a file.
For example:
List<String> lines = FileUtils.readLines(f);
String[][] data = new String[lines.length][2];
for (int i = 0; i < lines.size(); i++) {
data[i][i % 2] = lines.get(i);
}
If you also don't want to use a (very useful) third party library, you could load up the data pretty simply with:
List<String> lines = new ArrayList<String>();
Scanner input = new Scanner(f);
while (input.hasNextLine()) {
lines.add(input.nextLine());
}
input.close();
Then go into the array population.
I would advice you to use RandomAccessFile. This has methods such as readLine() to read the line and seek(long pos) to set the file read pointer. You may use seek(0L) to restart the reading of the file.

Categories

Resources