从int转换为字节强行导致

编程入门 行业动态 更新时间:2024-10-27 06:34:59
本文介绍了从int转换为字节强行导致---异常螺纹"主" java.lang.NumberFormatException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我被利用的方法写一个程序将字符串转换为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转换为字节强行导致

本文发布于:2023-05-31 10:03:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/389516.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   字节   int

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!