Solved

Genrating numbers between given ranges

Posted on 2013-01-28
18
196 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Facing this issue for maven proxy setting 2 27
Powershell command 2 42
ejb mdb examples 1 21
How to configure empty element in XML Document parser? 15 44
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

733 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