Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 20th, 2004, 1:19 PM   #1
the_real_g
Newbie
 
Join Date: Oct 2004
Posts: 2
Rep Power: 0 the_real_g is on a distinguished road
Hey everyone

I'm not good with java so i was wondering if anyone could help me build this basic java program in textpad. I need to have a program that takes a string (upper case only) as a parameter and returns a histogram of the letters in the string. The ith element of the histogram should contain the number of ith character in the string alphabet. For example, if the user types in a "AAABBEECEDEDEDDDE", then the string alphabet is "(A,B,C,D,E)". The output should like something like this:

Please input a string:
> AAABBEECEDEDEDDDE
The histogram is:
A xxx
B xx
E xxxxxx
C x
D xxxxx

Anyone who can code this, thanks alot!!!!!!
the_real_g is offline   Reply With Quote
Old Oct 20th, 2004, 4:11 PM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Show us what you have attempted so far. The lowest price you will be able to pay anyone on this forum to do your code thus far would be $999.98 I believe. Personally, I cost $1000.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Oct 20th, 2004, 4:34 PM   #3
the_real_g
Newbie
 
Join Date: Oct 2004
Posts: 2
Rep Power: 0 the_real_g is on a distinguished road
This is what i have so far. It only reads the input i think. I does not give my required output. Can you help?

class Histogram
{
public static void main(String[] args)
*throws java.io.IOException
*{
*char c;
*StringBuffer name = new StringBuffer();
*
*System.out.println("Please input a string and press enter")
*do
*{
*c = (char)System.in.read();
*name.append©;
*}


Thanks
the_real_g is offline   Reply With Quote
Old Oct 20th, 2004, 5:36 PM   #4
sykkn
Hobbyist Programmer
 
Join Date: Apr 2004
Location: Texas
Posts: 106
Rep Power: 5 sykkn is on a distinguished road
have a little formatting ... no charge ... it's on me ....

class Histogram
{
 * * * *public static void main(String[] args) 
 * * * * throws java.io.IOException
 * * * *{
 * * * * * * * *char c;
 * * * * * * * *StringBuffer name = new StringBuffer();
 * *
 * * * * * * * *System.out.println("Please input a string and press enter")
 * * * * * * * *do
 * * * * * * * *{
 * * * * * * * * * * * *c = (char)System.in.read();

 * * * * * * * * * * * *name.append©;
 * * * * * * * *}
 * * * *}
}


I'm not sure what the © thing is all about ...
__________________
[ [ SykkN alloc ] initWithThePowerTo: destroyYouAll ];
/* Don't make me use it! */
sykkn is offline   Reply With Quote
Old Oct 21st, 2004, 3:21 AM   #5
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
c = (char)System.in.read();

that line will throw an error on more then 1 character :/

Here is a little idea in pseudo codeish on how to get it done

Create array size 26 type int - each elemnt starts at 0;
readin line from user = line
for (int i = 0;i < line.length(); i++){
get character at position i;
case 'a'
array[1]++;

then

method to create #
for (int i = 0; i < 26; i++)
print new line
print amount of # for the number in the array

(i no its poo pseudo code but dont want to give you the answer/.
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity."

- Albert Einstein
Berto is offline   Reply With Quote
Old Oct 22nd, 2004, 10:45 AM   #6
sykkn
Hobbyist Programmer
 
Join Date: Apr 2004
Location: Texas
Posts: 106
Rep Power: 5 sykkn is on a distinguished road
c=(char)System.in.read();

this does not give an error if you enter more than one character. System.in.read() by default only reads 1 char from the STDIN. In order for you to use this though, you need to complete your do-WHILE loop.

This will get you passed reading the user input ... you should be able to take it from there.

class Histogram
{
    public static void main(String[] args) throws java.io.IOException
    {
        char c;
        StringBuffer name = new StringBuffer();

        System.out.println("Please input a string and press enter");
        do {
            c = (char)System.in.read();
            name.append(c);
        } while(c != '\n');

        // this line is purely to show that the input was gathered ... 
        // not useful to your actual application ...
        System.out.println(name.toString());
    }
}
__________________
[ [ SykkN alloc ] initWithThePowerTo: destroyYouAll ];
/* Don't make me use it! */
sykkn is offline   Reply With Quote
Old Nov 6th, 2004, 7:26 PM   #7
qub333
Newbie
 
Join Date: Aug 2004
Posts: 13
Rep Power: 0 qub333 is on a distinguished road
do yourslef a favor an read the input into a string,

import java.io.*;

public class ReadString {

public static void main (String[] args) {

// prompt the user to enter their name
System.out.print("Enter your name: ");

// open up standard input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String userName = null;

// read the username from the command-line; need to use try/catch with the
// readLine() method
try {
userName = br.readLine();
} catch (IOException ioe) {
System.out.println("IO error trying to read your name!");
System.exit(1);
}

System.out.println("Thanks for the name, " + userName);

}

} // end of ReadString class


or the more inplementable version

import java.io.*;

public class ReadString {

public String read (String prompt) {

// prompt the user to enter their name
System.out.print(prompt);

// open up standard input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String input = null;

// read the username from the command-line; need to use try/catch with the
// readLine() method
try {
input = br.readLine();
} catch (IOException ioe) {
System.out.println("IO error trying to read your name!");
System.exit(1);
}

return input;
}

} // end of ReadString class


i don't get exactly why you would want to read each letter individualy, thats more C than something like this, Java has quite an extensive API and thier documentation is exclent, more than once ive started a project only to find that half of it was already done. granted, its still good practice to learn how to do it the hard way, but something like this, it just makes life much simpler.
qub333 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:28 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC