java - IP程序,将对象设置为默认值

标签 java

我正在做一项作业,但遇到了问题。我试图弄清楚如果传递给对象的信息无效(无效的 IP 地址),如何让 PC3 对象显示默认值。事实上,显示屏仅显示 IP: null,我需要让它调用 setDefault() 方法并显示它。

我很抱歉这里有很长的代码,但我想让你看看发生了什么。 非常感谢您的帮助!

import java.util.Random;

public class IPAddress 
{           
   private String IPAddress;
   private String subnetMask;
   private int intParts[];
   private String retString;


   IPAddress(){  //sets default IP address in 169.254.X.Y, where X and Y are values between 0 and 255
      setDefault();   

   }

   IPAddress(String ip, String sm){
      setIPAddress(ip);
      setSubnetMask(sm); 
   }

   public void setDefault(){
      String temp="169.254.";
      int octet3=randVal();
      int octet4=randVal();
      while((octet3==0 && octet4==0) || (octet3==255 && octet4==255)){  //cannot have 169.254.0.0 as it is a network address
         octet4=randVal();                                              //cannot have69.254.255.255 as it is a broadcast address
      }

      temp=temp+octet3+"."+octet4;

      setIPAddress(temp);

      setSubnetMask("255.255.0.0");

   }

   public void setIPAddress(String ip){
      if(isValidIPAddress(ip)){
         IPAddress=ip;
      }
      else{
         System.out.println("Invalid IP Address:  "+ip);
      } 
   }

   public void setSubnetMask(String sm){
      if(isValidSubnetMask(sm)){
         subnetMask=sm;
      }
      else{
         System.out.println("Invalid Subnet Mask:  "+sm);
      } 
   }

   public int randVal(){
        //value between 0 and 255
      Random rnd1=new Random();

        int x=(int)(rnd1.nextDouble()*255);

        return x;
    }

    public String tellCase(){

     //String retString = "";
      if ((intParts[0] > 0) && (intParts[0] <= 128))
         retString = "A";
      else if ((intParts[0] >= 127) && (intParts[0] <= 191))
         retString = "B";
      else if ((intParts[0] >=192) && (intParts[0] <= 223))
         retString = "C";
      else if ((intParts[0] >= 224) && (intParts[0] <= 239))
         retString = "D"; 

     return retString;
    }

   public boolean checkValidIPClass (String x){
    //System.out.println(retString);  checks to make sure the string made it to the method
    for ( int i = 0; i < 4; i++) {
         if (retString == "A"){
            if ((intParts[1] == 0) && (intParts [2] == 0) && (intParts [3] == 0))
               return false;
            if ((intParts[1] == 255) && (intParts [2] == 255) && (intParts [3] == 255))
               return false;
         }
         if (retString == "B"){
            if ((intParts [2] == 0) && (intParts [3] == 0))
               return false;
            if ((intParts [2] == 255) && (intParts [3] == 255))
               return false;

         }
         if (retString == "C"){
            if (intParts [3] == 0)
               return false;
            if (intParts [3] == 255)
               return false;
         }
   }
   return true;
}



   public boolean isValidIPAddress(String ip){ /*YOU NEED TO FILL IN THE METHOD*/

      //check to verify only 3 dots, the values in each octet are between 0 and 255 and is an IP address which can be assigned 
      //Class a 1st octet 1-127 and the rest cannot be 0's or 255's
      //Class b 1st octet 128-191 second 0-255 and last two cannot be all 0's or 255's

       try {
        if (ip == null || ip.isEmpty()) {
            return false;
        }

        String[] parts = ip.split( "\\." );
        if ( parts.length != 4 ) {
            return false;
        }

        intParts = new int[4];

        for (int j = 0; j < 4; j++ ) {
            int i = Integer.parseInt( parts[j] );
            if ( (i < 0) || (i > 255) ) {
                return false;
            }

            intParts[j] = i;

        } //ends for loop for storing values into int array

        if(ip.endsWith(".")) {
                return false;
        }

        checkValidIPClass(tellCase());


        return true;
    } catch (NumberFormatException nfe) {
        return false;
    }

 }

   public boolean isValidSubnetMask(String sm){  /*YOU NEED TO FILL IN THE METHOD*/

      //only checks for default subnet masks (either 255.0.0.0 or 255.255.0.0 or 255.255.255.0).
       try {
        if (sm == null || sm.isEmpty()) {
            return false;
        }

        String[] parts = sm.split( "\\." );
        if ( parts.length != 4 ) {
            return false;
        }

        if ((sm != "255.0.0.0") && (sm != "255.255.0.0") && (sm != "255.255.255.0")) {
            return false;
        }        

        if(sm.endsWith(".")) {
                return false;
        }

        return true;
    } catch (NumberFormatException nfe) {
        return false;
    }
}

   public int stringToInt(String v){
      boolean answer=true;
      int returnVal=-1; 

      for(int i=0; i<v.length(); i++){  //verify all characters in v are digits
         if(!Character.isDigit(v.charAt(i))){  //as soon as 1 non-digit is found, end the loop as we know v is not a number
            answer=false;
            i=v.length()+1;
         }
      }

      if(answer){ //if v is a number, then change it to its number
         returnVal=Integer.parseInt(v);
      }
      return returnVal;  //if this is -1, then v is not a number.  otherwise, v is a number
   }

   public String getIPAddress(){
      return IPAddress;
   }

   public String getSubnetMask(){
      return subnetMask;
   }

   public String toString(){
      String returnString="IP Address:  "+IPAddress+"\n";
      returnString+="Subnet Mask:  "+subnetMask+"\n";

      return returnString;
   }

   public static void main(String[] args)

   {  
      //sample creation of objects of the class
      IPAddress PC1=new IPAddress();  //uses default constructor
      IPAddress PC2=new IPAddress("165.0.0.0", "255.255.0.0");  //uses alternate constructor (overloaded)
      IPAddress PC3=new IPAddress("122..15.12.1", "255.0.0.0");   //uses alternate constructor (overloaded) - should cause errors messages to appear.

      //sample output from creation of objects of the class
      System.out.println(PC1);
      System.out.println(PC2);
      System.out.println(PC3);  //should end up printing something in 169.254.X.Y range once you have created the check methods.

      System.out.println(PC1.stringToInt("110"));  //way of testing to see how individual methods work       
      System.out.println(PC1.stringToInt("a"));  //prints -1 beacause a is not a number
      System.out.println(PC1.stringToInt("110d"));  //prints -1 because d isn't a digit
      System.out.println(PC1.stringToInt("13e5"));  //prints -1 because e isn't a digit


   }  

}  //end class 

最佳答案

所以调用setDefault()而不是

System.out.println("Invalid IP Address:  "+ip);

在您的setIPAddress方法中。

您的 setDefault 还有一个错误, 在 while 循环中,您仅重新生成 octe4 值而不是 octed3,因此如果 octed3 无效,它将永远不会终止。

关于java - IP程序,将对象设置为默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29521524/

相关文章:

java - JFrame 正在加载音乐正在运行

java - 我如何检测到 Horizo​​ntalScrollView 在 Android 中已经结束?

java - 寻找避免小程序重新加载的可能策略

java - 测试前设置TestNG的输出目录

java - 读取从 PHP 回显的字符串到 java 中?

java - Jaxb:在同一个包中使用多个命名空间解码 xml

java - 在 Google Cloud Endpoints 中获取原始 HTTP 数据( header 、Cookie 等)

java - Web 应用程序 : Parallel changes of data in repository

java - 将 setVisible() 函数放在函数的开头是否与我将它放在该函数的末尾有所不同?

java - 将 FileObject 转换为文件