1,105,344 Community Members

how do i convert this from horizontal to vertical?

Member Avatar
forjustincase
Newbie Poster
23 posts since Aug 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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:
1
5
0
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.

Member Avatar
Taywin
Posting Maven
2,632 posts since Apr 2010
Reputation Points: 134 [?]
Q&As Helped to Solve: 378 [?]
Skill Endorsements: 17 [?]
 
1
 

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

Member Avatar
forjustincase
Newbie Poster
23 posts since Aug 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

and then what after this??

Member Avatar
Lucaci Andrew
Master Poster
746 posts since Jan 2012
Reputation Points: 128 [?]
Q&As Helped to Solve: 116 [?]
Skill Endorsements: 18 [?]
 
1
 

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.

Member Avatar
ObSys
Junior Poster
136 posts since Nov 2011
Reputation Points: 56 [?]
Q&As Helped to Solve: 13 [?]
Skill Endorsements: 1 [?]
 
0
 
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

Member Avatar
forjustincase
Newbie Poster
23 posts since Aug 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Taywin
Posting Maven
2,632 posts since Apr 2010
Reputation Points: 134 [?]
Q&As Helped to Solve: 378 [?]
Skill Endorsements: 17 [?]
 
2
 

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.

Member Avatar
jalpesh_007
Posting Whiz
385 posts since Sep 2010
Reputation Points: -3 [?]
Q&As Helped to Solve: 43 [?]
Skill Endorsements: 6 [?]
 
0
 

AS Taywin has given you a good solution.To convert int to String then print it character wise.

Member Avatar
forjustincase
Newbie Poster
23 posts since Aug 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

thts how i did it

public class New
{
    public static void main (String[] args)
    {
        int a=130,b,c,d;
        for (b=0; b<=10; b++)
        {
            b=a/10;
            c=b%12;
            d=b%10;
            System.out.println(c+"\n"+d+"\n");
        }
    }
}

now how do i use loop to control this??? plus its giving me 3 and 0 but not 1.

Member Avatar
JamesCherrill
... trying to help
10,366 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,750 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
1
 

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.

Member Avatar
JamesCherrill
... trying to help
10,366 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,750 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
1
 

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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: