954,228 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Number sequence

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! :)

newbieGirl
Newbie Poster
19 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

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.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

Agree

Artmann
Newbie Poster
19 posts since Nov 2007
Reputation Points: 10
Solved Threads: 1
 

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

kingz91
Newbie Poster
1 post since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

Agree with previous post.
Basically:
-Create an int array lengthed 5 that has the values from 0 to 4
-use for loop:

for(int a=0; 0<arrayName.length(); a++)
{
   if(count==9)
    {
       count=0;
       System.out.println("");
     }
   count++;
   System.out.print(arrayName[a]);
}

if(count==9)
{
   count=0;
   System.out.println("");
}
count++;
System.out.print("0");

for(int a=4; 0=<a; a--)
{
   if(count==9)
   {  count=0;
       System.out.println("");
   }
   count++;
   System.out.print(arrayName[a]);
}


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.

roswell67
Light Poster
32 posts since Sep 2009
Reputation Points: 12
Solved Threads: 4
 
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.

roswell67
Light Poster
32 posts since Sep 2009
Reputation Points: 12
Solved Threads: 4
 
import java.io.Serializable;

public class MyObjectToSerialize implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private int autogeneratedNumber;

	public MyObjectToSerialize(int autogeneratedNumber) {
		this.autogeneratedNumber = autogeneratedNumber;
	}

	public String toString() {
		return autogeneratedNumber + "";
	}

	public int getAutogeneratedNumber() {
		return autogeneratedNumber;
	}

	public void setAutogeneratedNumber(int autogeneratedNumber) {
		this.autogeneratedNumber = autogeneratedNumber;
	}

}


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class ObjectSerialization {

	private static void saveObject(Serializable object, String filename)
			throws IOException {
		ObjectOutputStream objstream = new ObjectOutputStream(
				new FileOutputStream(filename));
		objstream.writeObject(object);
		objstream.close();
	}

	private static Object loadObject(String filename)
			throws ClassNotFoundException, IOException {
		ObjectInputStream objstream = new ObjectInputStream(
				new FileInputStream(filename));
		Object object = objstream.readObject();
		objstream.close();
		return object;
	}

	public static void main(String[] args) {
		// MyObjectToSerialize original = new MyObjectToSerialize(0);
		try {
			// saveObject(original, "C:/serializable.ser");
			MyObjectToSerialize loaded = (MyObjectToSerialize) loadObject("C:/serializable.ser");
			loaded.setAutogeneratedNumber(loaded.getAutogeneratedNumber() + 1);
			saveObject(loaded, "C:/serializable.ser");
			System.out.println(String.format("%08d", loaded.getAutogeneratedNumber()));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


SNIP

javed123
Newbie Poster
14 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

ok. I have solved your "0" problem

class newbie
{
  public static void main(String args[])
 {
 int i,j;
 for(i=0;i<6;i++)
 {
  for(j=0;j<i;j++)
  {
  System.out.print(0);
  }
System.out.println();
  }
 }
}


Now let me see how much u have tried before i give u the solution

akulkarni
Junior Poster
111 posts since Jun 2009
Reputation Points: 11
Solved Threads: 4
 

@akulkarni

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

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

SNIP

javed123
Newbie Poster
14 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

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.

akulkarni
Junior Poster
111 posts since Jun 2009
Reputation Points: 11
Solved Threads: 4
 
int z=0;       
   for(int x=1; x<=5; x++){
       for(int y=1; y<=x; y++){
           System.out.print("0");
       }
       for(int a=1; a<=4; a++){
           if(a==4){
                   for(int y=a; y>=z; y--){
                       System.out.print(y);
                   }
                   z++;
           }else{
               System.out.print(a);
           }
       }
       System.out.println();
   }
SoftEngr
Newbie Poster
1 post since May 2012
Reputation Points: 0
Solved Threads: 0
 

Post: Markdown Syntax: Formatting Help
You