Solved

java ip class validator

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Bot application - advice 3 80
Eclipse with various Java releases 7 87
Java array 21 150
netbeans can not run self signed app 8 37
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

734 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