Number sequence

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2007
Posts: 19
Reputation: newbieGirl is an unknown quantity at this point 
Solved Threads: 0
newbieGirl newbieGirl is offline Offline
Newbie Poster

Number sequence

 
0
  #1
Nov 13th, 2007
Ok, I am totally confused - probably because I've been staring at my computer for hours, but I need some guidance (not for homework, just my own personal knowlegde)...

Here's the output I'm trying to achieve:

012343210
001234321
000123432
000012343
000001234

...And I know I need to use a for loop. I would know how to get it to count up to 4, but to then count down from 4, I get confused there (also adding the 0's for each new line, etc.). Anyone have any ideas? It's probably an easy setup, just one detail that's not clicking in my head, but it's just wracking my brain, so I figured someone could rather "tutor" me on this quick. Thanks!
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Number sequence

 
0
  #2
Nov 14th, 2007
Create a single array of characters to print that can print the entire sequence, then create a loop where the loop index is used to determine where in the array to start.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 19
Reputation: Artmann is an unknown quantity at this point 
Solved Threads: 0
Artmann Artmann is offline Offline
Newbie Poster

Re: Number sequence

 
0
  #3
Nov 14th, 2007
Agree
-Signed Artmann
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 1
Reputation: kingz91 is an unknown quantity at this point 
Solved Threads: 0
kingz91 kingz91 is offline Offline
Newbie Poster

Re: Number sequence

 
0
  #4
Sep 15th, 2009
having same problem but slightly differnt.ineed my software to preoduce number inequence like
00000001
00000002
00000003
...
..
00000100
and assign it to a user everytime it registers a new user
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 24
Reputation: roswell67 is an unknown quantity at this point 
Solved Threads: 3
roswell67 roswell67 is offline Offline
Newbie Poster

Re: Number sequence

 
0
  #5
Sep 16th, 2009
Agree with previous post.
Basically:
-Create an int array lengthed 5 that has the values from 0 to 4
-use for loop:
  1. for(int a=0; 0<arrayName.length(); a++)
  2. {
  3. if(count==9)
  4. {
  5. count=0;
  6. System.out.println("");
  7. }
  8. count++;
  9. System.out.print(arrayName[a]);
  10. }
  11.  
  12. if(count==9)
  13. {
  14. count=0;
  15. System.out.println("");
  16. }
  17. count++;
  18. System.out.print("0");
  19.  
  20. for(int a=4; 0=<a; a--)
  21. {
  22. if(count==9)
  23. { count=0;
  24. System.out.println("");
  25. }
  26. count++;
  27. System.out.print(arrayName[a]);
  28. }

Havn't complied this, so double check. But I'm sure it'll work =)!
Goodluck =)
please mark as solved if this its what you were looking for.
Last edited by roswell67; Sep 16th, 2009 at 2:48 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 24
Reputation: roswell67 is an unknown quantity at this point 
Solved Threads: 3
roswell67 roswell67 is offline Offline
Newbie Poster

Re: Number sequence

 
0
  #6
Sep 16th, 2009
Originally Posted by kingz91 View Post
having same problem but slightly differnt.ineed my software to preoduce number inequence like
00000001
00000002
00000003
...
..
00000100
and assign it to a user everytime it registers a new user
mmm create a new thread so you can get exclusive help as its not the same thing.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 14
Reputation: javed123 is an unknown quantity at this point 
Solved Threads: 0
javed123 javed123 is offline Offline
Newbie Poster

Re: Number sequence

 
0
  #7
Sep 16th, 2009
  1. import java.io.Serializable;
  2.  
  3. public class MyObjectToSerialize implements Serializable {
  4.  
  5. private static final long serialVersionUID = 1L;
  6.  
  7. private int autogeneratedNumber;
  8.  
  9. public MyObjectToSerialize(int autogeneratedNumber) {
  10. this.autogeneratedNumber = autogeneratedNumber;
  11. }
  12.  
  13. public String toString() {
  14. return autogeneratedNumber + "";
  15. }
  16.  
  17. public int getAutogeneratedNumber() {
  18. return autogeneratedNumber;
  19. }
  20.  
  21. public void setAutogeneratedNumber(int autogeneratedNumber) {
  22. this.autogeneratedNumber = autogeneratedNumber;
  23. }
  24.  
  25. }
  26.  
  27.  
  28. import java.io.FileInputStream;
  29. import java.io.FileOutputStream;
  30. import java.io.IOException;
  31. import java.io.ObjectInputStream;
  32. import java.io.ObjectOutputStream;
  33. import java.io.Serializable;
  34. import java.text.DecimalFormat;
  35. import java.text.NumberFormat;
  36.  
  37. public class ObjectSerialization {
  38.  
  39. private static void saveObject(Serializable object, String filename)
  40. throws IOException {
  41. ObjectOutputStream objstream = new ObjectOutputStream(
  42. new FileOutputStream(filename));
  43. objstream.writeObject(object);
  44. objstream.close();
  45. }
  46.  
  47. private static Object loadObject(String filename)
  48. throws ClassNotFoundException, IOException {
  49. ObjectInputStream objstream = new ObjectInputStream(
  50. new FileInputStream(filename));
  51. Object object = objstream.readObject();
  52. objstream.close();
  53. return object;
  54. }
  55.  
  56. public static void main(String[] args) {
  57. // MyObjectToSerialize original = new MyObjectToSerialize(0);
  58. try {
  59. // saveObject(original, "C:/serializable.ser");
  60. MyObjectToSerialize loaded = (MyObjectToSerialize) loadObject("C:/serializable.ser");
  61. loaded.setAutogeneratedNumber(loaded.getAutogeneratedNumber() + 1);
  62. saveObject(loaded, "C:/serializable.ser");
  63. System.out.println(String.format("%08d", loaded.getAutogeneratedNumber()));
  64. } catch (Exception e) {
  65. e.printStackTrace();
  66. }
  67. }
  68. }

SNIP
Last edited by happygeek; Sep 30th, 2009 at 5:54 am. Reason: spam deleted
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 99
Reputation: akulkarni is an unknown quantity at this point 
Solved Threads: 4
akulkarni akulkarni is offline Offline
Junior Poster in Training

Re: Number sequence

 
0
  #8
Sep 16th, 2009
ok. I have solved your "0" problem

  1. class newbie
  2. {
  3. public static void main(String args[])
  4. {
  5. int i,j;
  6. for(i=0;i<6;i++)
  7. {
  8. for(j=0;j<i;j++)
  9. {
  10. System.out.print(0);
  11. }
  12. System.out.println();
  13. }
  14. }
  15. }

Now let me see how much u have tried before i give u the solution
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 14
Reputation: javed123 is an unknown quantity at this point 
Solved Threads: 0
javed123 javed123 is offline Offline
Newbie Poster

Re: Number sequence

 
0
  #9
Sep 17th, 2009
@akulkarni

I think instead of using for loops you can use String.format function as below:

System.out.println(String.format("%08d", number));

SNIP
Last edited by happygeek; Sep 30th, 2009 at 5:54 am. Reason: spam deleted
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 99
Reputation: akulkarni is an unknown quantity at this point 
Solved Threads: 4
akulkarni akulkarni is offline Offline
Junior Poster in Training

Re: Number sequence

 
0
  #10
Sep 17th, 2009
ya i did think of using string ; with for i found it tough.i thought i could get with it with for loops but i failed.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3398 | Replies: 9
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC