Results 1 to 15 of 15
- 12-11-2011, 02:53 AM #1
Senior Member
- Join Date
- Dec 2011
- Posts
- 102
- Rep Power
- 0
Problem with printing numers literally
Hi guys,
I've got a homework assignment and I've got stuck with a part of it.
I need to accept an integer from the user and to print the numbers literally in their right order (left--> right).
An example:
user input : 18427
computer output: one, eight, four, two, seven,
Now, the trick is that i can't use more than 1 loop in each method :| meaning only 1 loop availabe for this part of my program.
Thanks!
- 12-11-2011, 04:06 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 17
Re: Problem with printing numers literally
That sounds like an interesting problem. What have you come up with so far? Are you thinking of working with the int value itself, or the String form of its base-10 representation? (There are lots of arithmetic operations and String methods available, so either approach should be good.)
- 12-11-2011, 05:21 AM #3
Senior Member
- Join Date
- Dec 2011
- Posts
- 102
- Rep Power
- 0
Re: Problem with printing numers literally
well..
if it wasn't for the limit of the loops, i'd go for it.
the real problem i'm facing is separating the digits.. i could try and divide the number to 10 100 1000 etc.. and just taking the number left to the point... but for that to be perfect i'd need to know how long is every integer i'm getting from the user, which is another annoying problem.. so i believe it isn't a "logicaly smart" way at all...
well... that's why i wanted to see if there's any easy and short way for it.. because i'm sure i'm just trying to invent the wheel :\
and yes, about the string.. I thought about it but wasn't comletely sure how to do it or even if it's the right thing.. anyway, if there's a way of doing it without a string i'd like to know..
- 12-11-2011, 06:59 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 17
Re: Problem with printing numers literally
I'm sure you realise that people here are not going to write the code for you. I doubt if the problem is nearly so useful as a wheel, but the point is that you're *supposed* to reinvent it. Doing so, and what you learn from doing so, is the only possible value to be obtained from the exercise.
You have the basic operations - and those of the Math/String classes - to consult. Aim at something that is correct, and then worry about easy and short ways of doing things. In the first instance don't even be too concerned with the "one for loop" requirement: coding any correct implementation may well shed light on the problem.
- 12-12-2011, 04:17 AM #5
Senior Member
- Join Date
- Dec 2011
- Posts
- 102
- Rep Power
- 0
Re: Problem with printing numers literally
So nice of you being cynical.
I haven't requested nothing but a tip for solving this problem... thanks anyway tho...
Now I think i've figured it out, but still having another problem with it.
I've written just a little part of the whole program which involves what i've asked about b4.. tho for some reason it won't start the "for" loop.
here's the method: (it accepts an integer from the main)
public static void printDigits(int BElement) //printDigits method starts - printing numbers literally
{
String convertInt ="";
convertInt = Integer.toString(BElement); //converting integer to string
int length = convertInt.length();
for (int i=0 ; i <= length ; i++) { //oh FFS!! it won't get in the loop~~~!!!
switch (convertInt.charAt(i)) {
case 1: System.out.println ("one, ");
break;
case 2: System.out.println ("two, ");
break;
case 3: System.out.println ("three, ");
break;
case 4: System.out.println ("four, ");
break;
case 5: System.out.println ("five, ");
break;
case 6: System.out.println ("six, ");
break;
case 7: System.out.println ("seven, ");
break;
case 8: System.out.println ("eight, ");
break;
case 9: System.out.println ("nine, ");
}//switch ends
}//for ends
}
I figure it might be a stupid mistake, but I'm new to programming.. so plz try and be gentle o_O
Thanks!Last edited by Mapisto; 12-12-2011 at 04:21 AM.
-
Re: Problem with printing numers literally
I see nothing cynical in his post, nothing at all. If this is how you are going to react to his kindness and helpful suggestions, you may decrease the motivation of others to help you further. Just a friendly comment, but take it as you will. As for me, I'll wait to see if you retract your statement above before offering any suggestions. pbrockway will likely be more generous than me though.
Last edited by Fubarable; 12-12-2011 at 04:23 AM.
- 12-12-2011, 04:29 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 17
Re: Problem with printing numers literally
Java Code:switch (convertInt.charAt(i)) { case 1: System.out.println ("one, "); break;
-----
A separate point is that you have forgotten about the zero digit.
- 12-12-2011, 04:38 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 17
Re: Problem with printing numers literally
pbrockway will likely be more generous than me though.
(How did poor Diogenes and his followers get such a bad reputation that "cynical" should be a term of opprobrium?)
- 12-12-2011, 04:55 AM #9
- 12-12-2011, 11:51 AM #10
Senior Member
- Join Date
- Dec 2011
- Posts
- 102
- Rep Power
- 0
Re: Problem with printing numers literally
I've never meant to insult :| (cynical isn't an insult as far as i'm concern : \ )
And i'm sorry for my former reply, I've just got frustrated for you making me look like a lazy guy who won't try to do anything himself.
- 12-12-2011, 11:52 AM #11
Senior Member
- Join Date
- Dec 2011
- Posts
- 102
- Rep Power
- 0
Re: Problem with printing numers literally
Thanks man
- 12-12-2011, 12:44 PM #12
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
Re: Problem with printing numers literally
Some people approach problems starting with a concrete approximation and then refining it, as suggested by pbrockway2.
Other people, and they can be beginners, are comfortable laying abstract foundations before proceeding.
I was always an abstract thinker, so for those like me, this is an interesting problem..a bit of a trick question..
1. First, I conceptually see a presentation "layer" and a "computational layer" to the code.
Presentation layer: accept input from user and send it to computational layer.
Computational layer: validate the input and bounce it back if it is invalid, otherwise convert it to a useful internal format.
Computational layer: sort internal format.
Computational layer: format internal format ready for display.
Presentation layer: Display external format.
2. The trick in the question is that there will almost certainly be a nested loop somewhere. The trick in the answer is to hide the inner loop in a method call. Even better, is to have the inner loop, or both loops 'ready made' for you n a java class method. pbrockway2's advice is good for those with a concrete approach. They too will naturally reach this realisation.
What internal format is useful? ;-)
Clearly an array of a primitive type that can hold digits and which is used commonly in the Java APIs? ;-)
- 12-12-2011, 07:58 PM #13
Senior Member
- Join Date
- Dec 2011
- Posts
- 102
- Rep Power
- 0
Re: Problem with printing numers literally
It's hard for me to just start writing a program without designing it first in my mind, or on a paper sometimes..
I guess it could be a bad habit.. so I'll try and stick to pbrockway2 way of solving problems from now on
thanks ppl
- 12-12-2011, 11:24 PM #14
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 17
Re: Problem with printing numers literally
You're welcome.
Let's forget "cynical". I took it that you meant to characterise my "I'm sure you realise that..." as being "snide" or "condescending" or something. The simple fact is that I don't know how better to express myself. Often you see on these forums "How do I do X?" and I just consider it the wrong place to start. I want to know "what do you know?" "what have you tried?" "what happened when you tried that?" and my intention (with whatever success!) was to keep the focus on those things in a way that wouldn't threaten or belittle.
You had outlined a numerical approach and I considered that you had to hit your head against that until it hurt and you were became more inclined to consider the possibility (insinuated as a question in #2) that this was a String problem.
I don't think I have an "approach" to computational problems that I would advocate. (And I'm d@mned sure I'm not qualified to have one. And I'm too shrewd to present myself to the forum as if I were thus qualified).
-----
So, did you get a working solution?
I ask because it might place you within reach of something that is truly an "easy and short way". It has already been mentioned that the String class has a method that could replace repeated charAt() calls with a single call that gets all the digits at once. Then there's the approach of using a lookup table that might replace the lengthy switch statement with a one-liner.
- 12-12-2011, 11:53 PM #15
Senior Member
- Join Date
- Dec 2011
- Posts
- 102
- Rep Power
- 0
Similar Threads
-
Printing Problem
By PROPA in forum New To JavaReplies: 7Last Post: 11-03-2011, 03:40 PM -
Problem in printing
By justbeller in forum Java 2DReplies: 0Last Post: 04-26-2011, 03:48 PM -
Printing problem
By justlynn in forum NetBeansReplies: 5Last Post: 08-03-2010, 03:41 PM -
Problem after Printing GUI.
By coldblood22 in forum AWT / SwingReplies: 1Last Post: 04-05-2008, 03:43 PM -
printing problem
By ntpl in forum AWT / SwingReplies: 0Last Post: 11-27-2007, 12:20 PM
Bookmarks