944,022 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 9121
  • Java RSS
Nov 13th, 2007
0

Number sequence

Expand Post »
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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
newbieGirl is offline Offline
19 posts
since Oct 2007
Nov 14th, 2007
0

Re: Number sequence

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Nov 14th, 2007
0

Re: Number sequence

Agree
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Artmann is offline Offline
19 posts
since Nov 2007
Sep 15th, 2009
0

Re: Number sequence

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kingz91 is offline Offline
1 posts
since Aug 2009
Sep 16th, 2009
0

Re: Number sequence

Agree with previous post.
Basically:
-Create an int array lengthed 5 that has the values from 0 to 4
-use for loop:
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 12
Solved Threads: 4
Light Poster
roswell67 is offline Offline
32 posts
since Sep 2009
Sep 16th, 2009
0

Re: Number sequence

Click to Expand / Collapse  Quote originally posted by kingz91 ...
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.
Reputation Points: 12
Solved Threads: 4
Light Poster
roswell67 is offline Offline
32 posts
since Sep 2009
Sep 16th, 2009
0

Re: Number sequence

Java Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
javed123 is offline Offline
14 posts
since Sep 2009
Sep 16th, 2009
0

Re: Number sequence

ok. I have solved your "0" problem

JAVA Syntax (Toggle Plain Text)
  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
Reputation Points: 11
Solved Threads: 4
Junior Poster
akulkarni is offline Offline
111 posts
since Jun 2009
Sep 17th, 2009
0

Re: Number sequence

@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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
javed123 is offline Offline
14 posts
since Sep 2009
Sep 17th, 2009
0

Re: Number sequence

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.
Reputation Points: 11
Solved Threads: 4
Junior Poster
akulkarni is offline Offline
111 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Save a map
Next Thread in Java Forum Timeline: Confirmation Failing





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC