2016-11-25 2 views
-1

で変数の内容を取得することができません:私はこれらの変数の内容を取得する問題が持っているJavaの

try{ 
      InputStream flux = new FileInputStream("file.txt"); 
      InputStreamReader read = new InputStreamReader(flux); 
      BufferedReader buff = new BufferedReader(read); 
      String ligne, ligne1; 

      while ((ligne = buff.readLine())!=null) 
      { 
       if(ligne.startsWith("k")) 
       { 
        first = Integer.parseInt(ligne.substring(6, 9)); 
        second = Integer.parseInt(ligne.substring(10, 13)); 
       }    
      } 

      while ((ligne1 = buff.readLine())!=null) 
      { 
       if(ligne1.startsWith("e")) 
       { 
        int ind1 = ligne1.lastIndexOf(" "); 
        n1 = Integer.parseInt(ligne1.substring(2, ind1)); 
        n2 = Integer.parseInt(ligne1.substring(ind1+1)); 
       } 
      } 
      ... 
      // Displaying of their content 
      System.out.println(first); // ok 
      System.out.println(second); // ok 
      System.out.println(n1); // doesn't work content 0 
      System.out.println(n2); // doesn't work content 0 

私は彼らのコンテンツを表示したくない:怒鳴るコードでは、N1らN2をしばらく。 ありがとうございます!

+2

「System.out.println(n1);」は「ok」ですが、「System.out.println(n1);」は「コンテンツ0は動作しません」というのはなぜですか? –

+0

エラー、それは秒でした。私はすでにそれを修正しました。 – hitech

+0

入力とは何ですか?あなたは何を期待していますか?何を手に入れますか? n1、n2、firstとsecondはどこに宣言されていますか? – Heri

答えて

0

whileループにSystem.out.printlnを転送します。

0

あなたがfirstsecond移入するためにバッファ全体をお読みください。

while ((ligne = buff.readLine())!=null) 
{ ... } 

をし、その後n1n2移入するためにバッファからより多くの行を取得しよう:もちろん

while ((ligne = buff.readLine())!=null) 
{ ... } 

を、バッファ空になりました - 最初のループのすべての行を読み込みます。したがって、ループ本体は決して実行されないので、n1n2は設定されません。

第1内側第二のループの内容を入れて:第二のループ前

 while ((ligne = buff.readLine())!=null) 
     { 
      if(ligne.startsWith("k")) 
      { 
       first = Integer.parseInt(ligne.substring(6, 9)); 
       second = Integer.parseInt(ligne.substring(10, 13)); 
      } 

      /* else */ if (ligne.startsWith("e")) 
      { 
       int ind1 = ligne1.lastIndexOf(" "); 
       n1 = Integer.parseInt(ligne1.substring(2, ind1)); 
       n2 = Integer.parseInt(ligne1.substring(ind1+1)); 
      } 
     } 

またはbuff再オープンします。

+0

あなたのお返事ありがとうございます!だから、私はループからそれらを使用したいので、最初の内側に2番目のループの内容を入れることはできません。私はちょうどあなたの第二の命題を試みたが、うまくいかなかった。おそらく私は間違いを犯しました。あなたは私に例を教えてもらえますか?私はJavaの初心者です。 – hitech

+0

あなたは間違っている必要があります。最初のループに2番目のループを置いても、1番目または2番目のループのいずれの変数のスコープも制限されません。私は私の答えで '...'を展開しましたが、実際には貼り付け+貼り付けだけです。 –

+0

それは私がやったことですが、私はまだ同じ問題があります – hitech

関連する問題