Results 1 to 15 of 15
Like Tree4Likes
  • 2 Post By pbrockway2
  • 1 Post By pbrockway2
  • 1 Post By pbrockway2

Thread: Problem with printing numers literally

  1. #1
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default 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!

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.)

  3. #3
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default 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..

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.
    DarrylBurke and Fubarable like this.

  5. #5
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default 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 03:21 AM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Problem with printing numers literally

    Quote Originally Posted by Mapisto View Post
    So nice of you being cynical.
    I haven't requested nothing but a tip for solving this problem... thanks anyway tho...
    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 03:23 AM.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Problem with printing numers literally

    Java Code:
    switch (convertInt.charAt(i)) {
        case 1: System.out.println ("one, ");
        break;
    charAt() will return you a char value, but you are comparing it with the int values 1,2,3,... '1'!=1.

    -----

    A separate point is that you have forgotten about the zero digit.
    Mapisto likes this.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Problem with printing numers literally

    pbrockway will likely be more generous than me though.
    Of course! Because I'm cynical...

    (How did poor Diogenes and his followers get such a bad reputation that "cynical" should be a term of opprobrium?)

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Problem with printing numers literally

    Quote Originally Posted by pbrockway2 View Post
    (How did poor Diogenes and his followers get such a bad reputation that "cynical" should be a term of opprobrium?)
    I don't know, it's all Greek to me!

  10. #10
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default 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.

  11. #11
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: Problem with printing numers literally

    Thanks man

  12. #12
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default 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? ;-)

  13. #13
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default 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

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Problem with printing numers literally

    Quote Originally Posted by Mapisto View Post
    Thanks man
    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.
    Fubarable likes this.

  15. #15
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: Problem with printing numers literally

    Yea, I've got it right now, after your last comment about changing the numbers from int values it works fine.
    Now i've got some other problems with the program, but I can deal with them alone I believe..

    tnx again m8.

Similar Threads

  1. Printing Problem
    By PROPA in forum New To Java
    Replies: 7
    Last Post: 11-03-2011, 02:40 PM
  2. Problem in printing
    By justbeller in forum Java 2D
    Replies: 0
    Last Post: 04-26-2011, 02:48 PM
  3. Printing problem
    By justlynn in forum NetBeans
    Replies: 5
    Last Post: 08-03-2010, 02:41 PM
  4. Problem after Printing GUI.
    By coldblood22 in forum AWT / Swing
    Replies: 1
    Last Post: 04-05-2008, 02:43 PM
  5. printing problem
    By ntpl in forum AWT / Swing
    Replies: 0
    Last Post: 11-27-2007, 11:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •