以下のコードは以下のCSVファイルの中から引数playerNameと同じものがあったら成功と表示するプログラムです。
しかしplayerNameをCSVファイルの中にあるkenやryoにしてもifの分岐で必ずelseの方に入ってしまいます。
どう改善したらいいでしょうか。
result.csv
name,win,lose
ken,2,0
hayato,3,2
ryo,0,3
java
1package save.function; 2 3import java.io.FileNotFoundException; 4import java.io.FileReader; 5import java.io.IOException; 6import java.util.List; 7 8import org.supercsv.io.CsvListReader; 9import org.supercsv.io.ICsvListReader; 10import org.supercsv.prefs.CsvPreference; 11 12public class SaveResult { 13 14 15 public static void saveResult( String playerName) throws Exception { 16 17 try { 18 ICsvListReader reader = new CsvListReader( new FileReader( "./result/result.csv"), CsvPreference.STANDARD_PREFERENCE); 19 List<String> list = null; 20 21 while ( (list = reader.read()) != null) { 22 for ( String str : list) { 23 if ( str == playerName) { 24 System.out.println( "成功"); 25 } 26 else { 27 System.out.println( str); 28 } 29 } 30 System.out.println(); 31 } 32 } 33 catch ( FileNotFoundException e) { 34 System.out.println( e); 35 } 36 catch ( IOException e) { 37 System.out.println( e); 38 } 39 } 40}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。