• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reversing user input

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, all. Im new to java and only a few weeks into my class. I have two problems.  One, is Im not sure how to put spaces between my reversed numbers. The second is I don't have the furthest clue how to display only the even numbers from the user input.

Ex.
User input: 45789
Display should show number reversed and spaced: 9 8 7 5 4



Ex.2
User input: 45789
Display should show only even numbers equally spaced: 4  8

Any help would be appreciative. Or if you could just point me on the right track. Like i said, I am new to this.

Thank you.

 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the ranch.

Printing the reversed number with spaces between the digits can be done in a number of ways. A relatively simple way would be to change your code so instead of adding each digit to the 'reverse' variable you print the digit and then a space.
 
Johnny Graves
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there anyway you could give me an example. Thanks
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Will print the digits 0 to 9 with a space between each digit.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An alternative approach is to change the 'reverse' variable's type to a String and concatenate each digit to the string along with a space char.
 
Johnny Graves
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've seen the first method before. I read on another page about the second method you suggested. In your honest opinion. Which would be easier for noob like me?
 
Marshal
Posts: 79392
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:An alternative approach is to change the 'reverse' variable's type to a String and concatenate each digit to the string along with a space char.

I would use a StringBuilder and insert the numbers at its beginning.

That will permit faster execution and teach you the StringBuilder's methods. Use myStringBulder.toString() to convert it back to a String.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Johnny Graves wrote:I've seen the first method before. I read on another page about the second method you suggested. In your honest opinion. Which would be easier for noob like me?


I suggest you try both methods, they should both be within your grasp. Then try the method Campbell suggested which is a little more involved but is a good solution. Remember, learning to program requires lots of practice so take the opportunity to code several solutions - they will all teach you something.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to teach you probably the single most important lesson you can learn about programming, and the sooner you embrace it, the better.

Your subject tells me you are not thinking about this correctly at all.  You shouldn't ever think about "reversing user input". Instead, you should think about "getting user input", and "reversing a string".  These are two completely independent tasks.  So you should write a method that does each of these, then combine them in your larger program.

That way, if next week your teacher says "reverse a string from a database", you're already half done - you can re-use the "reverse" part, and only focus on the "get a string from a database part".
 
reply
    Bookmark Topic Watch Topic
  • New Topic