Results 1 to 4 of 4
Thread: Stuck, need help please
- 11-03-2011, 01:36 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 22
- Rep Power
- 0
Stuck, need help please
Hi, i'm doing this program, and im currently stuck.
For this assignment you are to print a table that gives frequencies of the digits 0 through 9 in a typed English text. To keep things simple and concrete, your job will be to print the digit frequencies for multiple lines of text you enter from the keyboard. More specifically, you program should continue entering lines of text until you type two Returns in a row. Here is some sample interaction:
Enter lines of text. Type two Returns in a row to end
[DrJava Input Box]124 people applied for the job 007
[DrJava Input Box]in the new Bond movie. But only 94 or 96 out of the 124 were really possible
[DrJava Input Box]
Digit Frequencies
-----------------------------
0 2
-----------------------------
1 2
-----------------------------
2 2
-----------------------------
3 0
-----------------------------
4 3
-----------------------------
5 0
-----------------------------
6 1
-----------------------------
7 1
-----------------------------
8 0
-----------------------------
9 2
-----------------------------
Tips/Further Qualifications
Use the input style that appears in Program Backwards in Chapter 7. (It accepts multiple lines of text).
You MUST do this assignment with two classes: a driver, which includes main and handles the input, and is called DigitDriver; and a principal processing class, called DigitCounter. Objects in the DigitCounter class are responsible for handling the character processing. DigitCounter must have an array instance variable which acts as a scoreboard, tallying digits when they are encountered in the entered text.
Remember that if someDigit is a char variable that holds a digit, then (someDigit-'0') gives the numerical position of someDigit among all digits. Thus if someDigit holds a 4, then (someDigit - '0') = 4. Use this fact to drive your array indexing.
Use the static method isDigit from the Character class to determine when you've come across a digit.
Your DigitCounter code must include and make use of a method called processString, which is passed a string as a parameter - a typed line of input - and which is responsible for finding and then tallying occurrences of digits in the parameter.
Be sure to comment your code.
This is what i have so far
main
and here's the classJava Code:import java.util.Scanner; public class DigitDriver{ public static void main(String[] args){ String[] stringArray = new String[100]; // setting string System.out.println("Enter text. Type two Returns in a row to end"); // Screen promt Scanner scan = new Scanner(System.in); // enable input from keyboard int pos = 0; String d = " "; while(d.length()>0){ d=scan.nextLine(); stringArray[pos]=d; pos++; } // get input from keyboard DigitCounter myDigit = new DigitCounter(countArray); countArray.processString(); System.out.println("Digit Frequencies"); for(int i=0; i<=9; i++){ System.out.println("-------------"); System.out.println(i + " " + countArray[i]); } // print the table } }
I'm stuck and I'm not sure if what I have is even right or on the right path. Help would be great, ThanksJava Code:public class DigitCounter{ char[] d; private String digit; public DigitCounter(char d){ this.d = new someDigit[digit]; } public String processString(String digit){ for(int j=0; j<=9; j++){ someDigit[j]=0; } for(int k=0; k<someDigit.length(); k++){ if(Character.isDigit(someDigit.charAt(k))){ someDigit[digit]++; return digit;} } }
- 11-03-2011, 02:36 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Re: Stuck, need help please
Search this forum (top right corner) for a thread titled "digit counter"; this problem was posted (and solved) here before.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-03-2011, 09:49 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 22
- Rep Power
- 0
Re: Stuck, need help please
I saw the thread, but I didn't see how it was solved. There were still errors.
- 11-03-2011, 10:25 PM #4
Re: Stuck, need help please
Oh boo hoo!
Nobody posted perfect code that you could copy. I'm sure someone (probably Jos) offered plenty of suggestions and help that would allow you to solve the problem on your own. Any code that was posted might partially solve your problem. It would still be upto you to take that code and rework it so that it did fully solve your problem. Remember it is your work not ours.
Similar Threads
-
Really stuck need help
By B_Tank88 in forum New To JavaReplies: 45Last Post: 07-18-2011, 07:52 PM -
I'm stuck help!!!
By nobody58 in forum Advanced JavaReplies: 2Last Post: 03-18-2010, 02:52 PM -
Stuck in sea
By programmer_007 in forum JDBCReplies: 1Last Post: 09-17-2009, 04:00 AM -
really stuck now..
By shongo in forum Advanced JavaReplies: 2Last Post: 11-09-2008, 02:56 AM -
Stuck in need of help!
By Zombie_Leg! in forum New To JavaReplies: 1Last Post: 09-23-2008, 02:22 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks