Solved

java ip class validator

Posted on 2011-03-03
4
641 Views
Last Modified: 2012-05-11
Hello experts,

im working on a small project in which i need to make java program that will validate ip classes.
for example, 2.2.2.2 is a class a, and 192.168.1.1 is a class c.

ive been working on it for days, and for some reason im not getting it to work..im using if else statements.

i also need a simple gui that will be linked to the logic part.

i know that in order to get the fields to transfer to the gui part in need something like this in the logic:
this.a = a;
this.b = b;
this.c = c;
this.d = d;


and the following in the gui, but i dont know to bring the two together!

int a = Integer.parseInt(fielda.getText());
int b = Integer.parseInt(fieldb.getText());
int c = Integer.parseInt(fieldc.getText());
int d = Integer.parseInt(fieldd.getText());

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package assignment1;

import java.util.Scanner;

/**
 *
 * @author
 */
public class IpValidator {

    private Scanner InputIp;
    private String IpAddress;
}
public IpValidator(int a, int b, int c, int d){
        if (a >= 1 && a <= 126 && b >= 0
                && b <= 255 && c >= 0 && c <= 255
                && d >= 0 && d <= 255) {
            return 0; //returns class a
        } else {
            if (a >= 128 && a <= 191 && b >= 0
                    && b <= 255 && c >= 0 && c <= 255
                    && d >= 0 && d <= 255) {
                return 1; //returns class b


            } else {
                if (a >= 192 && a <= 223 && b >= 0
                        && b <= 255 && c >= 0 && c <= 255
                        && d >= 0 && d <= 255) {
                    return 2; //returns class c

                }
            }
        }
    }
}

Open in new window

0
Comment
Question by:pk24573
[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
  • 2
  • 2
4 Comments
 
LVL 13

Expert Comment

by:darren-w-
ID: 35026944
The code seems to work fine, re factored it thought...
package ipchecker;

import java.util.Scanner;
public class IPValidator {

    private int a, b, c, d;

    public IPValidator(int vala, int valb, int valc, int vald) {
        a = vala;
        b = valb;
        c = valc;
        d = vald;
    }

    public String Checktype() {
        String type = "";
        if (isClassA()) {
            type += "a";
        }
        if (isClassB()) {
            type += "b";
        }
        if (isClassC()) {
            type += "c";
        }
        return type;


    }

    private boolean isClassA() {
        return (a >= 1 && a <= 126 && b >= 0
                && b <= 255 && c >= 0 && c <= 255
                && d >= 0 && d <= 255) ? true : false;
    }

    private boolean isClassB() {
        return (a >= 128 && a <= 191 && b >= 0
                && b <= 255 && c >= 0 && c <= 255
                && d >= 0 && d <= 255) ? true : false;
    }

    private boolean isClassC() {
        return (a >= 192 && a <= 223 && b >= 0
                && b <= 255 && c >= 0 && c <= 255
                && d >= 0 && d <= 255) ? true : false;
    }
}

Open in new window

MAIN CLASS:

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        IPValidator a = new IPValidator(6, 6, 6, 6);
            System.out.println(a.Checktype());
    }

}

Open in new window

0
 
LVL 1

Author Comment

by:pk24573
ID: 35027055
refactoring did help,
but i dont understand the gui part...
it wouldve helped had i posted it earlier

this is where i think i have to insert the

int a = Integer.parseInt(fielda.getText());
int b = Integer.parseInt(fieldb.getText());
int c = Integer.parseInt(fieldc.getText());
int d = Integer.parseInt(fieldd.getText());

right?
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package assignment1;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @
 */
public class ipvalidatorui extends JFrame implements ActionListener {

    private JTextField fielda;
    private JTextField fieldb;
    private JTextField fieldc;
    private JTextField fieldd;

    public ipvalidatorui() {
        setSize(400, 400);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        fielda = new JTextField();
        fieldb = new JTextField();
        fieldc = new JTextField();
        fieldd = new JTextField();

        add(fielda, BorderLayout.NORTH);
        add(fieldb, BorderLayout.SOUTH);
        add(fieldc, BorderLayout.WEST);
        add(fieldd, BorderLayout.EAST);

        JButton btn = new JButton("Check IP Validity");
        btn.addActionListener(this);
        add(btn, BorderLayout.NORTH);

        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        String p = field.getIP();
        ipvalidator IpAddress = new ipvalidator(IpAddress);
        int result = IpAddress = IpAddress.validate();

        switch (result) {
            case 0:
                JOptionPane.showMessageDialog(this, "This is a class A address");
                break;
            case 1:
                JOptionPane.showMessageDialog(this, "This is a class B address");
                break;
            case 2:
                JOptionPane.showMessageDialog(this, "This is a class C address");
                break;
        }
    }

    public static void main(String[] args) {
        ipvalidatorui ipvui = new ipvalidatorui();
    }
}

}

}

Open in new window

0
 
LVL 13

Accepted Solution

by:
darren-w- earned 500 total points
ID: 35027480
You will need to work the rest out :)

package ipchecker;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @
 */
public class IPvalidui extends JFrame implements ActionListener {

    private JTextField fielda;
    private JTextField fieldb;
    private JTextField fieldc;
    private JTextField fieldd;

    public IPvalidui() {
        setSize(400, 400);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        JPanel p = new JPanel();
        p.setLayout(new BorderLayout());
        fielda = new JTextField("test");
        fieldb = new JTextField("test");
        fieldc = new JTextField("test");
        fieldd = new JTextField("test");

        p.add(fielda, BorderLayout.NORTH);
        p.add(fieldb, BorderLayout.SOUTH);
        p.add(fieldc, BorderLayout.WEST);
        p.add(fieldd, BorderLayout.EAST);
        this.add(p);
        JButton btn = new JButton("Check IP Validity");
        add(btn, BorderLayout.NORTH);
        btn.addActionListener(this);

        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        System.out.println("got here");
        IPValidator IpAddress = new IPValidator(Integer.parseInt(fielda.getText()), Integer.parseInt(fieldb.getText()), Integer.parseInt(fieldc.getText()), Integer.parseInt(fieldd.getText()));
        String result = IpAddress.Checktype();

        if (result.compareTo("a") == 0) {
            JOptionPane.showMessageDialog(this, "This is a class A address");
            System.out.println("a");
        }

        if (result.compareTo("b") == 0) {
            JOptionPane.showMessageDialog(this, "This is a class B address");
            System.out.println("b");
        }

        if (result.compareTo("c") == 0) {
            JOptionPane.showMessageDialog(this, "This is a class C address");
            System.out.println("c");
        }

    }
}

Open in new window

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        IPvalidui a = new IPvalidui();
    }

}

Open in new window

0
 
LVL 1

Author Comment

by:pk24573
ID: 35028131
i see your point.
thank you very much.
0

Featured Post

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This video teaches viewers about errors in exception handling.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

617 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