We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,473 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

how do i convert this from horizontal to vertical?

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.

6
Contributors
10
Replies
4 Days
Discussion Span
6 Months Ago
Last Updated
11
Views
forjustincase
Newbie Poster
23 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

and then what after this??

forjustincase
Newbie Poster
23 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

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

forjustincase
Newbie Poster
23 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
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

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

jalpesh_007
Posting Whiz
337 posts since Sep 2010
Reputation Points: 4
Solved Threads: 36
Skill Endorsements: 4

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.

forjustincase
Newbie Poster
23 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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
Moderator
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
Moderator
8,532 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0924 seconds using 2.77MB