Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

java ip class validator

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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 …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

721 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