Solved

java ip class validator

Posted on 2011-03-03
4
630 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
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HashTable highest marks enumeration alternative 9 47
ejb on wildfly 5 45
Java 8 to Java 6 8 32
Tomcat 9 + java 8 error while trying to deploy a war file 2 681
Introduction This question got me thinking... (http://www.experts-exchange.com/questions/28707487/GLOBALS.html) Why shouldn't we use Globals? This is a simple question without a simple answer.  How do you explain these concepts to a programmer w…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

685 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