Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

code problem

 
ten doeschate
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to reverse the output ie 5 4 3 2 1 to 1 2 3 4 5 how to do it
my code is
 
Mark Beardsley
Ranch Hand
Posts: 32
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Must admit that I would convert the value into a String and then simply use Java's String handling capabilities to reverse the order of the characters.
 
Tony Docherty
Bartender
Posts: 2957
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to reverse the output ie 5 4 3 2 1 to 1 2 3 4 5 how to do it

Sorry but what part are you stuck on? your code would appear to do what you want.
 
Ishan Pandya
Ranch Hand
Posts: 226
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the output you are getting?? and what is the problem?? could you please mention that?
 
ten doeschate
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getting output as 5 4 3 2 1

but i want 1 2 3 4 5

not able to reverse order from the above code
 
Tony Docherty
Bartender
Posts: 2957
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But your seed number is 12345 so it will reverse to 54321. If you want an output of 12345 you will need to start with 54321.
 
Prajakta Acharya
Ranch Hand
Posts: 138
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whats wrong? The input considered is 12345 whose reverse is 54321?

Am i missing something?
 
ten doeschate
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but i want to print 1 2 3 4 5
 
Tony Docherty
Bartender
Posts: 2957
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've already told you what to do but if you don't understand what I said think about the problem yourself. If you have code that reverses a number ie changes 6789 to 9876 and if you want a specific output such as 12345 what number do you have to start with so when it is reversed it will be 12345.
 
Ishan Pandya
Ranch Hand
Posts: 226
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why dont you just print i before the while loop..you will get your output.
@tony- i dont think he is getting you.
 
mykodes java
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ten doeschate,

Use this code.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic