File folder2 = new File("C:\\Users\\user\\fypworkspace\\TextRenderer");
File[] listOfFiles2 = folder.listFiles();
System.out.println("Please enter the required word :");
Scanner scan2 = new Scanner(System.in);
String word2 = scan.nextLine();
String [] array2 = word2.split(" ");
{
for (int i=0; i < listOfFiles.length; i++)
{
if ((listOfFiles2[i].getName().endsWith(".txt")))
{
try
{
BufferedReader in= new BufferedReader(new FileReader(listOfFiles2[i].getName()));
int numDoc = 0;
Scanner s2 = new Scanner(listOfFiles2[i].getName());
{
while (s2.hasNext())
{
if (s2.next().equals(word2)) numDoc++;
}
System.out.println("The number of document containing the term is " + numDoc);
}
in.close();
} catch (IOException e) {
}
This is my code for counting the number of documents that contain a specific term.
Every time the program finds a specific term inside the document, it will increment numDoc counter.
This code does not do anything, however. What am I doing wrong?
Add System.out.println() throughout your code to output important information for debugging.
Not directly related but you can use a enhance for loop to loop through the files. See: http://download.oracle.com/javase/tutorial/java/nutsandbolts/for.html
Your System.out.println for your total is inside your loop and it should be outside after you have finished looping through all the documents.
(And maybe the most important) You are not handling your IOException. At the least printout a stack trace or the message from the exception.
Related
I have been stuck for some time now. In my code, I want to read all of the names I have from a separate file, and I want to allow the user to select how many names they want to save to a new separate file and allow them to select which names they want to save to the file (by selecting the number in-front of the names printed to the screen).
I am not sure on how I would allow the user to select certain positions in the array "names" to print to a new file. My idea was to use the array "select" and check what number was in that array (eg select[0] = 1) and print out number[1].
Any help would be much appreciated, thankyou!
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter writefile;
BufferedReader readfile;
Scanner scan = new Scanner(System.in);
readfile = new BufferedReader(new FileReader("names.txt"));
writefile = new BufferedWriter(new FileWriter("nameChoices.txt"));
String names[] = new String[10];
System.out.println("Here are the list of names:");
System.out.println();
for (int i = 0; i < 10; i++) {
names[i] = readfile.readLine();
System.out.println(i + ". " + names[i]);
}
System.out.println();
System.out.println("Please enter the number of names you want to select:");
int choice = scan.nextInt();
System.out.println("Enter the number infront of the names you want to select: ");
int select[] = new int[choice];
for (int j = 0; j < choice; j++) {
select[j] = scan.nextInt();
}
}
}
Your Question: "I am not sure on how I would allow the user to select certain positions in the array "names" to print to a new file."
There is nothing wrong with the way you have already opted to do this other than perhaps letting the User know what selection they happen to be currently on and trapping for invalid entries such as values too high or too low and of course alpha characters where numerical characters are required. Without taking care of this business you leave things open to specific Error Exceptions. When asking the User for input a good solution would be for you to take care of what can go wrong and if it does, allow the User to re-enter the required data.
The biggest problem you have is that you have no mechanism in place to write the desired data to file. You've started by declaring a BufferedWriter object but you have yet to implement it. This would of course entail iterating through the select array which ultimately each element holds the index of the names desired to save to file. Simply iterate through the select array and write each name to file:
for (int i = 0; i < select.length; i++) {
writefile.write(name[select[i]] + System.lineSeparator());
writefile.flush();
}
// Close the file when all writing is done.
writefile.close();
With what is discussed above you might have a solution that would look something like this:
try {
BufferedWriter writefile = new BufferedWriter(new FileWriter("nameChoices.txt"));
BufferedReader readfile = new BufferedReader(new FileReader("names.txt"));
Scanner scan = new Scanner(System.in);
String allNames = "", fileLine = "";
while((fileLine = readfile.readLine()) != null) {
// Using a 'Terary Operator' to fill the allNames variable
// with a comma delimited string of all the names in file. It
// doesn't matter how many names are in file.
allNames+= (allNames.equals("")) ? fileLine : "," + fileLine;
}
// Split the string in allNames to the names array.
String names[] = allNames.split(",");
// Display the names in console:
// Use the System.out.format() method
// to create a columnar display.
System.out.println("Here are the list of names:");
for (int i = 0; i < names.length; i++) {
// Use the modulus operator to ensure 4 columns
if (i % 4 == 0){
// Print a new line
System.out.print("\n");
}
System.out.format("%-15s", (i+1) + ". " + names[i]);
}
readfile.close(); // Close the reader. Don't need to leave it open.
System.out.println();
// Get the number of names the User wants to save...
int choice = 0;
while (choice == 0) {
System.out.println("\nPlease enter the number of names you want to select:");
try {
choice = scan.nextInt();
}
catch (InputMismatchException ex) {
System.err.println("Invalid Entry! Numerical value Only!\n");
scan.nextLine(); // Clear the scanner buffer
choice = 0;
}
}
// Get the actual names the User wants to save...
System.out.println();
int j = 0;
int select[] = new int[choice];
System.out.println("Enter the numbers in front of the names you want to select: ");
while (j < choice) {
System.out.println("Enter the number for name #" + (j+1) + ": ");
try {
int index = scan.nextInt();
if (index < 1 || index > names.length) {
throw new InputMismatchException();
}
select[j] = index;
j++;
}
catch (InputMismatchException ex) {
System.err.println("Invalid Entry! Numerical value Only (1 to " +
names.length + ")!\n");
scan.nextLine(); // Clear the scanner buffer
}
}
//Write choices to file and Console...
System.out.print("\nSaving Names: ");
for (int i = 0; i < select.length; i++) {
String save = names[select[i]-1] ;
System.out.print(save + "\t");
writefile.write(save + System.lineSeparator());
writefile.flush();
}
writefile.close(); // close the writer
System.out.println("\nDONE! File Created!");
}
catch (IOException ex) { ex.printStackTrace(); }
In this example invalid entries are taken care of and the User is given the chance to enter valid data.
I am working with reading/writing files using arrays. This is for a college Java 2 assignment, I feel like I should know this but I'm drawing a blank.
What I need to do is sort the numbers in the file in ascending order.
First, the program checks if a certain file exits within the src file here:
File file = new File("src/chapter12/randomfile.dat"); //File path
if(file.exists())
{ //If the file exists, print out each element on the file.
Scanner input = new Scanner(file);
while(input.hasNextLine())
{
System.out.println(input.nextLine());
}
input.close();
}
^^ This part works out perfectly for searching for the file, then if it is found printing out the lines it contains.
Then, if the file is not found we create it and write 100 random numbers to it here, from 0-100:
else
{ //If the file isn't found, create it and write 100 random numbers to it.
try(PrintWriter output = new PrintWriter(file))
{
for(int i = 0; i < size; i++)
{
randomArray[i] += random.nextInt(101);
output.print(randomArray[i] + " ");
}
}
I have tried inputting Array.sort(randomArray) in two places:
randomArray[i] += random.nextInt(101);
output.print(randomArray[i] + " ");
Arrays.sort(randomArray);
and here
Scanner input = new Scanner(file);
Arrays.sort(randomArray);
while(input.hasNextLine())
{
System.out.println(input.nextLine());
}
input.close();
The first location is in the loop, where it would sort it before the end of each iteration.
The second one is before I do the check for the file, because the first time the program runs, the file doesn't exist so the array is empty. However since the file is created after the first run, logically it would sort the already full array before reading the file, but it made no sense because it would sort the array, but since the file is created, it can't sort the file.
Currently I am trying to search a way to possibly sort the file before printing out what was read, however my assignment states "You can use the array class to sort the file." This is where I draw a blank, any suggestions?
Here is a full snippet of code if needed:
package chapter12;
import java.io.*; //Allows us to use File Writer.
import java.util.Arrays; //Allows us to sort the array.
import java.util.Random; //Allows us to get random numbers.
import java.util.Scanner; //Allows us to read the file.
public class Excercise1215
{
public static void main(String[] args) throws FileNotFoundException
{
int size = 100;
int[] randomArray = new int[size];
Random random = new Random();
File file = new File("src/chapter12/randomfile.dat"); //File path
if(file.exists())
{ //If the file exists, print out each element on the file.
Scanner input = new Scanner(file);
Arrays.sort(randomArray);
while(input.hasNextLine())
{
System.out.println(input.nextLine());
}
input.close();
}
else
{ //If the file isn't found, create it and write 100 random numbers to it.
try(PrintWriter output = new PrintWriter(file))
{
for(int i = 0; i < size; i++)
{
randomArray[i] += random.nextInt(101);
output.print(randomArray[i] + " ");
Arrays.sort(randomArray);
}
}
//Exception handling
catch(FileNotFoundException ex)
{
System.out.println("File was not found.");
}
catch(Exception ex)
{
System.out.println("Something went wrong.");
}
}
}
}
Before writing to the file if it doesn't exist, you should sort your array before writing it. Do this using two for loops like this:
for(int i = 0; i < size; i++) {
randomArray[i] += random.nextInt(101); //Populate the array
}
Arrays.sort(randomArray); // Sort the array
for(int i = 0; i < size; i++) {
output.print(randomArray[i] + " "); // Write the array to the file
}
If the file does exist you should read the values into your array, sort the array, then print the array. Here is a sample:
if(file.exists())
{ //If the file exists, print out each element on the file.
Scanner input = new Scanner(file);
int count = 0;
while(input.hasNextInt())
{
randomArray[count++] = input.nextInt();
}
input.close();
Arrays.sort(randomArray);
for(int i = 0; i < size; i++) {
System.out.println(randomArray[i]);
}
}
You code should be something like this when reading in your unsorted file
Scanner input = new Scanner(file);
i = 0;
while(input.hasNextInt())
{
// add to randomArray
randomArray[i++] = input.nextInt();
}
Arrays.sort(randomArray);
// now iterate your sorted array and print out
for (i = 0; i < randomArray.length; i++) {
System.out.println(randomArray[i]);
}
below is my code for a homework assignment where I need to read the contents of an external file and determine the number of words within it, number of 3-letter words, and percentage of total. I've got that part down just fine, but I also have to print the external file's contents prior to displaying the above information. Below is my current code:
public class Prog512h
{
public static void main( String[] args)
{
int countsOf3 = 0;
int countWords = 0;
DecimalFormat round = new DecimalFormat("##.00"); // will round final value to two decimal places
Scanner poem = null;
try
{
poem = new Scanner (new File("prog512h.dat.txt"));
}
catch (FileNotFoundException e)
{
System.out.println ("File not found!"); // returns error if file is not found
System.exit (0);
}
while (poem.hasNext())
{
String s = poem.nextLine();
String[] words = s.split(" ");
countWords += words.length;
for (int i = 0; i < words.length; i++)
{
countsOf3 += words[i].length() == 3 ? 1 : 0; // checks for 3-letter words
}
}
while(poem.hasNext())
{
System.out.println(poem.nextLine());
}
System.out.println();
System.out.println("Number of words: " + countWords);
System.out.println("Number of 3-letter words: " + countsOf3);
System.out.println("Percentage of total: " + round.format((double)((double)countsOf3 / (double)countWords) * 100.0)); // converts value to double and calculates percentage by dividing from total number of words
}
}
The statement
while(poem.hasNext())
{
System.out.println(poem.nextLine());
}
is supposed to print the external file's contents. However, it doesn't. When I try moving it before my prior while loop, it prints, but screws up my printed values for the # of words, 3-letter words, percentage, etc. I'm not really sure what the issue is here. Could someone provide some assistance?
Thank you in advance.
Your scanner is trying to reread the file but it is at the bottom so there are no more lines to read. You have two options:
Option 1
Create a new Scanner object for the same file (to start at the beginning again) and then call your while loop on that file (works, but not a great design).
Scanner poem2 = null;
try
{
poem2 = new Scanner (new File("prog512h.dat.txt"));
}
catch (FileNotFoundException e)
{
System.out.println ("File not found!"); // returns error if file is not found
System.exit (0);
}
while(poem2.hasNext())
{
System.out.println(poem2.nextLine());
}
Option 2
A better option would be to display each line as you read it in. This can be accomplished by adding an extra line to the already existent while loop:
while (poem.hasNext())
{
String s = poem.nextLine();
System.out.println(s); // <<< Display each line as you process it
String[] words = s.split(" ");
countWords += words.length;
for (int i = 0; i < words.length; i++)
{
countsOf3 += words[i].length() == 3 ? 1 : 0; // checks for 3-letter words
}
}
This only requires one Scanner object and only requires one read-through of the file which is much more efficient.
For a homework assignment, I need to create a class that that can read and write Byte arrays to/from a file. I have successfully created classes that can read and write CSV and text, however I am having some difficulty, when it comes to arrays.
When I run the 'readByte' method (see below) I do not get compiler errors , instead, I can not get the contents of the file to print. Instead the console just displays "File Read", demonstrating that it has successfully processed the nested for loop. I have studied various resources yet I can not find the solution. I am sure it is a simple mistake somewhere, any advice on how I can resolve this will be greatly appreciated.
The contents of the file I am trying to read is also below.
A second question (I thought it would be best to put both questions in one post), in my 'writeByte' method (the user enters values into a 2D array which is then printed in a .txt file), allows me to enter two values before I get the follow error message:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at com.gc01.FileManager.ByteManager.writeByte(ByteManager.java:93)
at com.gc01.FileManager.ByteManager.main(ByteManager.java:111)
I am sure its something to do with how the for the loop collects the user input, but I can't work out how to correct it. Ideally the file would look similar to the file being read by the 'readByte' method.
I understand this a long question, but I have run into a brick wall and all help would be greatly appreciated!
File Contents (separated by tab)
1 10
2 11
3 12
4 13
public class ByteManager {
public String getByteFile(){
Scanner sc = new Scanner (System.in);
System.out.println("Please enter the file directory of the chosen txt file?");
System.out.println("For Example: /Users/UserName/Downloads/FileName.txt");
///Users/ReeceAkhtar/Desktop/FileName.txt
final String fileName = sc.nextLine();
System.out.println("How many columns are in the file?");
final int columns = sc.nextInt();
System.out.println("How many rows are in the file?");
final int rows = sc.nextInt();
return fileName;
}
public void readByte(final String fileName, int rows,int columns){
BufferedReader br = null;
String[] line;
String splitBy = "\t";
int [][] data = new int[rows] [columns];
try {
br = new BufferedReader(new FileReader(fileName));
for (int i = 0; i < rows; i++){
line = br.toString().split(splitBy);
//data[i] [0] = Integer.parseInt(line[i]);
for (int j = 0; j < columns; j++){
data[i] [j] = Integer.parseInt(line[j]);
System.out.println(data[i][j]);
}
}
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("*****File Read*****");
}
public String chooseFileOutput(){
Scanner sc = new Scanner (System.in);
System.out.println("Please enter the file directory for the output of the chosen file");
System.out.println("For Example: /Users/UserName/Downloads/FileName.txt");
///Users/ReeceAkhtar/Desktop/GeoIPCountryWhois.csv
final String fileNameOUT = sc.nextLine();
return fileNameOUT;
}
public void writeByte(final String fileNameOUT){
Scanner input = new Scanner (System.in);
FileOutput createData = new FileOutput (fileNameOUT);
System.out.println("How many rows?");
int rowsOut = input.nextInt();
System.out.println("How many columns?");
int columnsOut = input.nextInt();
System.out.println("Please enter data.");
int newDataR = 0;
int newDataC = 0;
int [] [] data = new int [rowsOut] [columnsOut];
for (int i = 0; i < rowsOut; i++){
createData.writeInteger(newDataR = input.nextInt());
System.out.print("\t");
for (int j = 0; j < columnsOut; i++){
createData.writeInteger(newDataC = input.nextInt());
data[i] [j] = data[newDataR] [newDataC];
System.out.print("\t");
}
}
createData.close();
}
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
final ByteManager object = new ByteManager ();
System.out.println("1 for Read File, 2 for Write file");
String choice = in.nextLine();
if("1".equals(choice)){
object.readByte(object.getByteFile(), 0, 0);
} else if ("2".equals(choice)){
object.writeByte(object.chooseFileOutput());
} else{
System.out.println("Goodbye!");
System.exit(0);
}
}
}
For your first question (about being unable to read):
In the main method, the following line:
object.readByte(object.getByteFile(), 0, 0);
Provides 0 and 0 for the number of rows and columns to be read from the text file. The method getByteFile() does prompt the user for a number of rows and columns, however it does not return this amount and does nothing with those numbers. You must prompt the user for the number of rows and columns and put those as the second and third arguments of the readByte method in main.
Also, in general, I wonder why you bother to create the object int[][] data in both your readByte and writeByte methods. They are void methods, so you don't return the int[][], or do anything meaningful with it. Do you intend to use this object later?
"Line 93 is: 'data[i] [j] = data[newDataR] [newDataC];"
OK that's your problem. I think what you mean to do is the following:
data[i][j] = newDataC;
A multidimensional array is just an array of arrays. When you have an access like this:
data[i][j]
What that means is you are accessing element #j of array #i.
data[][] is an array of int arrays, data[i] is an array of ints and data[i][j] is an int element in data[i].
So what I would recommend is reviewing how multidimensional arrays work and then generally reviewing your code to look for stuff like this:
createData.writeInteger(newDataR = input.nextInt());
Which basically seems nonsensical. Not meant as a negative comment, this line just doesn't make sense in the context of multidimensional arrays because only the "columns" hold int elements.
For your exception:
I wasn't sure my comment was clear, so I'll try here.
You have:
for (int i = 0; i < rowsOut; i++){
createData.writeInteger(newDataR = input.nextInt());
System.out.print("\t");
for (int j = 0; j < columnsOut; i++){ //do you mean to increment j here?
createData.writeInteger(newDataC = input.nextInt());
data[i] [j] = data[newDataR] [newDataC];
System.out.print("\t");
}
}
On your second for loop, your are incrementing i, instead of j. You're incrementing it twice, so you'll go 0,2,4...
So you are almost certainly going to exceed that dimension of your array. As Radiodef said, I think you would benefit from reading up on multi-dimensional arrays.
System.out.println("Please enter the required word :");
Scanner scan2 = new Scanner(System.in);
String word2 = scan2.nextLine();
String[] array2 = word2.split(" ");
for (int b = 0; b < array2.length; b++) {
int numofDoc = 0;
for (int i = 0; i < filename; i++) {
try {
BufferedReader in = new BufferedReader(new FileReader(
"C:\\Users\\user\\fypworkspace\\TextRenderer\\abc"
+ i + ".txt"));
int matchedWord = 0;
Scanner s2 = new Scanner(in);
{
while (s2.hasNext()) {
if (s2.next().equals(word2))
matchedWord++;
}
}
if (matchedWord > 0)
numofDoc++;
} catch (IOException e) {
System.out.println("File not found.");
}
}
System.out.println("This file contain the term " + numofDoc);
}
}
}
this is my code for calculating number of documents containing a specific term. For example :
assume i have 10 million text file and string COW appears in one thousand of these. I am looking for the total one thousand documents containing the COW string.
My program currently only can process one string input.
The output of my program is :
COW
The files containing this term is 1000.
The problem i facing now is when i input 3 strings, It cannot process 3 strings. For example :
COW IS GOOD
The files containing this term is 0.
The files containing this term is 0.
The files containing this term is 0.
I have been trying whole day but i cant see where is my mistake. Mind pointing my mistakes ?
According to your code, you do a loop 3 times (array2.length) but you don't use the array2 at all, instead, you look for the string "COW IS GOOD" three times. you should change the line s2.next().equals(word2) to s2.next().equals(array2[b])
The problem lies here:
if (s2.next().equals(word2))
if word2 = "I love you" and you're doing an equals(), s2.next() must contain the word I love you.
One way to solve this.
String[] words = word2.split(" ");
for (String word: words) {
if (s2.next().equals(word)) {
matchedWord++;
}
}