需要数组,但Java.lang.string找到String错误

时间:2015-03-27 09:33:30

标签: java

我正在尝试使用2D数组来创建JTable。在JTable中为列分配值时,我发现了Java.lang.String发现错误。变量的数据类型也是String,2D数组也是String类型。

import java.io.*;
import java.util.*;
/**
* Write a description of class PhoneBook here.
* 
* @author (your name) 
* @version (a version number or a date)
*/
 public class PhoneBook 
{
static PhoneBookEntry contacts[] = new PhoneBookEntry[100];
int a=0;
int b[] = new int [100];
int count=0;
public void getData()throws IOException
{
    FileReader in = new FileReader("phonebookinput.txt");
    BufferedReader textreader = new BufferedReader(in);
    String sp[];
    for(a=0; a<contacts.length; a++)
    {
        String s = textreader.readLine();
        sp = s.split("\t");
        contacts[a] = new PhoneBookEntry(sp[0], sp[1], (Integer.parseInt(sp[2])), sp[3], sp[4]);
    }
}
public void add(String z, String x, int c, String d, String e)
{
    for (int t=0; t<b.length; t++)
    {
        if (contacts[b[t]].getNumber().equals("XXXX"))
        {
           contacts[b[t]] = new PhoneBookEntry(z, x, c, d, e);
        }

        else
       {
           contacts[a+1] = new PhoneBookEntry(z, x, c, d, e);
           a++;
       }
   }
}
   public int searchName(String n)
{
    int y=-1;
    for (int b=0; b<contacts.length;b++)
    {
        if (contacts[b].getFirstName().equalsIgnoreCase(n))
        {
            y=b;

        }
        else if (contacts[b].getLastName().equalsIgnoreCase(n))
        {
            y=b;

        }
    }
    return y;
}
public int searchNumber(String m)
{
    int x=-1;
    for(int d=0; d<contacts.length; d++)
    {
        if (contacts[d].getNumber().startsWith(m))
        {
            x=d;
        }
        else if (contacts[d].getNumber().endsWith(m))
        {
            x=d;
        }
    }
    return x;
}
public boolean edit(String a, String b, int c, String d, String e, String f)
{
    int g=searchName(f);
    int h=searchNumber(f);
    if (g!=-1)
    {
        contacts[g].setFirstName(a);
        contacts[g].setLastName(b);
        contacts[g].setAge(c);
        contacts[g].setNumber(d);
        contacts[g].setEmail(e);
        return true;
    }
    else if (h!=-1)
    {
        contacts[h].setFirstName(a);
        contacts[h].setLastName(b);
        contacts[h].setAge(c);
        contacts[h].setNumber(d);
        contacts[h].setEmail(e);
        return true;
    }
    else {return false;}
}
public void deleteValue(String u)
{
    int g=searchName(u);
    int h=searchNumber(u);
    if (g!=-1)
    {
        contacts[g].setFirstName("XXXX");
        contacts[g].setLastName("XXXX");
        contacts[g].setAge(-1);
        contacts[g].setNumber("XXXX");
        contacts[g].setEmail("XXXX");
        b[count]=g;
    }
    else if (h!=-1)
    {
        contacts[h].setFirstName("XXXX");
        contacts[h].setLastName("XXXX");
        contacts[h].setAge(-1);
        contacts[h].setNumber("XXXX");
        contacts[h].setEmail("XXXX");
        b[count]=h;
    }
    count = count + 1; 
}
public void sortFirstName()
{
    for (int r=99; r>=0; r--)
       {
           for (int h=0; h<=r-1; h++)
           {   
               if (contacts[h].getFirstName().compareTo(contacts[h+1].getFirstName())>0)
               {
                   String temp = contacts[h+1].getFirstName();
                   contacts[h+1].setFirstName(contacts[h].getFirstName());
                   contacts[h].setFirstName(temp);
                }
            }
    }
  }
public void sortLastName()
{
    for (int r=99; r>=0; r--)
       {
           for (int h=0; h<=r-1; h++)
           {   
               if (contacts[h].getLastName().compareTo(contacts[h+1].getLastName())>0)
               {
                   String temp = contacts[h+1].getLastName();
                   contacts[h+1].setLastName(contacts[h].getLastName());
                   contacts[h].setLastName(temp);
                }
            }
        }
    }
public void printDetails()
{
    String [] columnNames = {"First Name", "Last Name", "Age", "Phone Number", "Email"};
    String data [][] = new String [100][5];
    for (int u=0; u<data.length; u++)
    {
        String first = contacts[u].getFirstName();
        String last = contacts[u].getLastName();
        String age = Integer.toString(contacts[u].getAge());
        String number = contacts[u].getNumber();
        String email = contacts[u].getEmail();
        columnNames[u][0] = first;     //Here is where the error comes
        columnNames[u][1] = last;
        columnNames[u][2] = age;
        columnNames[u][3] = number;
        columnNames[u][4] = email;
    }
    JTable table = new JTable (data, columnNames);
    table.setEnabled(false);
}

}

1 个答案:

答案 0 :(得分:2)

上面代码中的

columnNames是一维String数组,但是你试图将它用作二维数组

columnNames[u][0] = first;     //Here is where the error comes

我认为您的意思是将循环中的值分配给data数组而不是columnNames数组,如

data[u][0] = first;