2016-11-29 15 views
0

私はこのプログラムをテキストファイルから読み込み、各行に "、"で区切ったデータがあります。コンマの左側にはアルファベットがあり、コンマの右側には値があります。私はアルファベットを入力し、私のプログラムにファイルのコンマの右側にある数値の値を与えたいと思っています。 しかし、何らかの形でプログラムは何も出力していませんが、私もelse条件で試してみましたが、while状態になっています。誰かが私を助けてください。入力された文字列またはアルファベットをデコードする

import java.io.*; 
import java.util.*; 
public class Decoding_message_tom1{ 

    public static void main(String[] args) throws IOException{ 
     BufferedReader br = new BufferedReader(new FileReader("I:\\Programming\\Java Tutorials\\sample.txt")); 

     // creating Link List 
     LinkedList<Decoding_node_tom1> list1 = new LinkedList<Decoding_node_tom1>(); 

     // checking if line is not empty   
     String line; 
     while ((line = br.readLine()) != null) 
     { 
      // splitting the line with delimiter and storing in an array. 
      String data[] = line.split(","); 

      // Object creation for passing the values in constructor of Decoding_node_tom1 class. 
      Decoding_node_tom1 obj1 = new Decoding_node_tom1(data[0], data[1]); 

      // inserting data as data[0] and data[1] in LinkedList object. 
      list1.add(obj1); 
     } 

     // scanner class to scan what user has input 
     Scanner scn = new Scanner(System.in); 

     System.out.println("Enter Your encoded message....."); 

     // Storing entered Input from user in str1 variable 
     String str1 = scn.nextLine(); 

     // Iterating through list1 
     Iterator itr = list1.iterator(); 


     // This will run until we have next values in iterator. 
     while(itr.hasNext()) 
     { 
      Decoding_node_tom1 var = (Decoding_node_tom1)itr.next(); 

      // Checking if the value entered is equal to values in sample.txt file 
      if(var.encode_value == str1) 
      { 
       System.out.println("Decode value for " + var.encode_value + " is " + var.decode_value); 
      } 
      else 
      { 
       System.out.println("Entered Encode value is Invalid.... "); 
       System.exit(0); 
      } 
     } 

    } 
} 



public class Decoding_node_tom1 { 

    String encode_value; 
    String decode_value; 

    // Constructor of the above class for assigning the values during the time of object creation. 
    Decoding_node_tom1(String encode_value, String decode_value) 
    { 
     this.encode_value = encode_value; 
     this.decode_value = decode_value; 
    } 
} 
+0

https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – David

答えて

1

あなたの問題が条件である:

if(var.encode_value == str1) 

if(var.encode_value.equals(str1)) 

あるべき理由は、標準入力を読み込む際に作成された文字列のインスタンスを解析する際に作成したものとは異なることですファイル:同じ値を持つ2つの異なるオブジェクト。 ==オペレータは、オブジェクト同一性を比較し、すなわち、2つの変数が同じオブジェクトインスタンスを参照しているかどうかをチェックする。 equals()関数は、オブジェクトの等価性をチェックします。つまり、2つの変数が指し示す2つのオブジェクトの「値」が同じであるかどうかをチェックします。アイデンティティは平等を意味しますが、その反対は真ではありません。

+0

ありがとう、助けのために現時点ではありがとうございました:) –

+1

満足です –

関連する問題