2017-03-16 22 views
1

割り当て用にクラスを処理するメニューを作成しようとしています。試したいクラスがありますが、メニューの作成に問題があるようです。変数が初期化されていません

switch(choice) 
{ 
    case 1: 
     System.out.print("Please enter a filename: "); 
     filename = option.next(); 

     //creating objects of the file manager to open the required files 
     FileManager e = new FileManager("Z:\\Java\\College\\Robo-Reader\\src\\"+filename+".txt"); 
     FileManager e1 = new FileManager("Z:\\Java\\College\\Robo-Reader\\src\\punctuation.txt"); 

     //creating connection to the files 
     e.connectToFile(); 
     e1.connectToFile(); 

     //Reading the files 
     String[] fileToBeRead= e.readFile(); 
     String[] punctMarks = e1.readFile(); 

     //closing the files 
     e.closeReadFile(); 
     e1.closeReadFile(); 
     fileRead++; 
     break; 

    case 2: 

     if(fileRead == 0) 
     { 
      System.out.println("No file was read. Please open a file to correct this error:"); 

     } 
     else 
     { 
      FindLan t3 = new FindLan(fileToBeRead); 
      t3.cLang(); 
     } 
     break; 

    case 3: 
     if(fileRead == 0) 
     { 
      System.out.println("No file was read. Please open a file to correct this error:"); 

     } 
     else 
     { 

      //Creating the object for the remove punctuation class 
      RemovePunct t1 = new RemovePunct(punctMarks, fileToBeRead); 

      //Calling the EndArray(Remove) method to clean an array of punctuation marks 
      String[] cleanWords = t1.EndArray(); 

      for(int i=0; i<10; i++) 
      { 
       System.out.println(cleanWords[i]); 
      } 
     } 
    default: 
     System.out.println("Option is not available"); 
}  

だから私は、ケース2及び3にケース1から変数を使用できるようにする必要がありますが、私は彼らが配列の長さを取得するには、ケース1で初期化する必要があります。

これまでのところtry/catchブロックを使用しようとしましたが、問題を解決していないようです。ケース2/3で値を与えなくても、他のケースでケース1から初期化された値をどのように使用することができるかについての他のアイデアはありますか?

主な目標は、サイズや要素をfilemanagerクラスから取得し、サイズや要素を定義することなく、他の2つのケースで使用するように、ケース1で定義された配列を使用できるようにすることです。

+0

全体的な目標は、配列が私のファイルマネージャクラスからそのサイズを受け取ることです。私はそこにサイズと要素を設定しているので、スイッチの外で初期化することはできません。さらに、私はそれらの返された配列をcase2、case3で他のクラスに使う必要があります。だから、case2とcase3のcase1の2つの配列を、自分自身に値を与えることなく使用できるようにしたい。 –

答えて

2
は次のようにswitchステートメントの外側で変数宣言を移動し

FileManager e = null; 
FileManager e1 = null; 
String[] fileToBeRead; 
String[] punctMarks; 

switch(choice) 
{ 
    case 1: 
     System.out.print("Please enter a filename: "); 
     filename = option.next(); 

     //creating objects of the file manager to open the required files 
     e = new FileManager("Z:\\Java\\College\\Robo-Reader\\src\\"+filename+".txt"); 
     e1 = new FileManager("Z:\\Java\\College\\Robo-Reader\\src\\punctuation.txt"); 

     //creating connection to the files 
     e.connectToFile(); 
     e1.connectToFile(); 

     //Reading the files 
     fileToBeRead= e.readFile(); 
     punctMarks = e1.readFile(); 

     //closing the files 
     e.closeReadFile(); 
     e1.closeReadFile(); 
     fileRead++; 
     break; 

    case 2: 

     if(fileRead == 0) 
     { 
      System.out.println("No file was read. Please open a file to correct this error:"); 

     } 
     else 
     { 
      FindLan t3 = new FindLan(fileToBeRead); 
      t3.cLang(); 
     } 
     break; 

    case 3: 
     if(fileRead == 0) 
     { 
      System.out.println("No file was read. Please open a file to correct this error:"); 

     } 
     else 
     { 

      //Creating the object for the remove punctuation class 
      RemovePunct t1 = new RemovePunct(punctMarks, fileToBeRead); 

      //Calling the EndArray(Remove) method to clean an array of punctuation marks 
      String[] cleanWords = t1.EndArray(); 

      for(int i=0; i<10; i++) 
      { 
       System.out.println(cleanWords[i]); 
      } 
     } 
    default: 
     System.out.println("Option is not available"); 
} 
+1

これでは十分ではありません。変数は初期化できないので、コンパイラーをパスしません。その1つは宣言でnull値を与えることです。 'FileManager e = null;'のようにコンパイラを渡しますが、変数がnullの場合、case 2と3の状況を処理する必要があります。 – Vadim

+0

コンパイルエラーをバイパスする必要があって、ifsを使用して変数が初期化されていることを確認していただきありがとうございます。どうもありがとう。 –

+0

ようこそ。それは通常の練習です。 – Vadim

関連する問題