![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2004
Posts: 2
Rep Power: 0
![]() |
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!!!!!! |
|
|
|
|
|
#2 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
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 |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Oct 2004
Posts: 2
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Apr 2004
Location: Texas
Posts: 106
Rep Power: 5
![]() |
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! */ |
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
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 |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Apr 2004
Location: Texas
Posts: 106
Rep Power: 5
![]() |
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! */ |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Aug 2004
Posts: 13
Rep Power: 0
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|