5 Replies - 586 Views - Last Post: 28 January 2013 - 03:56 AM Rate Topic: -----

#1 Nubisco  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-January 13

How to create a for loop to display alphabet occurence count from file

Posted 27 January 2013 - 10:32 PM

So basically i'm trying to write a code that will count the amount of times a letter appears in a text file.
I've got most of the code down, and as far I know it should work. However, I can not figure out how to display
the count, there is a precision error, and i'm guessing it has to do with the part where i try to use a char where or an int should be or vice versa. I am reusing code that I wrote for a different project that used a char array,
this one uses an int array to do it, which explains the precision error, but i do not know how to convert the code to work for an int array. i've tried switching int to char, but that doesn't work.

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

public class SchardinA02 {
 
 public static void main(String [] args) {

	int[] counter = new int[26];
	char c = 0;
	int buf = -1;
	Reader in = null;
	
	try {
	
		System.out.println("Count of each letter found in " + "input.txt" + ":");
		System.out.println("");
		
		in = new FileReader("input.txt");
			while((buf = in.read()) > -1) {
				c = (char) buf;
				c = Character.toLowerCase(c);
				
				if (Character.isLetter(c)) {
					counter [c -'a']++;
			   }
		   }
	   //-------------------------------------------------------------
	 System.out.println("Count of each letter found in input.txt: " );
	 
	 Arrays.sort(counter);
	 
	  for (int addUp = 0; addUp < counter.length; addUp++) {
           char ch = counter[addUp];
           int count = 0;
         
			for (int i = 0; i < counter.length; i++) {
              if (ch == counter[i])
                  count++;
             }//first nested for loop
               boolean flag = false;
         for (int j = addUp - 1; j >= 0; j--) {
              if (ch == counter[j])
                  flag = true;
             }//second nested for loop
              if (!flag) {
              System.out.println(ch + ":" + count);
              }//if statement

		 }//Main for loop
	 
		
    System.out.println("Total letters found: "+ counter.length );
		//------------------------------------------------------

		
	}catch (IOException e) {
     System.out.println("Could not read from file: " + e.getMessage());
	}
	
 }

}


Is This A Good Question/Topic? 0
  • +

Replies To: How to create a for loop to display alphabet occurence count from file

#2 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1275
  • View blog
  • Posts: 2,837
  • Joined: 05-April 11

Re: How to create a for loop to display alphabet occurence count from file

Posted 27 January 2013 - 11:05 PM

If I were you, I would remake the whole last part
What are all those loops for?
The counter variable is holding how many times a letter occurs, so why are you trying to convert the count into a char =o ? I don't follow you on that one
char ch = counter[addUp];



It can be done much simpler
for (int i=0; i<counter.length; i++) {
	char letter = 'a' + i;
	System.out.println(letter + " occured " + counter[i] + " times");
}


Was This Post Helpful? 0
  • +
  • -

#3 Nubisco  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-January 13

Re: How to create a for loop to display alphabet occurence count from file

Posted 27 January 2013 - 11:31 PM

i'm fairly inexperienced and new to java, so forgive me for that.

I had attempted to use those loops from a past project that had successfully done what i had wanted, I know it could have been done simpler but i wasn't able to figure that out.

I also tried to implement your loop with my project but still got that precision error.
It's embarrassing that I ask for help with something I know shouldnt be too difficult, but I've tried nevertheless.

This is what I tried to do.

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

public class SchardinA02 {
 
 public static void main(String [] args) {

	int[] counter = new int[26];
	char c = 0;
	int buf = -1;
	Reader in = null;
	
	try {
	
		System.out.println("Count of each letter found in " + "input.txt" + ":");
		System.out.println("");
		
		in = new FileReader("input.txt");
			while((buf = in.read()) > -1) {
				c = (char) buf;
				c = Character.toLowerCase(c);
				
				if (Character.isLetter(c)) {
					counter [c -'a']++;
			   }
		   }
	   //-------------------------------------------------------------
	 System.out.println("Count of each letter found in input.txt: " );
	 
	  for (int i=0; i<counter.length; i++) {
	      char letter = 'a' + i;
	      System.out.println(letter + " occured " + counter[i] + " times");
         }
	 
    System.out.println("Total letters found: "+ counter.length );
		//------------------------------------------------------

		
	}catch (IOException e) {
     System.out.println("Could not read from file: " + e.getMessage());
	}
	
 }

}

Was This Post Helpful? 0
  • +
  • -

#4 Nubisco  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-January 13

Re: How to create a for loop to display alphabet occurence count from file

Posted 27 January 2013 - 11:46 PM

View PostCasiOo, on 27 January 2013 - 11:05 PM, said:

If I were you, I would remake the whole last part
What are all those loops for?
The counter variable is holding how many times a letter occurs, so why are you trying to convert the count into a char =o ? I don't follow you on that one
char ch = counter[addUp];



It can be done much simpler
for (int i=0; i<counter.length; i++) {
	char letter = 'a' + i;
	System.out.println(letter + " occured " + counter[i] + " times");
}




i'm fairly inexperienced and new to java, so forgive me for that.

I had attempted to use those loops from a past project that had successfully done what i had wanted, I know it could have been done simpler but i wasn't able to figure that out.

I also tried to implement your loop with my project but still got that precision error.
It's embarrassing that I ask for help with something I know shouldnt be too difficult, but I've tried nevertheless.

This is what I tried to do.

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

public class SchardinA02 {
 
 public static void main(String [] args) {

	int[] counter = new int[26];
	char c = 0;
	int buf = -1;
	Reader in = null;
	
	try {
	
		System.out.println("Count of each letter found in " + "input.txt" + ":");
		System.out.println("");
		
		in = new FileReader("input.txt");
			while((buf = in.read()) > -1) {
				c = (char) buf;
				c = Character.toLowerCase(c);
				
				if (Character.isLetter(c)) {
					counter [c -'a']++;
			   }
		   }
	   //-------------------------------------------------------------
	 System.out.println("Count of each letter found in input.txt: " );
	 
	  for (int i=0; i<counter.length; i++) {
	      char letter = 'a' + i;
	      System.out.println(letter + " occured " + counter[i] + " times");
         }
	 
    System.out.println("Total letters found: "+ counter.length );
		//------------------------------------------------------

		
	}catch (IOException e) {
     System.out.println("Could not read from file: " + e.getMessage());
	}
	
 }

} 

Was This Post Helpful? 0
  • +
  • -

#5 Nubisco  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-January 13

Re: How to create a for loop to display alphabet occurence count from file

Posted 28 January 2013 - 12:44 AM

an important thing i forgot to mention is that i'm trying to only display the letters that appears in the file, i'm sorry i didn't make that clear in my first description.

that is why i used those loops.
Was This Post Helpful? 0
  • +
  • -

#6 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1275
  • View blog
  • Posts: 2,837
  • Joined: 05-April 11

Re: How to create a for loop to display alphabet occurence count from file

Posted 28 January 2013 - 03:56 AM

Oh yeah you will have to cast it to a char
The cast is needed because not all int's are chars, but in this case you know they are :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1