We\'re making a simple program with java taking an input.txt file, reading it an
ID: 3598886 • Letter: W
Question
We're making a simple program with java taking an input.txt file, reading it and the output will be printing the frequency of the word. We haven't been taught maps or bufferedreader so I'm not sure how to go about this. I currently have:
import java.io.File;
import java.io.PrintWriter;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
public class wordCount
{
static public void main(String[] args)
{
try
{
Scanner sc = new Scanner(System.in);
String name;
System.out.println("Please type the location and file type you want to read in: "); // i.e. "/Users/admin/documents/input.txt" ..
name = sc.nextLine();
// create a Scanner object to read the actual text file
File input = new File(name);
Scanner scan = new Scanner(input);
String line = scan.nextLine();
String[] words = new String[];
words[0] = words[0];
counts[0] = 1;
/** this gives me an error:
String[] words = new String[arr.length];
int[] counts = new int[arr.length];
words[0] = words[0];
counts[0] = 1;
for (int i = 1, j = 0; i < arr.length; i++)
{
if (words[j].equals(arr[i]))
{
counts[j]++;
} else
{
j++;
words[j] = arr[i];
counts[j] = 1;
}
}*/
sc.close();
scan.close();
} catch (Exception ex)
{
ex.printStackTrace();
}
}
}
Also, would it be better for me to use a while loop instead of the for loop:
while (scan.hasNext())
{
line = scan.nextLine();
System.out.println(line);
}
Explanation / Answer
Implemeted the code as per the requirement. Please comment if any modification required.
Code:
========
import java.io.File;
import java.io.PrintWriter;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
public class WordCount
{
static String line = "";
static String[] words = null;
static int[] counts = null;
static int index = 0 , tot_wds = 0;;
static public void main(String[] args)
{
try
{
Scanner sc = new Scanner(System.in);
String name;
System.out.println("Please type the location and file type you want to read in: "); // i.e. "/Users/admin/documents/input.txt" ..
name = sc.nextLine();
// create a Scanner object to read the actual text file
File input = new File(name);
Scanner scan = new Scanner(input);
//This while loop will count the total number of words in the file
while(scan.hasNext()){
scan.next();
tot_wds++;
}
scan = new Scanner(input);
words = new String[tot_wds];
counts = new int[tot_wds];
String [] temp = null;
while(scan.hasNext()){ //Checking until last line of the file
line = scan.nextLine(); //Reading line
temp = line.split(" "); //Splitting each line by space
int i=0,j = 0, id=0;
boolean found = false;
for ( i = 0; i < temp.length; i++)
{
for( j = 0; j<words.length; j++){
if(words[j]!=null && words[j].equals(temp[i])){ //Checking if word is already counted
found = true;
id=j;
break;
}
}
if(found){
counts[id]++; //Recurred word count
}
else{ //This is first time appearing a word, giving counter value
words[index] = temp[i];
counts[index] = 1;
index++;
}
}
}
for(int i=0; i<index; i++){
System.out.println(words[i] + " "+counts[i] );//Result
}
sc.close();
scan.close();
} catch (Exception ex)
{
ex.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.