There are many ways to do this. One way (and is safer for any number length) is to convert the integer to string using Integer.toString(int_value);.
int a = 150;
String aString = Integer.toString(a); // now it contains "150"
Then iterate through each character of the string (using for-loop) and display the character using System.out.println().
Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17
Then iterate through each character of the string (using for-loop) and display the character using System.out.println().
or wasn't that clear enough?
You have in your string the integer: like 150 to string is "150". What does that mean?
It means that you transformed your integer into a collection of characters, which you can access individually. How can you access a characters from the String, you may ask? Easy, with the charAt(index) method from the String class.
1st: create a for loop from 0 to the lenght of the string
2nd: access the element
3rd: print the element
method print_char
for i=0 to string.lenght() do
print string.charAt(i)
end_for
end_method
I suggest you take a look at the String class from Java doc: Click Here and
at the charAt(int index) method: Click Here
It would be for the best to read from your coursebook also, because a lot of this things are elementary stuff, and can be easily found in any coursebook.
Lucaci Andrew
Practically a Master Poster
649 posts since Jan 2012
Reputation Points: 91
Solved Threads: 91
Skill Endorsements: 12
for(int i=0; i < string.length; i++){
System.out.println(string.charAt(i));
}
something like that.
Basically it loops through the string and prints each character to the console. Giving:
1
5
0
ObSys
Junior Poster in Training
77 posts since Nov 2011
Reputation Points: 24
Solved Threads: 7
Skill Endorsements: 0
If you read my previous post about safer for any number length, you wouldn't do the way you did. Also in your way, you need to store the number somewhere before you can display because you are working on it backward (from the least significant value). That's why I suggested the string conversion. Your way is not a better way but it is another solution to solve the problem.
Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17
Sorry, but that code makes no sense at all, it just looks like a random collection of assignments. Line 8 will screw up the loop variable, and where did 12 ever come into this problem? I guess you just tried things at random until the output was nearly right for that one test case?
You should re-read the good advice you have been given, and follow it.
JamesCherrill
... trying to help
8,532 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30
Seriously dude, please re-read the previous posts. There's one that gives you a hint, followed by one that has pseudo-code, followed by one that has the actual Java code you need. All you have to do is adapt it into your code.
I'm detecting a hint of panic in your posts, so the first thing is to slow down, take a deep breath, maybe do something else for a few minutes, then settle down and re-read all thoses posts very slowly and carefully. The answers you seek are aready there.
JamesCherrill
... trying to help
8,532 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30