Code Comments
Programming Forum and web based access to our favorite programming groups.Hey guys i really need some help. I'm trying to make a program that takes a
string as input e.g "AAABBCCD" and outputs a histogram of this e.g.
A ***
B **
C **
D *
I have this program (below) that simply lists the elements of the string
but doesn't produce the histogram. Does anyone have some code that
finishes it off? I've been trying for ages!
Thanks alot!
import java.io.*;
class Histogram
{
public static void main(String[] args) throws java.io.IOException
{
char c;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String name = null;
System.out.println("Please input a string and press enter:");
try {
name = br.readLine();
} catch (IOException ioe) {
System.out.println("IO error trying to read your name!");
System.exit(1);
}
//change string to uppercase
name = name.toUpperCase();
for(int i = 0; i < name.length() ; i++ )
{
/*
This for loop would go through each letter in the string.
Everytime you encounter a letter see if you've
already seen the letter or not and keep count of each letter
you have encountered.
*/
//Just default output to show for loops is functional
System.out.println(""+name.charAt(i));
}
}
}
Post Follow-up to this messageOn Fri, 22 Oct 2004 07:15:29 -0400, the_real_g wrote: > I have this program (below) that simply lists the elements of the > string but doesn't produce the histogram. Does anyone have some code > that finishes it off? I've been trying for ages! Show us some of your failed attempts! It looks like the only code "you" have managed to produce, is the template your instructor gave you to get you started. /gordon -- [ do not email me copies of your followups ] g o r d o n + n e w s @ b a l d e r 1 3 . s e
Post Follow-up to this message"the_real_g" <gregriddle84@hotmail.com> schreef in bericht news:19e7bca86b375052219c7648b3bcc057@lo calhost.talkaboutprogramming.com... > Hey guys i really need some help. I'm trying to make a program that > takes a string as input e.g "AAABBCCD" and outputs a histogram of this > e.g. > A *** > B ** > C ** > D * > I have this program (below) that simply lists the elements of the > string but doesn't produce the histogram. Does anyone have some code > that finishes it off? I've been trying for ages! > Thanks alot! Try (for yourself) to (re)formulate an algorithm using programming terms, e. g. numbers, datastructures and operations on them. Then test its correctness b y going over the algorithm with different inputs. Finally, implement the algorithm in a computer program.
Post Follow-up to this messagethe_real_g wrote: > Hey guys i really need some help. I'm trying to make a program that takes > a string as input e.g "AAABBCCD" and outputs a histogram of this e.g. > A *** > B ** > C ** > D * > I have this program (below) that simply lists the elements of the string > but doesn't produce the histogram. Does anyone have some code that > finishes it off? "Finishes it off"? Your code doesn't do anything that the assignment requires. Rule number one: Stop asking for code. 1. Think about how the program works. What does it have to do, when, with whst results? 2. Read your Java computer programming textbook from cover to cover. 3. Write your program. -- Paul Lutus http://www.arachnoid.com
Post Follow-up to this message"the_real_g" <gregriddle84@hotmail.com> writes: > /* > This for loop would go through each letter in the string. > Everytime you encounter a letter see if you've > already seen the letter or not and keep count of each letter > you have encountered. > */ We don't exist to do your homework, but a couple of hints: *) If you know you only will get letters, use char-'A' as offset in an int array which counts occurences *) Use i+'A' as the "label" afterwards.
Post Follow-up to this messagePaul Lutus wrote: <snip request for making the_real_g's homework for him> > 1. Think about how the program works. What does it have to do, when, with > whst results? And this, I would stress, is the really, really important part. The motto "Divide and conquer" is the programmer's best friend. After you know *what* you want done, it's easy to break it down to a set of sub-operations (1. do A, 2. do B, 3. do C) and then (with the help of a textbook) write the code you need to get A, B and C done. > 2. Read your Java computer programming textbook from cover to cover. > > 3. Write your program. IMHO, it is not necessary to read the textbook from cover to cover before beginning to write the program. What I would do (and, in some cases, still do) is start with point 1, then start implementing the elemants necessary and use the textbook as reference where needed. -- -Aki "Sus" Laukkanen
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.