我被利用的方法写一个程序将字符串转换为IP喂,地址和IP地址 InetAddress.getByAddress(字节[]地址)。
所以,我所做的就是输入IP,从字符串的形式给用户。解析它,分裂的IP。使用 String.split(\\\\)。
然后,我开始转换的String数组到字节数组,其中我现在被困在
请帮我摆脱这种局面。任何解决办法或进入本的替代方法将heartly AP preciated ...
的code竟把: -
公共静态无效的主要(字串[] args){ 尝试{ 的System.out.println(输入IP地址的MAC地址,你想知道: - ); 扫描器S =新的扫描仪(System.in); 字符串IPA = s.nextLine(); 字符串BA [] = ipa.split(\\\\); 对于(字符串AP:BA){ 的System.out.println(鉴于IP =+ AP); } 字节[] =坏新的字节[ba.length] 的for(int i = 0; I< ba.length;我++){ 的System.out.println(巴 - + I +=+ BA [I]); 如果(Integer.valueOf(BA [I])GT; 127){ INT TEMP = Integer.valueOf(BA [I]); 的+ I +//System.out.println(\"Value ---+温度); 坏[I] =(字节)的温度; //这会产生错误在运行时 } 坏[I] = Byte.valueOf(BA [I]); 的System.out.println(坏 - + I +=+坏[I]); } //字节坏[] = {(字节)192,(字节)168,122,1}; InetAddress类IA = InetAddress.getByAddress(坏的); ...............这里是code的其余部分,它编译良好。抛出异常: -
输入IP地址的MAC地址,你想知道: - 192.168.122.1鉴于IP = 192鉴于IP = 168鉴于IP = 122鉴于IP = 1BA-0 = 192异常线程mainjava.lang.NumberFormatException:值超出范围。值:192基数:10在java.lang.Byte.parseByte(Byte.java:151)在java.lang.Byte.valueOf(Byte.java:205)在java.lang.Byte.valueOf(Byte.java:231)在NP_7.main(NP_7.java:36)Java结果:1解决方案
您可以从这些麻烦保存自己,如果你使用 InetAddress.getByName(字符串) 来代替。
您得到错误,因为字节的范围是从-128到127,因此,例如192超出范围。
您可以通过更改填充坏进入这个循环修复code
的for(int i = 0; I< ba.length;我++){ 的System.out.println(巴 - + I +=+ BA [I]); 坏[I] =(字节)的Integer.parseInt(BA [I]); }I was writing a program to convert a String fed IP-Address to IP-Address by utilising the method InetAddress.getByAddress(byte[] addr).
So, what I did was input IP-from the user in the form of String. Parsed it and splitted the IP on . using String.split("\\.").
Then, I started converting that String array into byte array where I am right now stuck on.
Please help me to get rid of this situation. Any workaround OR alternate way of accessing this will be heartly appreciated...
The code goes as :-
public static void main(String[] args) { try{ System.out.println("Enter the IP-Address whose MAC-address you wanna know :-"); Scanner s=new Scanner(System.in); String ipa=s.nextLine(); String ba[]=ipa.split("\\."); for(String ap:ba){ System.out.println("Given IP="+ap); } byte [] bad=new byte[ba.length]; for(int i=0;i<ba.length;i++){ System.out.println("Ba-"+i+"="+ba[i]); if(Integer.valueOf(ba[i])>127){ int temp=Integer.valueOf(ba[i]); //System.out.println("Value of "+i+"---"+temp); bad[i]=(byte) temp; // this produces error at run-time } bad[i]=Byte.valueOf(ba[i]); System.out.println("Bad-"+i+"="+bad[i]); } //byte bad[]={(byte)192,(byte)168,122,1}; InetAddress ia=InetAddress.getByAddress(bad); ............... here is the rest of code and it compiles well.Exception thrown :-
Enter the IP-Address whose MAC-address you wanna know :- 192.168.122.1 Given IP=192 Given IP=168 Given IP=122 Given IP=1 Ba-0=192 Exception in thread "main" java.lang.NumberFormatException: Value out of range. Value:"192" Radix:10 at java.lang.Byte.parseByte(Byte.java:151) at java.lang.Byte.valueOf(Byte.java:205) at java.lang.Byte.valueOf(Byte.java:231) at NP_7.main(NP_7.java:36) Java Result: 1解决方案
You can save yourself from all this trouble if you use InetAddress.getByName(String) instead.
You get the error because the range of byte is from -128 to 127, so for example 192 is out of range.
You can fix the code by changing the loop that fills bad into this:
for(int i=0;i<ba.length;i++){ System.out.println("Ba-"+i+"="+ba[i]); bad[i] = (byte) Integer.parseInt(ba[i]); }
更多推荐
从int转换为字节强行导致
发布评论