2016-10-19 7 views
0

私は最初に得たものを含めるつもりです。オブジェクトとtoStringの配列

import java.util.Scanner; 

// Creates a class called presidents 
public class presidents3{ 
    // Initilaizes an object called input using the Scanner that is public. 
    public static Scanner input = new Scanner(System.in); 
    // Initializes a public static String variable called strInfo. 
    public static String strInfo; 
    // Initializes a public int variables number. 
    public static int number; 
    // Creates a main method that doesn't return anything. 
    public static void main(String[] args){ 
     // Initilaizes a String multidimensional array that holds the information about the presidents. 
     String presidents[][] = { 
     {"1 ","George"," ","Washington"," (1789-1797) ","John Adams"}, 
     {"2 ","John"," ","Adams"," (1797-1801) ","Thomas Jefferson"}, 
     {"3 ","Thomas"," ","Jefferson"," (1801-1809) ","Aaron Burr"}, 
     {"4 ","James"," ","Madison"," (1809-1817) ","George Clinton"}, 
     {"5 ","James"," ","Monroe"," (1817-1825) ","Daniel D. Tompkins"}, 
     {"6 ","John"," Quincy ","Adams"," (1825-1829) ","John C. Calhoun"}, 
     {"7 ","Andrew"," ","Jackson"," (1829-1837) ","John C. Calhoun"}, 
     {"8 ","Martin"," Van ","Buren"," (1837-1841) ","Richard M. Johnson"}, 
     {"9 ","William"," Henry ","Harrison"," (1841) ","John Tyler"}, 
     {"10 ","John"," ","Tyler"," (1841-1845) ","None"}, 
     {"11 ","James"," K. ","Polk"," (1845-1849) ","George M. Dallas"}, 
     {"12 ","Zachary"," ","Taylor"," (1849-1850) ","Millard Fillmore"}, 
     {"13 ","Millard"," ","Fillmore"," (1850-1853) ","None"}, 
     {"14 ","Franklin"," ","Pierce"," (1853-1857) ","William King"}, 
     {"15 ","James"," ","Buchanan"," (1857-1861) ","John C. Breckinridge"}, 
     {"16 ","Abraham"," ","Lincoln"," (1861-1865) ","Hannibal Hamlin"}, 
     {"17 ","Andrew"," ","Johnson"," (1865-1869) ","None"}, 
     {"18 ","Ulysses"," S. ","Grant"," (1869-1877) ","Schuyler Colfax"}, 
     {"19 ","Rutherford"," B. ","Hayes"," (1877-1881) ","William Wheeler"}, 
     {"20 ","James"," A. ","Garfield"," (1881) ","Chester Arthur"}, 
     {"21 ","Chester"," ","Arthur"," (1881-1885) ","None"}, 
     {"22 ","Grover"," ","Cleveland"," (1885-1889) ","Thomas Hendricks"}, 
     {"23 ","Benjamin"," ","Harrison"," (1889-1893) ","Levi P. Morton"}, 
     {"24 ","Grover"," ","Cleveland"," (1893-1897) ","Adlai E. Stevenson"}, 
     {"25 ","William"," ","McKinley"," (1897-1901) ","Garret Hobart"}, 
     {"26 ","Theodore"," ","Roosevelt"," (1901-1909) ","None"}, 
     {"27 ","William"," Howard ","Taft"," (1909-1913) ","James S. Sherman"}, 
     {"28 ","Woodrow"," ","Wilson"," (1913-1921) ","Thomas R. Marshall"}, 
     {"29 ","Warren"," G. ","Harding"," (1921-1923) ","Calvin Coolidge"}, 
     {"30 ","Calvin"," ","Coolidge"," (1923-1929) ","None"}, 
     {"31 ","Herbert"," ","Hoover"," (1929-1933) ","Charles Curtis"}, 
     {"32 ","Franklin"," D. ","Roosevelt"," (1933-1945) ","John Nance Garner"}, 
     {"33 ","Harry"," S. ","Truman"," (1945-1953) ","None"}, 
     {"34 ","Dwight"," D. ","Eisenhower"," (1953-1961) ","Richard Nixon"}, 
     {"35 ","John"," F. ","Kennedy"," (1961-1963) ","Lyndon B. Johnson"}, 
     {"36 ","Lyndon"," B. ","Johnson"," (1963-1969) ","None"}, 
     {"37 ","Richard"," ","Nixon"," (1969-1974) ","Spiro Agnew"}, 
     {"38 ","Gerald"," ","Ford"," (1974-1977) ","Nelson Rockefeller"}, 
     {"39 ","Jimmy"," ","Carter"," (1977-1981) ","Walter Mondale"}, 
     {"40 ","Ronald"," ","Reagan"," (1981-1989) ","George Bush"}, 
     {"41 ","George"," ","Bush"," (1989-1993) ","Dan Quayle"}, 
     {"42 ","Bill"," ","Clinton"," (1993-2001) ","Al Gore"}, 
     {"43 ","George"," W. ","Bush"," (2001-2009) ","Dick Cheney"}, 
     {"44 ","Barack"," ","Obama"," (2009-2017) ","Joe Biden"}, 
     }; 
     President[] President = new President[44]; 
     // Welcomes user and asks for input which will appear on the same line that asks for your input. 
     System.out.println("This will display the President and VP of the United States based on the number you provide."); 
     System.out.print("Please enter a number between 1 and 44 to see information or q to quit: "); 
     strInfo = input.nextLine(); 
     // Creates a while loop and checks if the strInfo equals q. 
     while (!strInfo.equals("q")){ 
      // Calls the isInteger method and passes the strInfo and sees if strInfo can become an int. 
      if (isInteger(strInfo)){ 
       // If the boolean is true, number will be set to Integer.parseInt(strInfo) which converts the String to int. 
       number = Integer.parseInt(strInfo); 
       if (number >= 1 && number <= 44){ 
        // Prints out the information. 
        President President = new President ("1 ","George"," ","Washington"," (1789-1797) ","John Adams"); 
        System.out.println(); 
        System.out.println(Presidents.toString()); 
        System.out.println(); 
        // Asks for input once again 
        System.out.print("Please enter a number between 1 and 44 to see information or q to quit: "); 
        strInfo = input.nextLine(); 
       }else{ 
        // Asks for input once again when you have entered an invalid number. 
        System.out.println(); 
        System.out.print("Wrong Input! Please enter number 1-44 or q to quit: "); 
        strInfo = input.nextLine(); 
       } 
      }else{ 
       // Asks for input once again when you have entered an invalid number and a String that is not q. 
       System.out.println(); 
       System.out.print("Wrong Input! Please enter number 1-44 or q to quit: "); 
       strInfo = input.nextLine(); 
      } 
     } 
     // Happens after you input q. 
     System.out.println(); 
     System.out.println("This program has been terminated. Good Bye!"); 
    } 

    // Creates a boolean method isInteger that takes the strInfo and checks if it can become an integer. 
    public static boolean isInteger(String strInfo){ 
     // Attempts to convert strInfo to int and looks for something to catch. If it is null or not a number it will return false. 
     try{ 
      Integer.parseInt(strInfo); 
     }catch(NumberFormatException e){ 
      return false; 
     }catch(NullPointerException e){ 
      return false; 
     } 
     // If false wasn't returned at all during this method, true will be returned. 
     return true; 
    } 
} 

他のクラス:

public class President{ 
    String number; 
    String firstName; 
    String middleName; 
    String lastName; 
    String years; 
    String vicePresident; 

    public President(String num, String fName, String mName, String lName, String yrs, String vp){ 
     number = num; 
     firstName = fName; 
     middleName = mName; 
     lastName = lName; 
     years = yrs; 
     vicePresident = vp; 
    } 

    public String toString(){ 
     return String.format("Our %s was %s %s %s. /n He was in office from %s. /n His Vice President was %s.", this.number, this.firstName, this.middleName, this.lastName, this.years, this.vicePresident); 
    } 
} 

は、ここで私はプロジェクトの一環として、何をしようとしているのです。私は2つのクラスの中でそれをするように指示されました。私は、toString()メソッドを使って適切にフォーマットしてn番目の社長が社長だったと言っているデータの配列からその情報を保持するコンストラクタを通じてオブジェクトの配列を作成しようとしています。それから年と副大統領。私は要請に応じてそれ以上説明することができます。私はまだそれを入力から取り除くために必要とし、それでも1〜44または "q"でないものは拒否します。誰もがアイデアを持っているか、それは私がそれを動作させるために見逃しているかのように見えるかを知っていますか?どんな助けでも大歓迎です。ここで

+0

あなたの現在のコードが生成する行動は何ですが? – donkon

+0

現在、2番目のクラスは何もしていませんが、現在、qとともにPresidentsに関する情報を表示するための文字列入力を受け入れます。私はtoString()メソッドを使用してthisキーワードを使用して適切にフォーマットするために、別のクラスを通してオブジェクトの配列を作成しようとしています。 – jweaver

+0

コード内のすべてが静的であるのはなぜですか? – scottb

答えて

1

は、オブジェクトの配列を作成することができる方法である:

​​

は、次にパラメータを渡します。

presidents[0] = new President ("1 ","George"," ","Washington"," (1789-1797) ","John Adams"); 

またはあなたがこのような何か行うことができます:

for(int i=0;i<44;i++){ 

     presidents = new President(YourString[i][0],YourString[i][1],YourString[i][2],YourString[i][3],YourString[i][4]); 

} 
+0

トップとボトムのコードはどのクラスに入るべきですか? – jweaver

+0

あなたのメインクラス。また、コンストラクタメソッドのシグニチャまたはセカンダリクラスの名前を社長に変更してください。 –

+0

Ok。 forループをどこに置くべきか、またはforループをどこに置き換えるべきですか? – jweaver

関連する問題