私は、ユーザーに名前、チケットの種類、および罰金を入力するように求めるプログラムを書いています。ユーザー入力はテキストファイルに格納されています。私はテキストファイルを 'list'という名前の配列に読み込み、ユーザに 'キーワード'を検索させるように促しています。私はキーワードをString =キーワードとして保存しています。 println(list.indexOf(キーワード))を使用すると、検索対象のキーワードが見つかったにもかかわらず "-1"の出力が得られます!それは私が立ち往生しているところです...私はなぜindexOf()メソッドが要素を見つけることができないのか分かりませんが、キーワードの検索は可能です。どんな助けでも大歓迎です!注:私の最終目標は "キーワード"を検索し、要素の位置を見つけて真を返すことです。私はその後、新しい名前、チケットの種類と罰金についてユーザーに尋ねるでしょう。私はテキストファイルの情報を置き換えて、先に述べたテキストファイルに新しい情報を書きます。事前に入力していただきありがとうございます。コードが含まれています(私はJavaの初心者ですので素敵にしてください:0)私はプログラムを終了したらコードを整理します。正しく問題を理解している場合、下記のコードは、あなたがJava ArrayListの検索と要素位置の返却 - indexOf()が機能していません。
list.add("for example")
list.add("for")
list.add("example")
System.out.println(list.indexOf("for example"))
System.out.println(list.indexOf("for"))
System.out.println(list.indexOf("example"))
のindexOfは要素内のキーワードを検索しません役立ちます
import java.util.*;
import java.io.*;
import java.text.Collator;
public class Project {
public static void main(String[] args) throws IOException {
String name = "";
String ticketType = "";
double fine = 0;
char quit;
boolean cont = true;
while (cont){
do {
//Create file Project.txt
PrintStream out = new PrintStream(new FileOutputStream("Project.txt", true));
Scanner input = new Scanner(System.in);
//Prompt user to enter name, ticket type, fine or 'q' if done
System.out.println("Name:");
name = input.nextLine();
System.out.println("Ticket Type:");
ticketType = input.nextLine();
System.out.println("fine:");
fine = input.nextDouble();
System.out.println("press 'q' if you are done entering or 'c' to continue entering");
quit = input.next().toLowerCase().charAt(0);
//Write user input to Project.txt file
out.print(name + ", ");
out.print(ticketType + ", ");
out.printf("%.2f",fine);
out.println();
out.close();
}
while (!(quit == 'q'));{
System.out.println("done");
}
Scanner input = new Scanner(System.in);
System.out.println("are you sure youre done");
System.out.println("enter y to if your done, n to enter more");
char Continue = input.next().toLowerCase().charAt(0);
//Prompt user in the are done entering
//If 'y', program moves on. If 'n' loop back to beginning
if (Continue == ('y')){
cont = false;
}
//Done entering names to list
//Now read in file to array and sort alphabetically
}
Scanner readArray = new Scanner(new File("Project.txt"));
ArrayList<String> list = new ArrayList<String>();
while (readArray.hasNext()){
list.add(readArray.nextLine().toLowerCase());
}
readArray.close();
Collections.sort(list, Collator.getInstance()); //sort array alphabetically
//print array as list comma separated
for (String value : list){
System.out.println(value);
}
//Prompt user to enter a name they want to search for
Scanner search = new Scanner(System.in);
System.out.println("search keyword: ");
String keyword = search.next().toLowerCase();
System.out.println("searching for: " + keyword);
//Search array 'list' for user input
boolean found = false;
for (String value : list) {
if (value.contains(keyword)) {
found = true;
}
}
if (found) {
System.out.println("found");
System.out.println(list.indexOf(keyword));
}
else {
System.out.println(" not found");
}
}
}
あなたが上がりませんリストにキーワードが含まれているかどうかを確認してください。あなたのリストの文字列にキーワードが含まれているかどうかをチェックしています。あなたのListに文字列 '' Something Test ''があり、' 'Something" 'をキーワード' 'IndexOf(" Something ")で検索すると、-1が返されますが、 "Something Test" .contains( "Something") '真を返す。 –
さらに、リストに要求された値が含まれているかどうかを調べるための別個のループは必要ありません。 indexOf()を呼び出すことができ、返された値が-1の場合、検索に失敗したことがわかります –
フィードバックありがとうございます!私は "Something Test" .contains( "Something")を探していて、正しい返答を得ています。私が探している文字列が戻り値を見つけました。私が苦労しているのは、配列内の要素位置を返すことです。私は、要素を返すし、set(int index、element)を使いたい。 indexOf()が機能していないため(場所に-1を返すため)、要素の位置を取得する方法を知る必要があります。 –