0

I am trying to use a 2D array to create JTable. When assigning the values for the columns in the JTable I get the Java.lang.String found error. The data type of the variables are also String and the 2D array is also of type 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);
}

}

2
  • 1
    Please format your code properly and turn it into a Minimal Complete Verifiable Example: stackoverflow.com/help/mcve btw: int b[] is c-style syntax - get rid of it. Commented Mar 27, 2015 at 9:36
  • columnNames is a simple array, on the error lines i expected to see data instead, no ? Commented Mar 27, 2015 at 9:37

1 Answer 1

2

columnNames in your code above is a one dimensional String array, but you're attempting to use it as a two dimensional array

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

I think you meant to assign values in your loop to the data array instead of the columnNames array, as in

data[u][0] = first;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.