【发布时间】:2016-05-15 14:38:59
【问题描述】:
我对 java 语言的了解非常新。我的大部分知识来自谷歌搜索如何做事。我一直在研究一个使用 switch 语句的 Java 控制台程序。整个程序使用一个 String [20][5] 数组。我已经编写了代码,现在可以添加条目、将数组保存到文件、将文件中的条目加载到数组中。
现在的问题是编辑和删除条目。我能够将值返回为 null,但它看起来像这样 [[null], [null, null, null, null, null]。
我希望值返回 [null, null, null, null, null], [null, null, null, null, null]。
Edit 条目出于某些原因返回 java.lang.ArrayIndexOutOfBoundsException:1 。
有人能指出我的错误吗?数组也是全局声明的。
public static String[][] rem(){
Scanner input = new Scanner(System.in);
int x,y=0;
//for(int i=0;i<array.length;i++){
//if(array[i][0]!=null){
System.out.println(Arrays.deepToString(array));//}
//}
System.out.println("Which entry would you like to remove? "
+ "\n" + "Enter number 0 - 20");
x = input.nextInt();
array[x][y]=null;
return array;}
public static String[][] edit(){
Scanner input = new Scanner(System.in);
String k;
int j;
int g;
// for(int i=0;i<array.length;i++){
//if(array[i][0]!=null){
//System.out.println(Arrays.deepToString(array));}
//}
System.out.println("Which entry would you like to edit? "
+ "\n" + "Enter number 0 - 20");
j = input.nextInt();
System.out.println("What would you like to edit? "
+"\n" + "Enter number 0 - 5");
g = input.nextInt();
System.out.println("You are now editing.");
k = input.next();
array[j][g] = k;
return array;}
更新
我想我解决了我的问题。该数组正确编辑我手动输入的值。当我将数据加载到数组中时会导致问题,因为当它加载数据时,它会以字符串 [] 的形式加载。我需要一个将数据加载为 String[][] 或数组数组的代码。
public static String[][] load()throws FileNotFoundException, IOException{
menu();
copyFile();
String file = ("c:/temp/Address.txt");
Scanner scan = new Scanner(new FileReader(file));
// initialises the scanner to read the file file
//String[][] entries = new String[100][3];
// creates a 2d array with 100 rows and 3 columns.
//int i = 0;
while(scan.hasNextLine()){
array[i][i] = scan.next().split("," , "\t");
i++;
}
//loops through the file and splits on a tab
//for (int row = 0; row < array.length; row++) {
// for (int col = 0; col < array[0].length; col++) {
// if(array[row][col] != null){
// System.out.print(array[row][0] );
// }
// }
// if(array[row][0] != null){
// System.out.print("\n");
//}
// }
//prints the contents of the array that are not "null"
selectMenu();
return array;}
更新 2
我找到了解决加载数据问题的解决方案。解决方法很简单!我将代码留在这里以供参考。不过,所有的代码都可以使用一些美化。
public static String[][] load()throws FileNotFoundException, IOException{
menu();
copyFile();
String file = ("c:/temp/Address.txt");
Scanner scan = new Scanner(new FileReader(file));
FileReader fr = new FileReader("c:/temp/Address.txt");
BufferedReader br = new BufferedReader(fr);
//int j=0;
//int lineNo = 0;
//String line = br.readLine();
//while(line!=null)
//{
//for(int i = 0; i < 5; i++)
//{
//array[lineNo][i] = line.substring(i,4);
//}
// lineNo++;
//line = br.readLine(); // This is what was missing!
//}
//while(scan.hasNextLine()){
//while(scan.hasNext()){
//for(j=0;j<5;j++){
for(int i = 0; i < 20; ++i){
for(int j = 0; j < 5; ++j)
{
if(scan.hasNext())
{
array[i][j] = scan.next();
//i++;
//j++;
}
//i++;
}
}
selectMenu();
return array;}
更新 3
所以在弄清楚如何使用分隔符之后,它给了我一个奇怪的问题。它在列的末尾添加 return。 [空,空,空,空,空返回]。我使用 ",|\n" 作为分隔符。有没有更好的方法?更新:添加了 .trim();解决负载的最终问题。现在它在目前的工作中得到了完善。不过,我敢肯定,可能会有不那么原始的方法。
public static String[][] load()throws FileNotFoundException, IOException{
copyFile();
//delimiter removes the comma or return to the next line. "\n" new line
Scanner scan = new Scanner(new FileReader(file)).useDelimiter(",|\n");
for(int i = 0; i < 20; ++i){
for(int j = 0; j < 5; ++j){
if(scan.hasNext())
array[i][j] = scan.next().replace(",", "").trim();
}
}
System.out.println("File loaded successfully!!");
scan.close();
return array;}
【问题讨论】:
标签: java arrays multidimensional-array