ok here's what i am trying to do. i ask user for a value or just assign a value my self for example int a=150;
now i want the output like this:
so is this possible?? please use simple programming i guess it can be done with for loop. i don't understand advance java so please be simple and specific.
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
for i=0 to string.lenght() do
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.
i found a better way for this... i just divided the number by 10 and then took its modulus.. as i have declared an integer it gives only integer value and then i just go to next line for next number :) hope this helps... and yeah i used a for loop as well.. make programming easy :) happy coding..
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.
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.
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.