Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to read lines of uppercase text from a file and to produce a sim

ID: 3564606 • Letter: W

Question

Write a program to read lines of uppercase text from a file and to produce a simplified text concordance, which is a list of distinct words in the text, with their frequency (number of occurrence).

Here's my linked list class to go with the class thats going to be made.

package Assign;

public class linkedlist {
  
   private Node head;
   private int size;
  
  
   public linkedlist() {
       // TODO Auto-generated constructor stub
       head=null;
       size=0;
   }

   private void addFirst (E item){
       Node temp = new Node (item, head);
       head=temp;
       size++;
   }
  
   private void addAfter(Node node, E item){
       Node temp = new Node(item, node.next);
       node.next=temp;
       size++;
   }
   public void add (int index, E item){
       if (index==0)
           addFirst(item);
       else
       {
           Node node = getNode(index-1);
           addAfter(node, item);
       }
   }
  
   public boolean add (E item){
       add(size, item);
       return true;
   }
  
  
   private Node getNode(int index){
       Node node=head;
       for (int i=0; i            node=node.next;
       return node;
   }
  
   public E get (int index){
       Node node = getNode(index);
       return node.data;
   }
  
  
  
   private static class Node {
       private E data;
       private Node next;
      
      
       private Node(E dataItem){
           data=dataItem;
           next=null;
       }
      
       private Node(E dataItem, Node nodeRef){
           data=dataItem;
           next=nodeRef;
       }

   }
}

Explanation / Answer

You can use charAt() like this example:
String s = "hello";
char ch = s.charAt(0); // first char of "hello" is 'h'

Hope this helps.

This is the program

import java.io.*;
import java.util.*;

public class Index
{
public static void main(String[] args) throws FileNotFoundException{

ArrayList<String> words = new ArrayList<String>();

try{
File testWords = new File("Test Words.txt");
Scanner in = new Scanner(testWords);

while(in.hasNext()){
String word = in.nextLine();
words.add(in.next());
}
}catch(IOException e){
System.out.println("File of Test Words not found");
}

System.out.println("The Original Text:");

for(int i = 0; i < words.size(); i++){
System.out.print(words.get(i)+" ");
}

System.out.println("The Text Sorted Out:");

Collections.sort(words);
for(int i = 0; i < words.size(); i++){
System.out.print(words.get(i)+" ");
}

}
}

Its nagging me at the back of my brain that there should be some way of searching the first letter of each word by using CHAR but I just can't seem to get it, and having a devil of a time trying to find examples.

Not looking for completed code here, but something that might help point me in the direction I need to go or an example of something similar.

Thanks,

Update : Okay..that helps, I figured it would be something char related just couldn't think of the means of it, however doing it that way I get "incomparable types: char and java.lang.String. With the following code:

for(int i = 0; i < words.size(); i++){
char ch = words.get(i).charAt(0);
if (ch=="A"){
aWords.add(i);
}
}

I attempted to try and change the array into a char Array to see if that might help by adding:

char[] tempArray;

tempArray = words.toCharArray();

to see if this might help but instead tells me: cannot find Symbol - Method toCharArray(). I do have java.lang.* imported just be sure..

Update 2: Okay...I have played around with it more, and managed to come up with the following:

for(int i = 0; i < words.size(); i++){
//String temp = words.get(i);
char ch = words.get(i).charAt(0);
if(ch == 'A'){
aWords.add(i);
}
}

Now the issue is when I'm trying to add it to my ArrayList aWords. I'm sure there has to be an easier way of converting it to Char, do the search then convert back to String so it can be added back into the Array..

Update 3: Wait..I think I figured it out...the code should appear something like:

for(int i = 0; i < words.size(); i++){
//String temp = words.get(i);
char ch = words.get(i).charAt(0);
if(ch == 'A'){
String temp = words.get(i);
aWords.add(temp);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote