所以我试图打开/关闭/重新打开/读取并将数组转换为1维数组和2维数组。
public class Assigntment2Condensed
{
public static void main (String[] args) throws IOException
{
//Creating the menu options
System.out.println("\n\nPlease choose an option below\n"
+ "\tA. Read Data\n"
+ "\tB. Modify Data\n"
+ "\tC. Clear Data\n"
+ "\tD. Display Data\n"
+ "\tE. Save Data to a file\n"
+ "\tF. Quit");
System.out.print("\n=> ");
//WHAT I NEED: While loop and a nested If statement
//getting user input:: can create a method
String input = keyboard.nextLine();
input = input.toUpperCase(); //what the user inputs will be made to an upper case char
char charInput = input.charAt(0); //gets the character in the 0 position
//main methods
int[][] originalMap = getMap(new Scanner(new File("map.dat"))); //reads data into the array :: Menu Option A
getShowArray(originalMap); //passing contents of original map into the method :: Menu Option D
getWriteArray(modifiedMap); //method created to write the contents of the modified map to a file. :: Menu Option B
// Method classes
//Read in array from file to a two dimen. array. returns array Map. :: Menu Option A
public static int[][] getMap(Scanner file)
{
int index = 0;
String map;
Scanner keyboard = new Scanner(System.in);
System.out.println("\n\nOpening 'map.dat' file and adding elements to the array....\n");
//open the file
File file = new File("map.dat");
Scanner openFile = new Scanner(file); //reads in the file into the openFile instance
//read the file contents
while(openFile.hasNext())
{
map = openFile.nextLine();
//System.out.println(map);
index++;
}
openFile.close();
//read contents into an array
File map2 = new File("map.dat");
Scanner openFile2 = new Scanner(map2);
int[] numArray = new int[index]; //creates Array
for(int i = 0; i < numArray.length; i++)
{
numArray[i] = openFile2.nextInt();
//System.out.print(numArray[i] + " " );
}
//-=-===-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=
int rows = file.nextInt();
int columns = file.nextInt();
int[][] map = new int[rows][columns];
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
map[i][j] = file.nextInt();
System.out.print(map[i][j] + " ");
}
System.out.println("");
}
file.close();
return map;
}
//displaying the array when called :: Menu Option D
private static void getShowArray(int[][] originalMap)
{
for (int row = 0; row < originalMap.length; row++)
{
for(int col = 0; col < originalMap.length; col++)
System.out.print(originalMap[row][col] + " | ");
System.out.println();
}
}
//writing modifiedMap array to a txt file :: Menu Option B
public static void getWriteArray(int[][] modifiedMap)
{
dataInputStream inputFile = new DataInputStream(new fileInputStream("map.dat"));
System.out.println("\nWriting to the 'map.dat' file...");
//write to binary file
for (int i = 0; i < modifiedMap.length; i++)
outputFile.writeInt(modifiedMap[i]);
//close file
outputFile.close();
System.out.println("Done.");
}
}
我收到错误:
Assigntment2Condensed.java:30: error: illegal start of expression
public static int[][] getMap(Scanner file)
^
Assigntment2Condensed.java:30: error: illegal start of expression
public static int[][] getMap(Scanner file)
^
Assigntment2Condensed.java:30: error: ';' expected
public static int[][] getMap(Scanner file)
^
Assigntment2Condensed.java:30: error: illegal start of expression
public static int[][] getMap(Scanner file)
^
Assigntment2Condensed.java:30: error: ';' expected
public static int[][] getMap(Scanner file)
^
Assigntment2Condensed.java:30: error: illegal start of expression
public static int[][] getMap(Scanner file)
^
Assigntment2Condensed.java:30: error: ';' expected
public static int[][] getMap(Scanner file)
^
Assigntment2Condensed.java:30: error: ')' expected
public static int[][] getMap(Scanner file)
^
Assigntment2Condensed.java:30: error: illegal start of expression
public static int[][] getMap(Scanner file)
^
Assigntment2Condensed.java:30: error: ';' expected
public static int[][] getMap(Scanner file)
^
10 errors
“= - = - = - = - = - = - = - = - = - = - = - = - ”表示该行之前的代码以前在该方法之外且以下任何内容已经在那种方法中工作了。我需要创建一个菜单选项,以便在他们单击A时将文件读入程序。
答案 0 :(得分:2)
您无法在方法中使用方法。编译器认为public static int[][] getMap(Scanner file)
在您的主要方法中,并且它可能不是您所追求的。