Solved

Genrating numbers between given ranges

Posted on 2013-01-28
18
195 Views
Last Modified: 2013-01-29
Hi, I have range of numbers i.e 93265033060000 and 93265033109999  so what I want is to generate numbers between start and end of above number, and I want this to be output to a text file and the number should be asked dynamically or taken form a file in some root .

thanks for help
0
Comment
Question by:hi4ppl
  • 8
  • 6
  • 2
  • +1
18 Comments
 
LVL 16

Expert Comment

by:Valeri
ID: 38826559
int range = 93265033109999 - 93265033060000;
long yourRandomNumber = 93265033060000 + (int)(Math.random()*range);

and read this to write to file:
http://www.abbeyworkshop.com/howto/java/writeText/
0
 
LVL 1

Author Comment

by:hi4ppl
ID: 38826602
if I put System.out.* and give the yourRandomNumber it will out put it in screen?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38826695
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 16

Expert Comment

by:Valeri
ID: 38826725
yes, output it in this way :
System.out.println("random number: " + yourRandomNumber);
0
 
LVL 1

Author Comment

by:hi4ppl
ID: 38826980
But what it does is it generate one number in between them and what I want is generate all number between them:

i.e between 10-20 I want to get all number like:

10
11
12
13
14
etc till it reach 20

thanks
0
 
LVL 84

Expert Comment

by:ozo
ID: 38827049
for( int i = 10; i <=20; ++i ){
        System.out.println(i);
    }
0
 
LVL 1

Author Comment

by:hi4ppl
ID: 38827197
Hi, thanks for replay when I do that it's not coming in sequence running the query for bellow:

		for( long i = 93265033060000L; i <=93265033109990L; ++i ){
	        System.out.println(i);
	    }

Open in new window


but what I want is like bellow:
93265033060000
93265033060001
93265033060002
93265033060003
etc
till it reach bellow number which is around 49999 numbers
93265033109999

thanks for the help
0
 
LVL 84

Expert Comment

by:ozo
ID: 38827349

but what I want is like bellow:
93265033060000
93265033060001
93265033060002
93265033060003
etc
till it reach bellow number which is around 49999 numbers
93265033109999
That's what I get when I run it, except that it stops at 93265033109990
 not 93265033109999, according to the  i <=93265033109990L condition.

What do you get when you run it?
0
 
LVL 1

Author Comment

by:hi4ppl
ID: 38827500
Hi thanks for replay here is what I did for outputting to txt file and it output only one number can you check what am I missing here?

		for( long i = 93265033060000L; i <=93265033109990L; ++i ){
	       // System.out.println(i);
	  

	     BufferedWriter write = new BufferedWriter(new FileWriter("D:log.txt"));
			  
			String filename =  ("D:Log.txt");
			try {
		      PrintWriter outputStream = new PrintWriter(filename);
		      outputStream.println(i);
		      outputStream.close();
		      write.close();
				
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38827517
You need to open the Writer before the loop and close it after. Ignore my first comment - i too thought you meant random numbers
0
 
LVL 1

Author Comment

by:hi4ppl
ID: 38828419
Thanks for support it worked for me, now I get something like this

93265033060000
93265033060001
93265033060002
93265033060003

what if I want to append two fields with each line like I want to get something like this I can handle the buffer things it get input from user what I want this to loop the value I give to the program and it give me out like bellow:

5|93265033060000|01/28/2013
5|93265033060001|01/28/2013
5|93265033060002|01/28/2013
5|93265033060003|01/28/2013

the int in the left and the date on the right is static it will not add up just static value will be asked from user and then add that value to each number till it ends the loop. I can create the input stuff but how to loop that I can't think of any way to add that static on the loop value  :(

thanks for help
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38828449
Just use printf on your PrintWriter, having set the constant value before the loop starts
0
 
LVL 1

Author Comment

by:hi4ppl
ID: 38829993
And one last question, now I manage to take the output right but I have one static value to input to the loop itself i.e I get normal loop like bellow:

93265033060000
93265033060001
93265033060002
93265033060003

and I want to add one stat value from the right second digit the value is always static exmaple is bellow:

932650330600800
932650330600801
932650330600802
932650330600803

8 will always be static and it should insert this in loop, how to achieve this any example would be appreciate it thanks for the help

This is example of PHP want to duplicate this on JAVA

<?php
for($i = 10000; $i < 10010; $i++){
 $res = substr_replace($i, '7', -1, 0);
 echo $res."<br>";
}
?>

Open in new window

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 310 total points
ID: 38830452
You can do the same sort of thing in Java. Treat your number as a String (with a StringBuilder) and then access it by index

StringBuilder sb = new StringBuilder();
sb.append(number);
sb.setCharAt(sb.length() - 3, '8');
System.out.println(sb);

Open in new window

0
 
LVL 1

Author Comment

by:hi4ppl
ID: 38830640
thanks for replay, but what it does is it replace the character with 8 it goes back 3 character to the loop and replace the 3rd position with 8 and remove the actual number what I want is to add that number not replace it with existing number as your example do
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38830687
Sorry, see StringBuilder.insertCharAt
0
 
LVL 1

Author Comment

by:hi4ppl
ID: 38830699
Hi thanks fix it with the following code

		for (long i = aNum; i < bNum ; i++) {
			temp = i;
			StringBuilder sb = new StringBuilder();
			sb.append(temp);
			sb.insert(sb.length() - 3, '$');
			System.out.println(sb);
			
		}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38830838
:) Not thinking clearly today ;)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hibernate jars 4 45
how to debug htl and js pages 8 39
Eclipse Java import and method not resolved 4 48
Check for Linux process in script 7 47
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question