Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.BufferedWriter;
- import java.io.FileWriter;
- import java.io.IOException;
- import javax.swing.JOptionPane;
- public class Controller{
- private View interfata;
- public Controller(View interfata){
- this.interfata = interfata;
- interfata.getBtnDoIt().addActionListener(new DoAction());
- }
- class DoAction implements ActionListener{
- public void actionPerformed(ActionEvent arg0) {
- if(arg0.getSource() == interfata.getBtnDoIt()){
- String from1 = interfata.getFrom1().getText();
- String from2 = interfata.getFrom2().getText();
- String from3 = interfata.getFrom3().getText();
- String from4 = interfata.getFrom4().getText();
- String to1 = interfata.getTo1().getText();
- String to2 = interfata.getTo2().getText();
- String to3 = interfata.getTo3().getText();
- String to4 = interfata.getTo4().getText();
- if(isValid(from1) && isValid(from2) && isValid(from3) && isValid(from4) && isValid(to1) && isValid(to2) && isValid(to3) && isValid(to4)){
- boolean cont=true;
- String temp;
- int ifrom1 = Integer.parseInt(from1);
- int ifrom2 = Integer.parseInt(from2);
- int ifrom3 = Integer.parseInt(from3);
- int ifrom4 = Integer.parseInt(from4);
- int ito1 = Integer.parseInt(to1);
- int ito2 = Integer.parseInt(to2);
- int ito3 = Integer.parseInt(to3);
- int ito4 = Integer.parseInt(to4);
- if (correctInput(ifrom1,ifrom2,ifrom3,ifrom4,ito1,ito2,ito3,ito4)){
- try {
- BufferedWriter out = new BufferedWriter(new FileWriter("result.txt"));
- while(cont) {
- temp = ifrom1+"."+ifrom2+"."+ifrom3+"."+ifrom4+"\n";
- out.write(temp);
- ifrom4++;
- if(ifrom4==256){
- ifrom4=0;
- ifrom3++;
- }
- if (ifrom3==256){
- ifrom3=0;
- ifrom2++;
- }
- if (ifrom2==256){
- ifrom2=0;
- ifrom1++;
- }
- if( (ifrom1 == ito1) && (ifrom2 == ito2) && (ifrom3 == ito3) && (ifrom4 == ito4) ) {
- temp = ifrom1+"."+ifrom2+"."+ifrom3+"."+ifrom4+"\n";
- out.write(temp);
- cont = false;
- }
- }
- out.close();
- } catch (IOException e) {
- }
- }
- }else JOptionPane.showMessageDialog(null, "Invalid input", "Error", JOptionPane.ERROR_MESSAGE);
- interfata.setLblstatus("Done!");
- }
- }
- }
- public boolean correctInput(int ifrom1, int ifrom2, int ifrom3, int ifrom4, int ito1, int ito2, int ito3, int ito4){
- boolean check=true;
- if(ifrom1 <= ito1){
- if(ifrom1 == ito1){
- if (ifrom2 <= ito2){
- if(ifrom2 == ito2){
- if(ifrom3 <= ito3){
- if(ifrom3 == ito3){
- if(ifrom4 <= ito4){
- }else {
- JOptionPane.showMessageDialog(null, "Please verify the IP range!", "Invalid range", JOptionPane.ERROR_MESSAGE);
- check = false;
- }
- }
- }else {
- JOptionPane.showMessageDialog(null, "Please verify the IP range!", "Invalid range", JOptionPane.ERROR_MESSAGE);
- check = false;
- }
- }
- }else {
- JOptionPane.showMessageDialog(null, "Please verify the IP range!", "Invalid range", JOptionPane.ERROR_MESSAGE);
- check = false;
- }
- }
- }else {
- JOptionPane.showMessageDialog(null, "Please verify the IP range!", "Invalid range", JOptionPane.ERROR_MESSAGE);
- check = false;
- }
- return check;
- }
- public boolean isValid(String arg){
- int test;
- try{
- test = Integer.parseInt(arg);
- }
- catch (NumberFormatException ex){
- return false;
- }
- if (test < 0 || test > 255) return false;
- else return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement