well if you are here then i’m sure you have search the whole web to get single answer about how to generate sample generator where you give the first and second number and between that it generate a series of numbers that are between these two numbers so before I write this post I have look a lot of places but there is many part in everywhere so I decided to create a sample range generator that it will generate series of numbers between those numbers so here i’m sharing what I have created so you can either learn how to create it or download the JAR file and run the program.

[java highlight=”8,9″]
package com.dbdiary.compare;
import java.io.*;

public class generateNum {

public static void main(String[] args) throws IOException {

InputStreamReader istream = new InputStreamReader(System.in) ;
BufferedReader bufRead = new BufferedReader(istream) ;
System.out.println("***************************");
System.out.println("AutoMatic Range Generator);
System.out.println("Developed by Omer NIAH");
System.out.println("***************************");
[/java]

The above code will create a new bufRead and Input Stream where it will ask the value from the user by value what I mean is the digit number the start and the end of the number latter on the other part of this post you will see where it asked the user input by try and catch block.

[java firstline=”13″ highlight=”17,21,26″]
try {

System.out.println("Please Give me first number: ");
String firV = bufRead.readLine();
long aCatch = Long.parseLong(firV);

System.out.println("Please Give me Second number: ");
String secV = bufRead.readLine();
long bCatch = Long.parseLong(secV);

System.out.println("Give the Path to Save the file");
System.out.println("If its windows then D://test.txt");
System.out.println("If its linux/Mac then Users/test.txt");
String fileLocation = bufRead.readLine();

long aNumber = aCatch;
long bNumber = bCatch;
String locFile =(String) fileLocation;

[/java]

Now as you see in the above code what in this part I’m doing is creating two long and one String variable where the user will be asked to give their input in the long variable I will store nothing but the number will be given to the program by the user, and the reason why I used the long is because I wanted this program to be flexible and can do all sort of generator no matter how big or small the number would be.

[java firstline=”30″ highlight=”30,32,33,34,35,36,37″]

BufferedWriter write =
new BufferedWriter(new FileWriter(locFile));

String filename =locFile;
try {
PrintWriter outputStream = new PrintWriter(filename);
for( long i = aNumber; i <=bNumber; ++i ){

outputStream.println(i);
}
System.out.println("The Random Number is Generated to");
System.out.println(locFile);
System.out.println("====================================");
System.out.println("For error contact omer.niah@gmail.com");
System.out.println("====================================");
outputStream.close();
write.close();

} catch (FileNotFoundException e) {

e.printStackTrace();
}
}catch (FileNotFoundException e) {
e.printStackTrace();

}
}
}
[/java]

now finally in this part of the program it will generate the values and then out put the files where ever you give the path to program this will work both for windows and mac and even Linux so that is why I have included a System out put where the user can input the path on the system they run this program, I hope this helped you to get your Range generator up and running if you have any question then let me know by commenting and I will be glad to explain and for those who don’t want to go for the hassle of creating this on their own then I have included the JAR file where you download and run the program and it will does the job for you without even doing anything just download and run it.

Bellow is all code at one place where you can check them out all at one place.

NumberrGenerator

[java collapse=”true”]
package com.dbdiary.compare;
import java.io.*;

public class generateNum {

public static void main(String[] args) throws IOException {

InputStreamReader istream = new InputStreamReader(System.in) ;
BufferedReader bufRead = new BufferedReader(istream) ;
System.out.println("***************************");
System.out.println("AutoMatic Range Generator");
System.out.println("Developed by Omer NIAH");
System.out.println("***************************");

try {

System.out.println("Please Give me first number: ");
String firV = bufRead.readLine();
long aCatch = Long.parseLong(firV);

System.out.println("Please Give me Second number: ");
String secV = bufRead.readLine();
long bCatch = Long.parseLong(secV);

System.out.println("Give Me Denomination");
String dEnomination = bufRead.readLine();

System.out.println("Give me Date");
String dDate = bufRead.readLine();

System.out.println("Give the Path to Save the file");
System.out.println("If its windows then D://test.txt");
System.out.println("If its linux/Mac then Users/test.txt");
String fileLocation = bufRead.readLine();

long aNumber = aCatch;
long bNumber = bCatch;
String locFile =(String) fileLocation;

BufferedWriter write = new BufferedWriter(new FileWriter(locFile));

String filename =locFile;
try {
PrintWriter outputStream = new PrintWriter(filename);
for( long i = aNumber; i <=bNumber; ++i ){

outputStream.println(i);
}
System.out.println("The Random Number is Generated to");
System.out.println(locFile);
System.out.println("========================================");
System.out.println("For error contact omer.niah@gmail.com");
System.out.println("=========================================");
outputStream.close();
write.close();

} catch (FileNotFoundException e) {

e.printStackTrace();
}
}catch (FileNotFoundException e) {
e.printStackTrace();

}

}

}

[/java]