Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Working with text files... help !


  • Please log in to reply

#1
mozala

mozala

    New Member

  • Member
  • Pip
  • 8 posts
Im trying to do some java exercises so here again i've got a problem
This is a question:
**************************************************************
The parameter of the procedure is the disk name of the
text file.The procedure will count the number of
words in the text file which begins with "a " ,which
begins with "b" , "c", etc and store the result in
some appropriate array.The array will be another
parameter of the procedure(eg.for instance ,if we
have the word , BOBBOBOSCO ,there are 4B ,4O,1S,1C.in
other words,the output will be the frequency of
occurence of each word in an array.
)
*************************************************************************

This is what I've started up to now.And the other problem my syntax is terrible in this case ,but i think got at least an idea how it supposed to look like but i dont know how to implement it,I can make an outline
for instance:
Static int CountAlphabet( String count.txt) throws exception
{
int count,i,sum;
count = i;
sum=0;
int [] words =new int[words.length];
for (int i=0;i<words.length;i++)
if words[i] = words[i+1]
count+=; ...................... Just have look hint me on some mistakes im doing.

**************************************************************************
package Methew;
import sugar.*;
public class Count{
public static void main(String[]args){
Sys.pln("Enter any word ");
String word = Sys.readLine();
CountAlphabet();
}
static string CountAlphabet(int[]array)
{
int sum=0;
int[]array = new int[ array.length];{
for(int i=0;i<array.length;i++)
{ int k;
k=i;
if array[i] =array[i+1]
sum+= k;
}
Sys.p("There are" +sum)
Sys.pln(" "+array[i])
}
}
  • 0

Advertisements


#2
dsenette

dsenette

    Je suis Napoléon!

  • Administrator
  • 26,046 posts
  • MVP
i'm gonna move you to the programming forum..you'll probably get some better responses
  • 0

#3
gust0208

gust0208

    Member

  • Member
  • PipPipPip
  • 311 posts
Hey Mozala,

I will attach the completed code for this exercise (and paste it to the bottom of this posting), but first I will give you my "pseudo-code" that I used to solve the problem.

The overall gist is that I converted the original word to parse into an array of characters. We then step through each character of the word. With each character, we have two options. We have either seen this character before and need to increment the counter in our integer array, the other is that we have never seen this character before and it needs a new slot in the integer array. The method I used (by no means optimal) to keep track of which characters I have seen is to make a string called "found_chars" that I add each found character to. This allows to see if we have already seen a character simply by seeing if it exists in the "found_chars" string.

Loop through each character in the word (current_char)
1) See if we have already found this char (see if char is contained in found_chars)
a) If not - this means we have a new letter, increment unique_chars, add char to found_chars, initialize appropriate spot in array
b) If found, increment appropriate spot in array

Here is the code (and it is also attached - it has been zipped):

//**************************************************************
import sugar.*;

public class Count{
public static void main(String[]args){
int[] char_freq;

Sys.pln("Enter any word ");
String word = Sys.readLine();
char_freq = CountAlphabet(word);
}

static int[] CountAlphabet(String word)
{
int unique_chars = 0;
char current_char = 'a';

int[] array = new int[word.length()];
char[] c_array = new char[word.length()];
String found_chars = "";

for(int i=0; i < word.length(); i++) {
/* Loop through each character in the word (current_char)
// 1) See if we have already found this char (see if char is contained in found_chars)
// a) If not - this means we have a new letter, increment unique_chars, add char to found_chars, initialize appropriate spot in array
// b) If found, increment appropriate spot in array
/*
current_char = word.charAt(i);
Sys.pln("Current char = " + current_char);
Sys.pln("index of = " + found_chars.indexOf(current_char));
if (found_chars.indexOf(current_char) > -1)
{
Sys.pln("Char previously found");
//Increment appropriate array slot
array[found_chars.indexOf(current_char)]++;
}
else
{
Sys.pln("New char = " + current_char);
found_chars = found_chars.concat(Character.toString(current_char));
array[found_chars.indexOf(current_char)] = 1;
}
Sys.pln("found_chars = " + found_chars);
Sys.pln("*********");
}

Sys.pln("Original word = " + word);
for (int k=0; k<array.length; k++) {
if (array[k] > 0) {
Sys.pln("Letter '" + found_chars.charAt(k) + "' has char_freq[" + k + "] = " + array[k]);
}
}

return array;
}

}// END Class count
//**************************

Cheers and I hope that helps you understand the problem and one man's solution by working with strings.
- Tom

Attached Files


  • 0

#4
gust0208

gust0208

    Member

  • Member
  • PipPipPip
  • 311 posts
Hey everyone,

I mis-typed one of my comment characters causing the code to not compile. Here is the working code and the fixed code is attached.

/**************************************************************
The parameter of the procedure is the disk name of the
text file.The procedure will count the number of
words in the text file which begins with "a " ,which
begins with "b" , "c", etc and store the result in
some appropriate array.The array will be another
parameter of the procedure(eg.for instance ,if we
have the word , BOBBOBOSCO ,there are 4B ,4O,1S,1C.in
other words,the output will be the frequency of
occurence of each word in an array.
)
*************************************************************************/

import sugar.*;

public class Count{
public static void main(String[]args){
int[] char_freq;

Sys.pln("Enter any word ");
String word = Sys.readLine();
char_freq = CountAlphabet(word);
}

static int[] CountAlphabet(String word)
{
int unique_chars = 0;
char current_char = 'a';

int[] array = new int[word.length()];
String found_chars = "";

for(int i=0; i < word.length(); i++) {
/* Loop through each character in the word (current_char)
// 1) See if we have already found this char (see if char is contained in found_chars)
// a) If not - this means we have a new letter, increment unique_chars, add char to found_chars, initialize appropriate spot in array
// b) If found, increment appropriate spot in array
*/
current_char = word.charAt(i);
Sys.pln("Current char = " + current_char);
Sys.pln("index of = " + found_chars.indexOf(current_char));
if (found_chars.indexOf(current_char) > -1)
{
Sys.pln("Char previously found");
//Increment appropriate array slot
array[found_chars.indexOf(current_char)]++;
}
else
{
Sys.pln("New char = " + current_char);
found_chars = found_chars.concat(Character.toString(current_char));
array[found_chars.indexOf(current_char)] = 1;
}
Sys.pln("found_chars = " + found_chars);
Sys.pln("*********");
}

Sys.pln("Original word = " + word);
for (int k=0; k<array.length; k++) {
if (array[k] > 0) {
Sys.pln("Letter '" + found_chars.charAt(k) + "' has char_freq[" + k + "] = " + array[k]);
}
}

return array;
}

}// END Class count

Attached Files

  • Attached File  Count.zip   1016bytes   161 downloads

  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP