package net.okren.java;
import java.util.*;
//将逗号分隔的字符串 转换成 Byte数组,
class GetByte{
public  byte[] getByte(String str){
System.out.println("getByte...");
int wn = 0;
for(int i = 0; i < str.length(); i++){
if(str.charAt(i) == ','){wn++;}
}
wn++;
byte[] reByte = new byte[wn];
for(int i = 0; i < wn; i++){
if( i != (wn - 1)){
reByte[i] = (byte) Integer.valueOf(str.substring(0, str.indexOf(",")), 16).intValue();
str = str.substring(str.indexOf(",") + 1, str.length());
}else{
reByte[i] = (byte) Integer.valueOf(str, 16).intValue();
}
}
return reByte;
}
}public class JavaTest {

public static void main(String[] args) throws NumberFormatException{

GetByte gb = new GetByte();
System.out.println("input a string ,,,");
Scanner in = new Scanner(System.in);
String line = in.nextLine();
System.out.println(line);
byte[] bb = gb.getByte(line);
for(int i = 0; i < bb.length; i++){
System.out.println(bb[i]);
}


}
}
输入,输出如下:不知道 是哪里错误了!!!!》???我运行出现错误,希望大家看看哪里错误了, 
input a string ,,, 
ok,lig,reafa,faf,af,afa,fa 
ok,lig,reafa,faf,af,afa,fa 
getByte... 
Exception in thread "main" java.lang.NumberFormatException: For input string: "ok" 
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) 
at java.lang.Integer.parseInt(Integer.java:447) 
at java.lang.Integer.valueOf(Integer.java:526) 
at net.okren.java.GetByte.getByte(JavaTest.java:15) 
at net.okren.java.JavaTest.main(JavaTest.java:34) 

解决方案 »

  1.   

    字符串应该存放数字字符串,如"1,2,3"
    “ok”不能转换成byte类型
      

  2.   

    正解
    字符串转换成byte[]就用str.getBytes();
      

  3.   

    应该是Integer.valueOf方法出错了,你想把ok转成Integer 但这是不能转换的你用String#split(',')分隔再
    断定 是不是数字 ,再解析 
      

  4.   


    尽量使用jdk已经提供的方法做吧
      

  5.   

    简单的修改了下 这个是不是你要的import java.util.*;
    //将逗号分隔的字符串 转换成 Byte数组,
    class GetByte{
        public  byte[][] getByte(String str){
            /*System.out.println("getByte...");
            int wn = 0;
            for(int i = 0; i < str.length(); i++){
                if(str.charAt(i) == ','){wn++;}
            }
            wn++;
            byte[] reByte = new byte[wn];
            for(int i = 0; i < wn; i++){
                if( i != (wn - 1)){
                 String sss = str.substring(0, str.indexOf(","));
                 System.out.println(sss);
                    reByte[i] = (byte) Integer.valueOf(sss, 16).intValue();
                    str = str.substring(str.indexOf(",") + 1, str.length());
                }else{
                    reByte[i] = (byte) Integer.valueOf(str, 16).intValue();
                }
            }*/
         String[] ss = str.split(",");
         byte[][] reByte = new byte[ss.length][];
         for(int i=0; i<ss.length; i++) {
         reByte[i] = ss[i].getBytes();
         }
                                        
            return reByte;
        }
    }public class JavaTest {
            
        public static void main(String[] args) throws NumberFormatException{
            
            GetByte gb = new GetByte();
            System.out.println("input a string ,,,");
            Scanner in = new Scanner(System.in);
            String line = in.nextLine();
            System.out.println(line);
            byte[][] bb = gb.getByte(line);
            for(int i = 0; i < bb.length; i++){
                System.out.println(new String(bb[i]));
            }
        
            
        }
    }
      

  6.   


    OK,正解,明白了!thanks!
      

  7.   

     reByte[i] = (byte) Integer.valueOf(str, 16).intValue();
    再仔细研究一下