我定义了一个字符串数组,但是只初始化了一部分(不知道具体长度)
将其转化为字符串,再进行串比较,该如何写程序呢?
苦思不得其解,我已经用尽了我所想到的所有的方式:
   public static void main(String []args)
{
char[] t=new char[4];
t[0]='1';
    String a=new String(t,0,2);
第一种:     if(a.equalsIgnoreCase("1 "))     //不管加不加空格都不对
System.out.println("equal");
    else
System.out.println("NO!");
第二种:      if((t[0]=='1')&&(t[1]==''))    //这里不管加不加空格也是不对的
第三种:      if((t[0]=='1')&&(t[1]==NULL))    //这样子写也不对
}字符串的空格和空值好像是没法比较的,但是空值的时候该如何判断啊?
请教啊!请教啊!

解决方案 »

  1.   

    public static void main(String []args)
    {
    char[] t=new char[4]; 
    t[0]='1';
        String a=new String(t,0,2);
    第一种:     if(a.equalsIgnoreCase("1 "))     //不管加不加空格都不对
    System.out.println("equal");
        else
    System.out.println("NO!");
    第二种:      if((t[0]=='1')&&(t[1]==''))    //这里不管加不加空格也是不对的
    第三种:      if((t[0]=='1')&&(t[1]==NULL))    //这样子写也不对
    }数组初始化的时候每个单元的内容并不是空格 而是空 '\0'
    所以 String a=new String(t,0,2);  // 的内容 不可能是 "1 " 而是 "1" 因为字符串使用 '\0'作为结束符,所以 new String(t,0,2) 不论是2还是几 都只返回一个 "1"当然 if((t[0]=='1')&&(t[1]=='')) 是不对的 应该将''替换成 '\0',
    同时 NULL 并不能代表 一个空值,在Java里没有定义NULL为关键字常量看了一定要明白呀,不然就别说自己是告程序的
      

  2.   

    public static void main(String[] args)
        {
            char[] t = new char[4];
            t[0] = '1';
            String a = new String(t, 0, 2);
            
            if (a.equalsIgnoreCase("1\0")) 
                System.out.println("equal");
            else
                System.out.println("NO!");
            
        }
    It will print "equal".
      

  3.   

    其实这道题目是这样子的:
      输入一日期如: 3,2,1990
     将其以"2nd March 1990"的形式写入文件.我是读入一字符串,然后再根据","将其读入一个二维字符数组.
    通过字符串比较再分别写入文件
    可是这样子搞来搞去,写了好多,且很麻烦,
    有没有其它的路径可以达到目的的.
    还望高人指点.
    我现在已经被绕进去了,怎么也想不出好的法子来以下是我写的程序:import java.io.*;
    import java.util.*;
    public class Date
    {
         public static void main(String []args)
           {
    // System.out.println("输入exit可退出该程序");
     System.out.print("输入日期(例如: 10,1,1949表示1949年10月1日): ");
     Scanner input=new Scanner(System.in);
     String time=input.next();
    // while(!time.equals("exit"))
    // {
            char [][]temp=new char[3][4];
    int a=0;
    int j=0;
         while(a<time.length())
         {
    int i=0;             
       char ch=Character.toLowerCase(time.charAt(a));
    while(ch!=',')
    {
      temp[j][i]=ch;
      i++;
      a++;
      if(a<time.length())
                           ch=Character.toLowerCase(time.charAt(a));
      else break;
    }  
    if(ch==',') {  a++;  j++;  }
                 }     
          try
                  {
                    File file=new File("date.txt");
    file.createNewFile();
    FileWriter myFile=new FileWriter(file);
    CharArrayWriter CharArrayOut=new CharArrayWriter(10);
             String day=new String(temp[1],0,2);
    //System.out.println(day+"day");
                    if((temp[1][0]=='1')&&(temp[1][1]=='\0'))
     {
                    CharArrayOut.write(temp[1][0]);
    CharArrayOut.write("st");
                    CharArrayOut.writeTo(myFile);       
    System.out.println(temp[1][0]+"st");  
     }
                    else if((temp[1][0]=='2')&&(temp[1][1]=='\0'))
     {
                    CharArrayOut.write(temp[1][0]);
    CharArrayOut.write("nd");
                    CharArrayOut.writeTo(myFile);       
    System.out.println(temp[1][0]+"nd");  
    }
    else if((temp[1][0]=='3')&&(temp[1][1]=='\0'))
     {
                    CharArrayOut.write(temp[1][0]);
    CharArrayOut.write("rd");
                    CharArrayOut.writeTo(myFile);       
    System.out.println(temp[1][0]+"rd");  
    }
                    else
     {
                   CharArrayOut.write(temp[1],0,2);
    CharArrayOut.write("th");
                   CharArrayOut.writeTo(myFile); 
    System.out.println(day+"th");               
    }         String month=new String(temp[0],0,2);
    if(month.equalsIgnoreCase("1\0"))
     {
                   CharArrayOut.write("Jan",0,3);
                   CharArrayOut.writeTo(myFile); 
    System.out.println("Jan");
     }
    else if(month.equalsIgnoreCase("2\0"))
     {             CharArrayOut.write("Feb",0,3);
                   CharArrayOut.writeTo(myFile); }
     else if(month.equalsIgnoreCase("3\0"))
    {              CharArrayOut.write("March",0,5);
                   CharArrayOut.writeTo(myFile); }
            else if(month.equalsIgnoreCase("4\0"))
    {              CharArrayOut.write("Apr",0,3);
                   CharArrayOut.writeTo(myFile); }
            else if(month.equalsIgnoreCase("5\0"))
    {              CharArrayOut.write("May",0,3);
                   CharArrayOut.writeTo(myFile); }
    else if(month.equalsIgnoreCase("6\0"))
    {    CharArrayOut.write("Jule",0,4);
                   CharArrayOut.writeTo(myFile); }
            else if(month.equalsIgnoreCase("7\0"))
    {    CharArrayOut.write("July",0,4);
                  CharArrayOut.writeTo(myFile); }
     else if(month.equalsIgnoreCase("8\0"))
    {    CharArrayOut.write("August",0,6);
                   CharArrayOut.writeTo(myFile); }
     else if(month.equalsIgnoreCase("9\0"))
    {    CharArrayOut.write("September",0,9);
                   CharArrayOut.writeTo(myFile); }
     else if(month.equalsIgnoreCase("10"))
    {    CharArrayOut.write("October",0,7);
                   CharArrayOut.writeTo(myFile); }
    else if(month.equalsIgnoreCase("11"))
    {  
        CharArrayOut.write("Cemtember",0,9);
                CharArrayOut.writeTo(myFile); 
    }
    else if(month.equalsIgnoreCase("12"))
    {    
    CharArrayOut.write("November",0,8);
            CharArrayOut.writeTo(myFile); 
    System.out.println("November");
    }
    else
    {
    System.out.println("error");
    }
                    CharArrayOut.write(temp[2],0,temp[2].length);
    CharArrayOut.writeTo(myFile);
      myFile.close();
                   }
           catch(IOException e)
    {
          System.out.println(e.getMessage());
    }
    // }       }
    }大家帮帮忙啊,看看有没得办法修改的简单些,我总觉得我绕了个大圈,走了个远路,可是自己去怎么也走不出来!~
      

  4.   

    1.根据字串获得时间对象
            String dateStr = "3,2,1990";
            String pattern = "M,d,yyyy";
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            Date date = sdf.parse(dateStr);2. 根据日期的date字段的值,自己进行判断,生成“2nd”3."March 1990"可以根据SimpleDateFormat生成。
       String pattern = "MMMMM yyyy";
      

  5.   

    噢,对了,高人!
    我这个程序还有个问题:
     就是为什么每次往文件里面给我写入的时候总是写上两三遍呢?
    比如,我输入了一个: 3,2,1908
    写入文件的就是:2nd2ndMarch2ndMarch1908
    这是什么原因啊?
    难道在缓冲区里面要复制吗? 啊?? 奇怪~~~~
      

  6.   

    多写了几个  CharArrayOut.writeTo(myFile); 
    对于写入缓冲区的数据,是先进先出的
    所以,前面在IF里面写了几个writeTo也就会往文件里面写几次啦
    只要在最后调用一次这个函数就可以啦!~