I'm trying to dele Items from a Jlist in real-time execution and but I always get a persistent error message the items of the list consist of user names that should be deleted when the server sends a specific message
package com.yorkrider;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class A_Chat_Client implements Runnable
{
// globals
Socket SOCK;
Scanner INPUT;
Scanner SEND = new Scanner(System.in);
PrintWriter OUT;
ArrayList<String> Users = new ArrayList<String>();
// -----------------------------------------------------------------------------------
public A_Chat_Client(Socket X)
{
this.SOCK = X;
}
// -----------------------------------------------------------------------------------
public void run()
{
try
{
try
{
INPUT = new Scanner(SOCK.getInputStream());
OUT = new PrintWriter(SOCK.getOutputStream());
OUT.flush();
CheckStream();
}
finally
{
SOCK.close();
}
}
catch(Exception X) { System.out.print(X); }
}
// ---------------------------------------------------------------------------------
public void DISCONNECT() throws IOException
{
OUT.println(A_Chat_Client_GUI.UserName + " has disconnected");
OUT.flush();
Remove_User(A_Chat_Client_GUI.UserName, SOCK);
//SOCK.close();
JOptionPane.showMessageDialog(null, "You Disconnected");
//Users.removeAll();
//System.exit(0);
}
// ---------------------------------------------------------------------------------
public void CheckStream()
{
while(true)
{
RECEIVE();
//A_Chat_Client_GUI.JL_ONLINE.setListData(Users)
}
}
// ---------------------------------------------------------------------------------
public void RECEIVE()
{
if(INPUT.hasNext())
{
String MESSAGE = INPUT.nextLine();
if(MESSAGE.contains("#?!"))
{
String TEMP1 = MESSAGE.substring(3);
TEMP1 = TEMP1.replace("[","");
TEMP1 = TEMP1.replace("]", "");
String[] CurrentUsers = TEMP1.split(", ");
A_Chat_Client_GUI.JL_ONLINE.setListData(CurrentUsers);
//Users.add(TEMP1);
A_Chat_Client_GUI.TA_CONVERSATION.append(A_Chat_Server.CurrentUsers.get(
A_Chat_Server.CurrentUsers.size()-1) +" has joined on IP address "+
A_Chat_Client_GUI.HostIP+" and port number "+A_Chat_Client_GUI.PortNumber +"\n");
A_Chat_Client_GUI.JL_ONLINE.removeAll();
System.out.println(Users);
}
//----------------The problem is here-------------------------
else if(MESSAGE.contains("@!@")) {
String TEMP1 = MESSAGE.substring(3);
TEMP1 = TEMP1.replace("[", "");
TEMP1 = TEMP1.replace("]", "");
for(int j=0; j < A_Chat_Client_GUI.JL_ONLINE.getModel().getSize(); j++){
if (TEMP1.equals(A_Chat_Client_GUI.JL_ONLINE.getModel().getElementAt(j))){
A_Chat_Client_GUI.JL_ONLINE.getModel().remove(j);
}
}
}
//--------------------The problem is here------------------------
else
{
A_Chat_Client_GUI.TA_CONVERSATION.append(MESSAGE + "\n");
}
}
}
// ---------------------------------------------------------------------------------
public void SEND(String X)
{
OUT.println(A_Chat_Client_GUI.UserName + ": " + X);
OUT.flush();
A_Chat_Client_GUI.TF_Message.setText("");
}
// ---------------------------------------------------------------------------------
public static void Remove_User(String UserName, Socket SOCK) throws IOException
{
Socket TEMP_SOCK = SOCK;
PrintWriter OUT = new PrintWriter(TEMP_SOCK.getOutputStream());
OUT.println("@!@" + UserName);
System.out.println(UserName);
OUT.flush();
}
}
That is the error message that is the persistent error message:
java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0
I can't understand your code without the implementation of your classes, but perhaps you need:
A_Chat_Client_GUI.JL_ONLINE.getModel().remove(j);
instead of:
A_Chat_Client_GUI.JL_ONLINE.remove(j);
The loop for
should start from the last index:
for (int j = A_Chat_Client_GUI.JL_ONLINE.getModel().getSize() - 1; j >= 0; j--) {
if (TEMP1.equals(A_Chat_Client_GUI.JL_ONLINE.getModel().getElementAt(j))) {
A_Chat_Client_GUI.JL_ONLINE.getModel().remove(j);
}
}
This way, non scanned indexes stay at the same place (keep same index j
) after any remove.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.