错误:java.lang.ArrayIndexOutofBounds异常:1,如何处理
如何处理发出此消息的代码中的错误 java.lang.ArrayIndexOutOfBoundsException:1 为了使用udp套接字将数据包从客户端发送到服务器错误:java.lang.ArrayIndexOutofBounds异常:1,如何处理,java,indexoutofboundsexception,Java,Indexoutofboundsexception,如何处理发出此消息的代码中的错误 java.lang.ArrayIndexOutOfBoundsException:1 为了使用udp套接字将数据包从客户端发送到服务器 public class DatagramServer { private final static int PACKETSIZE = 100 ; public static void main( String args[] ) { // Check the arguments if( args.length
public class DatagramServer
{
private final static int PACKETSIZE = 100 ;
public static void main( String args[] )
{
// Check the arguments
if( args.length != 0 )
{
System.out.println( "usage: DatagramServer port" ) ;
return ;
}
try
{
// Convert the argument to ensure that is it valid
int port = Integer.parseInt( args[1] ) ;
// Construct the socket
DatagramSocket socket = new DatagramSocket( port ) ;
System.out.println( "The server is ready..." ) ;
}
}
数组元素从索引0开始,而不是
1
:
int port = Integer.parseInt( args[0] ) ; // first argument is args[0]
将
if
语句更改为检查是否等于,即
if(args.length==0)
或者使用1
代替0
,即
if(args.length!=1)
当前代码意味着只有当
args
的长度为0
时,才能到达Integer.parseInt()
语句,因此通过寻址args[0]
可以尝试索引空数组的第一个位置 谢谢你的帮助,但是它一直给我同样的问题谢谢,它工作了,但是我需要它转到try语句,但是它保留在IF语句中如果它在两种情况下都保留在'IF``语句中,那么你就不能通过这个论点。如何调用