Solved

Genrating numbers between given ranges

Posted on 2013-01-28
18
191 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
Comment Utility
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
Comment Utility
if I put System.out.* and give the yourRandomNumber it will out put it in screen?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
0
 
LVL 16

Expert Comment

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

Author Comment

by:hi4ppl
Comment Utility
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
Comment Utility
for( int i = 10; i <=20; ++i ){
        System.out.println(i);
    }
0
 
LVL 1

Author Comment

by:hi4ppl
Comment Utility
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
Comment Utility

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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
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
Comment Utility
Just use printf on your PrintWriter, having set the constant value before the loop starts
0
 
LVL 1

Author Comment

by:hi4ppl
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry, see StringBuilder.insertCharAt
0
 
LVL 1

Author Comment

by:hi4ppl
Comment Utility
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
Comment Utility
:) Not thinking clearly today ;)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

744 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now