spacing integers using modulous


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: spacing integers using modulous

  1. #1
    Join Date
    Jun 2004
    Posts
    24

    Smile spacing integers using modulous

    Hi what i want to do is take a string of 1 five digit number and turn it into a integers. I want to print them out but with 3 spaces in between the numbers. How do you do that using modulous? thanks

  2. #2
    Join Date
    May 2004
    Posts
    26
    I have no clue what you are looking for.

    You want to convert a String to int?? Or break-up a String into single characters, each one an int?

  3. #3
    Join Date
    May 2004
    Posts
    26

    Talking

    Just type the input String, and what you want the output to look like

  4. #4
    Join Date
    Jun 2004
    Posts
    24
    I already have them as single ints using division, or should i use mod? But anyway i figured out how to print the spaces, i guess i just print the 3 spaces

  5. #5
    Join Date
    Feb 2004
    Posts
    808
    modulus has nothing to do with it:

    Code:
    for(int i=0; i< myString.length(); i++){
      System.out.print(myString.charAt(i)+"   ");
    }
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  6. #6
    Join Date
    Jun 2004
    Posts
    24
    Hey guys, well i tired that but thats not what i need, i need to use modulous now to seperate the numbers, how do i seperae the integers with modulous? thanks

  7. #7
    Join Date
    Jun 2004
    Posts
    24
    ok here i did this, but how do i get this in one msg box instead of 5
    while(x>0)
    {JOptionPane.showMessageDialog(null, "digits" + x%10 + " ");

    x=x/10;
    }

  8. #8
    Join Date
    May 2004
    Posts
    219
    I think we're all confused as to what you are trying to do exactly, and why you would use modulus for it. The code cjard posted is the solution to what I interpreted your problem to be. You might as well just paste in the part of your assignment specification that talks about this, since it's pretty obvious by now that this is some type of homework

    Edit: Ohhh I see what you're doing. You don't want any number to be above 9? Anyway, put the message box call outside of the while loop, and create a string that appends the int + a space to the end like so:
    Code:
    String s = new String();
    while(x > 0)
    { 
         s += x%10 + " ";
         ...
    }
    // Message Box call
    Edit2: String instantiation before loop

    Edit3: Ah. I get what you were doing now. When you mod a number like 124 by 10, you get a remainder of 4, then divide it by 10, and you have 12 and so on, so the output is each digit in order from the lowest 'place' ("124" comes out as "4 2 1").

  9. #9
    Join Date
    Jun 2004
    Posts
    24
    import javax.swing.JOptionPane; // Imports javax.swing.JOptionPane (user input and output)

    public class TEST {
    public static void main( String args[] )
    {
    String num1;

    int x;



    num1 = JOptionPane.showInputDialog( "Enter Number consisting of 5 digits:" );

    x = Integer.parseInt( num1 );



    // does calc
    String s = new String();
    while(x > 0)
    {
    s += x%10 + " ";

    }


    JOptionPane.showMessageDialog(null, s);





    // exits program
    System.exit ( 0 );
    }
    }

    ok this is not working for me...do you know why

  10. #10
    Join Date
    May 2004
    Posts
    219
    Uh, because you aren't doing the division? x/10 like in your original code.

  11. #11
    Join Date
    Jun 2004
    Posts
    24

    Unhappy

    I know im using mod to seperate the numbers, can anyone correct this code please?!

    thanks

  12. #12
    Join Date
    May 2004
    Posts
    219
    Originally posted by firewire
    I know im using mod to seperate the numbers, can anyone correct this code please?!
    Originally posted by Drain
    Uh, because you aren't doing the division? x/10 like in your original code.

  13. #13
    Join Date
    Jun 2004
    Posts
    24
    Hey whats up, thanks for sponding. But thats just it, i need to use modulous and and im not sure how to turn it back into a string again and then print it when im out of the loop. Can you show me?

    Thanks a lot.

  14. #14
    Join Date
    May 2004
    Posts
    219
    Alright, I'm tired of this thread, so here you go:
    Code:
    import javax.swing.JOptionPane;
    
    public class TEST
    {
    	public static void main( String args[] )
    	{
    		String num1 = JOptionPane.showInputDialog(
    			"Enter Number consisting of 5 digits:" );
    		int x = Integer.parseInt( num1 );
    
    		String s = new String();
    		while(x > 0)
    		{
    			s = x%10 + " " + s;
    			x /= 10;
    		}
    
    		JOptionPane.showMessageDialog(null, s);
    	}
    }
    I basically told you the answer before, but whatever.

  15. #15
    Join Date
    Jun 2004
    Posts
    24
    hey man thanks, it was small mistakes but thanks for clearing it up, i really appreciate it and sorry for my confusion, you helped out a lot, so thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center