Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

java ip class validator

Posted on 2011-03-03
4
Medium Priority
?
661 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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
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…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

971 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