Home > Back-end >  Java subscript crossing the line how to solve
Java subscript crossing the line how to solve

Time:09-16

Package bianma;

Import the Java. IO. *;
Import the Java. Util. HashMap;


Public class P2 {
Public static void main (String [] args) throws IOException {
HashMap The map=new HashMap<> (a);
BufferedReader br=new BufferedReader (new FileReader (" paixu. TXT "));
BufferedWriter bw=new BufferedWriter (new FileWriter (" paixu2. TXT "));
String line;
While ((line=br. ReadLine ())!=null) {
//String [] arr=new String [3].
Arr=line. The split (" \ \ ");
//System. Out. Println (arr [0]).
//System. Out. Println (arr [1]).
The map. The put (arr [0], arr [1]).
//System. Out. Println (map);
}

For (String key: map. KeySet ()) {
The String value=https://bbs.csdn.net/topics/map.get (key);
System.out.println(value);
Line=key + "" + value;
Bw. Write (line);
Bw. NewLine ();//write line breaks
}
bw.close();
Br. The close ();
}
}

An Exception in the thread "is the main" Java. Lang. ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
Line error has been displayed is "map. The put (arr [0], arr [1])," the line
If before the trip plus "System. Out. Println (arr [1]);" , an error is the line
To make the "map. The put (arr [0], arr [1])," commented program running (nothing)
Could you tell me what the problem ah


CodePudding user response:

Only one data only the subscript subscript 0 1 is crossing the line

CodePudding user response:

System. The out. PRNTLN (arr);
Arr=line. The split (" \ \ ");

The front print your data, it should be a row no decimal point integral,

CodePudding user response:

refer to the second floor old zizhu reply:
System. Out. PRNTLN (arr);
Arr=line. The split (" \ \ ");

The front print your data, it should be a row no decimal point integral,


Original code I am direct new arr=line. The split (" \ \ ");
Print arr does not make sense

CodePudding user response:

To determine the length of a parameter

CodePudding user response:

The building Lord, you are require users to input, according to your specifications and requirements two data input, data with a delimiter, but a user use, partial not according to your request, so that you don't get the split into what you want
At this time, you want to do is to determine whether a user input format meets the requirements, if do not conform to, error directly, don't go to the following processing logic,

CodePudding user response:

reference 4 floor akihabara の black cat replies:
Quote: refer to the second floor of the old zizhu response:
System. Out. PRNTLN (arr);
Arr=line. The split (" \ \ ");

The front print your data, it should be a row no decimal point integral,


Original code I am direct new arr=line. The split (" \ \ ");
Print arr meaningless


Check paixu. TXT this file, do as the second floor so the possibility is very large, the data error light to see the program you for a lifetime to find problems,

CodePudding user response:

Paixu. TXT the file content without [.], the split is not up to the effect you want
  • Related