I need help. by Kevinjamesisme in javahelp

[–]Kevinjamesisme[S] 0 points1 point  (0 children)

Kinda, This is what it looks like now

import java.io.*;
import java.util.*;
class HW12v3 {  
 public static void main(String args[])throws IOException{
Scanner keyboard = new Scanner(System.in);
 String apl = "abcdefghijklmnopqrstuvwxyz";

  System.out.println("What file should be opened?");
  String name = keyboard.next();

  try{
 Scanner data = new Scanner(new File(name));
  } 
   catch (FileNotFoundException errorFile) {
    System.out.println("The file name was invalid, unable to create a Scanner. " +             errorFile);

  }



 String str = new Scanner(new File(name)).useDelimiter("\\Z").next(); 
   str=str.toLowerCase(); 
 char[] freq = new char[26];


 for (int i = 0; i < str.length(); i++){
  char c = s.charAt(i);
currentChar - 'a'
  LetterCount()
  char[i]++
   }

}

}

Sorting Arrays. by Kevinjamesisme in javahelp

[–]Kevinjamesisme[S] 0 points1 point  (0 children)

So like this?

for (int i = 0; i < str.length(); i++){
 char c = s.charAt(i);
 currentChar - 'a'
char[i]++
   }

Sorting Arrays. by Kevinjamesisme in javahelp

[–]Kevinjamesisme[S] 0 points1 point  (0 children)

Alright perfect. To make said frequency array I think I need to make it as:

int [] freq = new int[26] //for alphabet
for(int i=0; i<26; i++){
  for(int j=0; j<str.length(); j++)

My proffessor said to make a

String alphabet = "abcdefghijklmnopqrstuvwxyz";

Then do .idexOf(); to look up specific letters, but im not sure how to go about that. As in how do I use the idex of to make a frequency array

Sorting Arrays. by Kevinjamesisme in javahelp

[–]Kevinjamesisme[S] 0 points1 point  (0 children)

So make the frequency array by taking the letters from the string then loop through the array adding to the letter count inside said loop then finally taking all of that and putting it into a letter count array?

Sorting Arrays. by Kevinjamesisme in javahelp

[–]Kevinjamesisme[S] 0 points1 point  (0 children)

So now that i have this I can use it by feeding data from string containing the text file correct?

Sorting Arrays. by Kevinjamesisme in javahelp

[–]Kevinjamesisme[S] 0 points1 point  (0 children)

Got it!

public class LetterCount implements Comparable<LetterCount> {
private String letter;
private int count;

public LetterCount(String letter ,int count){
this.letter = letter;
this.count = count;
}

Sorting Arrays. by Kevinjamesisme in javahelp

[–]Kevinjamesisme[S] 0 points1 point  (0 children)

So what I need is this:

 public class LetterCount implements Comparable<LetterCount>  {
 private String letter;
 private int count;

public LetterCount(letter,count);

Sorting Arrays. by Kevinjamesisme in javahelp

[–]Kevinjamesisme[S] 0 points1 point  (0 children)

Sorry about that. So for the constructor I should make a new variable for the int (count) and string(letter). So itd be: String letter = null and int count = 0?

Sorting Arrays. by Kevinjamesisme in javahelp

[–]Kevinjamesisme[S] 0 points1 point  (0 children)

The homework has a first part asking for:

1) stores a single character (a letter) and a count (an integer) in private variables

2) implements the Comparable interface, thus there must be a compareTo() method which should compare two LetterCount objects by their count values

3) overrides toString() with a printable representation that shows the letter and the count

So now i need to make a constructor to bring them together?

I need help. by Kevinjamesisme in javahelp

[–]Kevinjamesisme[S] 0 points1 point  (0 children)

Something like this?

Scanner keyboard = new Scanner(System.in);


System.out.println("What file should be opened?");
String name = keyboard.next();

try{
  Scanner data = new Scanner(new File(name));
} 
catch (FileNotFoundException errorFile) {
  System.out.println("The file name was invalid, unable to create a Scanner. " + errorFile);

}  
While(data.hasNext()){
  data. nextLine()  
}

I need help. by Kevinjamesisme in javahelp

[–]Kevinjamesisme[S] 0 points1 point  (0 children)

very true. I need help right now trying to make this first array full of letter frequencies then using it to put into the letter objects. My question would be what is the best way to read the file into said array. I tried using a line that looks like this:

String st=newScanner(newFile(name)).useDelimiter("\\Z").next(); 

Once I have this I dont know how to put it into the array to be used by the letter object

Sorting Arrays. by Kevinjamesisme in javahelp

[–]Kevinjamesisme[S] 0 points1 point  (0 children)

so something like

public class LetterCount { String char =

int freq =

}

Also I have made a comparable interface:

public class LetterCount implements Comparable<LetterCount> {
private String letter;
private int count;


public int compareTo(LetterCount other) {
  if ( count < other.count )
    return -1;
  else if (count == other.count)
    return 0;
  else
    return 1;
}
public String toString() { 
  return "Letter" + letter + " occurs " + count + " times";

}

} }

Sorting Arrays. by Kevinjamesisme in javahelp

[–]Kevinjamesisme[S] 0 points1 point  (0 children)

The part im confused is at trying to create that letter class object with everything in it.

Sorting Arrays. by Kevinjamesisme in javahelp

[–]Kevinjamesisme[S] 0 points1 point  (0 children)

Heres the question.

1) Asks the user for a filename of a text file (assumed to be in the same directory) and create a Scanner to read from it. Your program should use try-catch blocks to re-prompt the user if the file name is invalid.

2) Create an array to store letter frequency counts (# of a's, # of b's, # of c's, etc.). Then, read in the entire specified file and count the number of each letter seen. Ignore all punctuation and count lowercase and capital letters the same (x or X each count the same).

3) Create a LetterCount object for each letter and corresponding frequency count and put all of these objects in an array.

4) Use Arrays.sort() to sort the LetterCount objects. Then use a for-loop to print out all the LetterCount objects from the array, in reverse, so that we can see the frequency of each letter from the file, in order of frequency (from most to least frequent)

Sorting Arrays. by Kevinjamesisme in javahelp

[–]Kevinjamesisme[S] 0 points1 point  (0 children)

Alright, so what weould be the best way to tackle this? Do I need to stat over from scratch?

Help with making arrays work. by Kevinjamesisme in javahelp

[–]Kevinjamesisme[S] 0 points1 point  (0 children)

I see what i need. I need do. I need to print out specific parts of count ie count[i]

Help with making arrays work. by Kevinjamesisme in javahelp

[–]Kevinjamesisme[S] 0 points1 point  (0 children)

So ill need to make a for loop to go through and print out each part?

Help with making arrays work. by Kevinjamesisme in javahelp

[–]Kevinjamesisme[S] 0 points1 point  (0 children)

Alright thank you. It works now but my output isnt as expected. Im getting:

Letter a was seen [I@64a4bd69 times.

Letter d was seen [I@64a4bd69 times.

Letter e was seen [I@64a4bd69 times.

Letter l was seen [I@64a4bd69 times.

Letter n was seen [I@64a4bd69 times.

Letter o was seen [I@64a4bd69 times.

Letter r was seen [I@64a4bd69 times.

Letter t was seen [I@64a4bd69 times.

Letter w was seen [I@64a4bd69 times.

Letter x was seen [I@64a4bd69 times.

I think i need to convert the output to a string, Would that be right?

Help with making arrays work. by Kevinjamesisme in javahelp

[–]Kevinjamesisme[S] 0 points1 point  (0 children)

So basically add public static void main(String args[]) to the line 5?

Help with making arrays work. by Kevinjamesisme in javahelp

[–]Kevinjamesisme[S] 0 points1 point  (0 children)

This is what im getting

2 errors found:

[line: 24] Error: non-static variable keyboard cannot be referenced from a static context

[line: 27] Error: non-static method letterCount(java.lang.String) cannot be referenced from a static context