Question

Hallo! I have a problem in my code, I need help please in java code. My...

Hallo!

I have a problem in my code, I need help please in java code.

My task is :

Write a program that can read a text from a file and do frequency analysis on the letters that occur . To store information (frequency) you must use an array.
Tip! The place where you collect the frequency of eg the letter A is the equivalent of the ASCII code of "A" ie 65.
The array should be 127 elements.
For more info see: asciitable.com  Output should be given in the form of a table:
Letter number
A 112
B 15
. ... ...
and so on
Frequency 9.33% 1.25%
The frequency is given in% and only letters that occur at least 1
once included.

my code is :

import java.util.Scanner;

import java.io.File;

import java.io.IOException;

import java.text.DecimalFormat;

public class FreqvensAnalys {

public static void main(String[] args) throws IOException {

int arr[] = new int[128];// array to store count of ascii char's

for (int i = 0; i < arr.length; i++)

arr[i] = 0;// initializes all values in arr to 0.

// opens file named input.txt and creates an object read.

Scanner read = new Scanner(new File("Klasslista(3).txt"));

String str = "";// reads the input data into str.

while (read.hasNextLine()) {

str += read.nextLine();

}

int i = 0, n = str.length();

while (i < n) {// iterates through the string and increases the count of

   // arr.

arr[(int) str.charAt(i)] += 1;

i++;

}

// prints the percentages and ascii values.

for (i = 0; i < arr.length; i++) {

double per = (arr[i] / (double) n) * 100;

System.out.printf("%3d : %2f ", i, per);}}}

/*=========================== END ================================*/

My problem in the out put, I get this out put! any body can help me please!

0 : NaN 1 : NaN 2 : NaN 3 : NaN 4 : NaN 5 : NaN 6 : NaN 7 : NaN 8 : NaN 9 : NaN 10 : NaN 11 : NaN 12 : NaN 13 : NaN 14 : NaN 15 : NaN 16 : NaN 17 : NaN 18 : NaN 19 : NaN 20 : NaN 21 : NaN 22 : NaN 23 : NaN 24 : NaN 25 : NaN 26 : NaN 27 : NaN 28 : NaN 29 : NaN 30 : NaN 31 : NaN 32 : NaN 33 : NaN 34 : NaN 35 : NaN 36 : NaN 37 : NaN 38 : NaN 39 : NaN 40 : NaN 41 : NaN 42 : NaN 43 : NaN 44 : NaN 45 : NaN 46 : NaN 47 : NaN 48 : NaN 49 : NaN 50 : NaN 51 : NaN 52 : NaN 53 : NaN 54 : NaN 55 : NaN 56 : NaN 57 : NaN 58 : NaN 59 : NaN 60 : NaN 61 : NaN 62 : NaN 63 : NaN 64 : NaN 65 : NaN 66 : NaN 67 : NaN 68 : NaN 69 : NaN 70 : NaN 71 : NaN 72 : NaN 73 : NaN 74 : NaN 75 : NaN 76 : NaN 77 : NaN 78 : NaN 79 : NaN 80 : NaN 81 : NaN 82 : NaN 83 : NaN 84 : NaN 85 : NaN 86 : NaN 87 : NaN 88 : NaN 89 : NaN 90 : NaN 91 : NaN 92 : NaN 93 : NaN 94 : NaN 95 : NaN 96 : NaN 97 : NaN 98 : NaN 99 : NaN 100 : NaN 101 : NaN 102 : NaN 103 : NaN 104 : NaN 105 : NaN 106 : NaN 107 : NaN 108 : NaN 109 : NaN 110 : NaN 111 : NaN 112 : NaN 113 : NaN 114 : NaN 115 : NaN 116 : NaN 117 : NaN 118 : NaN 119 : NaN 120 : NaN 121........................

Regardes!

Hi!

I have Eclipse, have to do anything for my environment?

What about file, Any file it's ok or not!

How I can put ASCII code as input file txt file,

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class FreqvensAnalys {

public static void main(String[] args) throws IOException
{
int arr[] = new int[128];// array to store count of ascii char's
for (int i = 0; i < arr.length; i++)
{
arr[i] = 0;// initializes all values in arr to 0.
}
// opens file named input.txt and creates an object read.

Scanner read = new Scanner(new File("Klasslista(3).txt"));
String str = ""; // reads the input data into str.

while (read.hasNextLine())
{
str += read.nextLine();
}

int i = 0, n = str.length();
while (i < n) // iterates through the string and increases the count of arr
{
arr[(int) str.charAt(i)] += 1;
i++;
}
  
// prints the percentages and ascii values.
for (i = 0; i < arr.length; i++)
{
// prints a new line after every 10 lines
if(i % 10 == 0 && i != 0)
System.out.println();
double per = (arr[i] / (double) n) * 100;
System.out.printf("%3d : %.3f\t", i, per); // %.3f means decimal upto 3 places
}
System.out.println("\n");
}
}

****************************************************************** SCREENSHOT *********************************************************

Input File:

Console Output:

Add a comment
Know the answer?
Add Answer to:
Hallo! I have a problem in my code, I need help please in java code. My...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java : Please help me correct my code: create a single class (Program11.java) with a main...

    Java : Please help me correct my code: create a single class (Program11.java) with a main method and some auxiliary methods to input a 2-D array from a disk file, input some “transactions” to change the 2-D array and output the changed 2-D array to another file. Your main method will be minimal (see below). Most of the work will go on in your methods. In main, declare (but do not instantiate) 2-D array. Then call the three methods. The...

  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • Computer Science - Java Explain the code step by step (in detailed order). Also explain what...

    Computer Science - Java Explain the code step by step (in detailed order). Also explain what the program required. Thanks 7 import java.io.File; 8 import java.util.Scanner; 9 import java.util.Arrays; 10 import java.io.FileNotFoundException; 12 public class insertionSort 13 14 public static void insertionSort (double array]) 15 int n -array.length; for (int j-1; j < n; j++) 17 18 19 20 21 double key - array[j]; int i-_1 while ( (i > -1) && ( array [i] > key array [i+1] -...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • I need help with my code when I run my code running the wrong thing like...

    I need help with my code when I run my code running the wrong thing like this After downSize() words.length=60003 wordCount=60003 vowelCount=206728 this is my code here import java.io.*; import java.util.*; public class Project02 {    static final int INITIAL_CAPACITY = 10;    public static void main (String[] args) throws Exception    {        // ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE COMMAND LINE        if (args.length < 1 )       ...

  • Here is my code to put a quote down, flip it, and reverse it. I do...

    Here is my code to put a quote down, flip it, and reverse it. I do not have much knowledge on coding. Does my code only use Variables, Console I/O, Java Program Design, Intro to IDEs, If Statements, Selection and Conditional Logic, Strings, Loops, Nesting, and Iteration? If not, could it be fixed to only use them? These are the offical steps of the assignment: - Ask a user to enter a quote - whether it's several sentences or a...

  • I need help with this code This is what I need to do: Implement the Stack...

    I need help with this code This is what I need to do: Implement the Stack Class with an ArrayList instead of an array, including the following functions: • empty • push • peek • pop • overrided toString( ) function which returns all of the stack’s contents Things to note: • You no longer need a size. • You no longer need to define a constant DEFAULT_CAPACITY. Since ArrayLists grow dynamically. • Whenever possible, use ArrayList functions instead of...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT