b47617

攀登!进步!
随笔 - 20, 文章 - 1, 评论 - 0, 引用 - 0
数据加载中……

String,StringBuffer ,基本数据类型的对象包装类

e.g1:编写一个程序来简单了解String类的使用.程序一行一行地读取从键盘上不停输入的字符串,并打印显示.直到输入一行"bye"为止.
 class ReadLine{
         public static void main(String args[]){
                   byte[] buf  = new byte[1024];
                   int ch = 0;
       int pos = 0;
       String strInfo = null;
                    while(true){
                           try{
                                   ch = System.in.read();
                           }catch(Exception e){
                                    System.out.println(e.getMessage());
                           }
       switch(ch){
        case '\r':
         break;
        case '\n':
         strInfo = new String(buf,0,pos);//在buf中提取0至pos元素.
        if(strInfo.equals("bye"))
         return;
        else{
         System.out.println(strInfo);
         pos = 0;
         break;
        }
        default:
         buf[pos++] = (byte)ch;
       }
  
     }
        }
 }
e.g2:如,要将字符串转换成基本数据类型,几乎都使用"Xxx包装类.parseXxx"方式实现(一个例外是对于Boolean类,用的是getBoolean方法):要将包装类转换成基本数据,几乎都是"Xxx包装类对象.xxxVaue"方式.
class TestInteger{
 public static void main(String[] args){
  int w = Integer.parseInt(args[0]);
  int h = new Integer(args[1]).intValue();
  for(int i = 0; i < w;i++){
   StringBuffer str = new StringBuffer();
   for(int j = 0 ; j < h;j++){
    str.append("*");
   }
   System.out.println(str.toString());
  }

 }
}

posted on 2006-03-20 10:59 原语 阅读(365) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: