私はこのプログラムをテキストファイルから読み込み、各行に "、"で区切ったデータがあります。コンマの左側にはアルファベットがあり、コンマの右側には値があります。私はアルファベットを入力し、私のプログラムにファイルのコンマの右側にある数値の値を与えたいと思っています。 しかし、何らかの形でプログラムは何も出力していませんが、私も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;
}
}
https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – David