2017-01-18 5 views
3

私はプログラミングが初めてで、Javaを学ぶことにしました。私はちょうど一次元の配列についての読書を終えたばかりで、私は検索に問題があります。Java用の単一配列の文字列の問題

私が作成したこのプログラムの概要は、どのくらい多くの学生がクラスに登録されるかをユーザーに尋ねることです。ユーザは次に、配列の長さに基づいて学生の名前を入力する。それから私は、ユーザーに学生名を検索させることができるようにしたい。どのように私はこれを達成することができますか?私が達成したいのは、ユーザーがファーストネームを入力すると、ファーストネームが一致するフルネームのリストを返すことです。私は本当にこれで苦労しています。高度な方法を教えてください。私は自分の本のペースペースにとどまりたいです。

私は、Javaプログラミングの包括的なバージョン第10版の紹介を使用しています。

import java.util.Scanner; 

public class classSystem { 

    public static void main(String[] args) 
    { 
     Scanner input = new Scanner(System.in); 

     System.out.println("Weclome instructure to your Class System!"); 
     System.out.println("Follow each steps to turn in your work instructor."); 
     System.out.println("\n1.) Enroll Students:"); 

     System.out.print("\nHow many students are enrolled? "); 
     int studentAmount = input.nextInt(); 

     String[] enrolledStudents = getStudentAttendance(studentAmount); 

     System.out.println("Here is your attendance list:"); 

     for (int count = 0; count < enrolledStudents.length; count++) { 
      System.out.print("\n\t" + (count + 1) +  ".) " + enrolledStudents[count]); 
     } 

     System.out.print("\n\nWhat sudent do you want to search: "); 

     String studentSearch = input.nextLine(); 

     System.out.println(getStudent(enrolledStudents, studentSearch)); 
    } 

    public static String[] getStudentAttendance(int studentAmount) 
    { 
     Scanner input = new Scanner(System.in); 

     String[] enrolledStudents = new String[studentAmount]; 

     System.out.println("Input the students names:"); 
     for (int count = 0; count < enrolledStudents.length; count++) 
     { 
      System.out.print((count + 1) + ".) "); 
      enrolledStudents[count] = input.nextLine(); 
     } 

     return enrolledStudents; 
    } 

    public static String getStudent(String[] enrolledStudents, String StudentSearch) 
    { 
     for (int count = 0; count < enrolledStudents.length; count++) 
     { 
      if(StudentSearch.equals(enrolledStudents[count])) 
      { 
       return getStudent; 
      } 
     } 
    } 

} 
+1

アドバイス:インデントが非常に重要です。あなたの本の例を見てみると、 'if'と' for'と '{'の間の他のコードブロックの内容が1つインデントされていることがわかります。また、名前の表記法もあります。変数名とメソッド名はCamelCaseの方が低いですが、クラス、インターフェース、列挙名はCamelCaseの上位です。 – RealSkeptic

答えて

1

コードを更新しました。コメントはインラインでご覧ください。お役に立てれば。

import java.util.Scanner; 

class classSystem { 
    static Scanner input; //created a static reference for Scanner 
          //as you will be using in both the methods 

    public static void main(String[] args) { 
     input = new Scanner(System.in); //creating the Scanner object. 
     System.out.println("Weclome instructure to your Class System!"); 
     System.out.println("Follow each steps to turn in your work instructor."); 
     System.out.println("\n1.) Enroll Students:"); 

     System.out.print("\nHow many students are enrolled? "); 
     int studentAmount = input.nextInt(); 
     input.nextLine();  //added this to consume new-line leftover 

     String[] enrolledStudents = getStudentAttendance(studentAmount); 

     System.out.println("Here is your attendance list:"); 

     for (int count = 0; count < enrolledStudents.length; count++) { 
      System.out.print("\n\t" + (count + 1) + ".) " + enrolledStudents[count]); 
     } 

     System.out.print("\n\nWhat sudent do you want to search: "); 

     String studentSearch = input.nextLine(); 

     System.out.println(getStudent(enrolledStudents, studentSearch)); 
     input.close(); //close the scanner 
    } 

    public static String[] getStudentAttendance(int studentAmount) { 

     String[] enrolledStudents = new String[studentAmount]; 

     System.out.println("Input the students names:"); 
     for (int count = 0; count < enrolledStudents.length; count++) { 
      System.out.print((count + 1) + ".) "); 
      enrolledStudents[count] = input.nextLine(); 
     } 

     return enrolledStudents; 
    } 

    public static String getStudent(String[] enrolledStudents, String studentSearch) { 
     boolean flag = false; //added flag, this will be true if name is found 
           //otherwise false 
     for (int count = 0; count < enrolledStudents.length; count++) { 
      if (studentSearch.equals(enrolledStudents[count])) { 
       flag = true; 
       break;   //if name is found breaking the loop. 
      } else { 
       flag = false; 
      } 
     } 
     if (flag == true)  //checking the flag here 
      return studentSearch + " is present in the class"; 
     else 
      return studentSearch + " is not present in the class: "; 
    } 

} 

私のコードを実行した後、結果が下がっています。

enter image description here

+0

これは私が[Ljava.lang.String; @ 5146213いつも得る]です –

+0

私のコードを実行するとこのエラーが出ますか?どのような行でエラーが発生します –

+0

はい、私はあなたのコードを実行しているエラーを取得します。私のIDEはどの行を表示していません。しかし、私は学生が検索するように頼まれたときに私が入力を入力した後、私はショーをコメントしたそのエラー。 –

0

あなたはすでに.equals()メソッドを使用して検索する方法を知っているようです。あなたが "見つからない"状況を処理することによってgetStudent()メソッドを修正すると仮定すると、あなたは完了する必要があります。

次に、検索を改善したいですか、それは本当の質問ですか?それは、どのタイプの検索を実装したいかによって異なります。部分一致、名前の先頭、大文字/小文字の区別、ワイルドカード検索は異なるオプションです。それがあなたが望むものなら、それを質問に加えてください。

+0

私は編集をしました –

関連する問題