2016-09-10 4 views
-5

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をクリックするとファイルがプログラムに読み込まれます。

+3

このようなコードをスタックオーバーフローにダンプしないでください。[ヘルプ]と​​[mcve]を読んでください。まず、 'main'メソッドを終了するために中括弧が欠けています。 – Tunaki

答えて

2

メソッド内にメソッドを持つことはできません。コンパイラはpublic static int[][] getMap(Scanner file)があなたのメインメソッドの中にあると思っています。

関連する問題