4

I'm trying to create a program that is a quiz (of American presidents). I want to create an array list (president number, president name, starting year, last year).

For some reason, my constructor does not work and I had to build another method but that second method causes a StackOverFlowError (this is the first time I think that I am using this website for a question and I think I came to the right place given the name of the site)

Here is my code:

public class Presidents 
{   

    public int presidentNumber;
    public String presidentName;
    public int startingYear;
    public int endingYear;

    public Presidents(int PresidentNumber, String NameOfPresident, int StartingYear, int EndingYear)
    {
        this.presidentNumber = PresidentNumber;
        this.presidentName = NameOfPresident;
        this.startingYear = StartingYear;
        this.endingYear = EndingYear;

    }

    public Presidents() {

    }

    public Presidents [] PresidentList () 
    {           
        Presidents [] president = new Presidents [45];
        president [0] = Presidents(1, "George Washington", 1789, 1797);
        president [1] = Presidents(2, "John Adams", 1797, 1801);
        president [2] = Presidents(3, "Thomas Jefferson", 1801, 1809);
        president [3] = Presidents(4, "James Madison", 1809, 1817);
        president [4] = Presidents(5, "James Monroe", 1817, 1825);
        president [5] = Presidents(6, "John Quincy Adams", 1825, 1829);
        president [6] = Presidents(7, "Andrew Jackson", 1829, 1837);
        president [7] = Presidents(8, "Martin Van Buren", 1837, 1841);
        president [8] = Presidents(9, "William Henry Harrison", 1841, 1841);
        president [9] = Presidents(10, "John Tyler", 1841, 1845);
        president [10] = Presidents(11, "James K. Polk", 1845, 1849);
        president [11] = Presidents(12, "Zachary Taylor", 1849, 1850);
        president [12] = Presidents(13, "Millard Fillmore", 1850, 1853);
        president [13] = Presidents(14, "Franklin Pierce", 1853, 1857);
        president [14] = Presidents(15, "James Buchanan", 1857, 1861);
        president [15] = Presidents(16, "Abraham Lincoln", 1861, 1865);
        president [16] = Presidents(17, "Andrew Johnson", 1865, 1869);
        president [17] = Presidents(18, "Ulysses S. Grant", 1869, 1877);
        president [18] = Presidents(19, "Rutherford B. Hayes", 1877, 1881);
        president [19] = Presidents(20, "James A. Garfield", 1881, 1881);
        president [20] = Presidents(21, "Chester A. Arthur", 1881, 1885);
        president [21] = Presidents(22, "Grover Cleveland", 1885, 1889);
        president [22] = Presidents(23, "Benjamin Harrison", 1889, 1893);
        president [23] = Presidents(24, "Grover Cleveland", 1893, 1897);
        president [24] = Presidents(25, "William McKinley", 1897, 1901);
        president [25] = Presidents(26, "Theodore Roosevelt", 1901, 1909);
        president [26] = Presidents(27, "William Howard Taft", 1909, 1913);
        president [27] = Presidents(28, "Woodrow Wilson", 1913, 1921);
        president [28] = Presidents(29, "Warren G. Harding", 1921, 1923);
        president [29] = Presidents(30, "Calvin Coolidge", 1923, 1929);
        president [30] = Presidents(31, "Herbert Hoover", 1929, 1933);
        president [31] = Presidents(32, "Franklin D. Roosevelt", 1933, 1945);
        president [32] = Presidents(33, "Harry S. Truman", 1945, 1953);
        president [33] = Presidents(34, "Dwight D. Eisenhower", 1953, 1961);
        president [34] = Presidents(35, "John F. Kennedy", 1961, 1963);
        president [35] = Presidents(36, "Lyndon B. Johnson", 1963, 1969);
        president [36] = Presidents(37, "Richard Nixon", 1969, 1974);
        president [37] = Presidents(38, "Gerald Ford", 1974, 1977);
        president [38] = Presidents(39, "Jimmy Carter", 1977, 1981);
        president [39] = Presidents(40, "Ronald Reagan", 1981, 1989);
        president [40] = Presidents(41, "George H. W. Bush", 1989, 1993);
        president [41] = Presidents(42, "Bill Clinton", 1993, 2001);
        president [42] = Presidents(43, "George W. Bush", 2001, 2009);
        president [43] = Presidents(44, "Barack Obama", 2009, 2017);
        president [44] = Presidents(45, "Donald Trump", 2017, 2018);

        return president;
    }   

    private Presidents Presidents(int i, String string, int j, int k) {         
        return Presidents (i, string, j, k);
    }

}
import java.util.Scanner;
import java.util.Random;


public class PresidentsQuiz {

    public static void main (String [] args)    
    {       
        System.out.println("Do you know the 45 presidents of the United States? Enter a number between 1 and 45");

        Scanner kb = new Scanner (System.in);           
        int input =  kb.nextInt();

        while (input < 1 ||  input > 45)        
        {
            System.out.println("Enter a number between 1 and 45");
            input =  kb.nextInt();
        }

        Presidents president = new Presidents ();                   
        Presidents [] presidentList = president.PresidentList();                        
        System.out.println(presidentList [0]);
        kb.close();

    }

}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • As a side note, I recommend looking at the Java Naming Conventions [here](https://www.oracle.com/technetwork/java/codeconventions-135099.html). Following these conventions will help to make your code more readable. – Logan Dec 01 '18 at 03:36

4 Answers4

4

You have a method here:

public Presidents [] PresidentList ()

    {       


        Presidents [] president = new Presidents [45];
        president [0] = Presidents(1, "George Washington", 1789, 1797);
        president [1] = Presidents(2, "John Adams", 1797, 1801);
        president [2] = Presidents(3, "Thomas Jefferson", 1801, 1809);
        president [3] = Presidents(4, "James Madison", 1809, 1817);
        president [4] = Presidents(5, "James Monroe", 1817, 1825);
        //...
        return president;
}

However every call to:

president [...] = Presidents(...);

Will call:

private Presidents Presidents(int i, String string, int j, int k) {

    return Presidents (i, string, j, k);
}

Which recalls the same method with the same parameters, which will recurse forever, causing a Stack Overflow error.


Do not have a method that is called the same thing as the class, and returns something. This is reserved for the constructor. You already have a constructor that takes the proper arguments so simply delete the method:

private Presidents Presidents(int i, String string, int j, int k) {

    return Presidents (i, string, j, k);
}

And then in your method you need to add the new keyword:

president [0] = new Presidents(1, "George Washington", 1789, 1797);
                 ^--- Here
GBlodgett
  • 12,704
  • 4
  • 31
  • 45
  • When I tried to use the constructor there was an error. It says "The method Presidents(int, String, int, int) is undefined for the type Presidents" and it forces me to create that method (the one you told me to delete). – Laurent Cousineau Dec 01 '18 at 03:48
1

You need to instantiate a new President object while you are allocating at the particular position in the list.

Here Your method:-

private Presidents Presidents(int i, String string, int j, int k) {
        //does not Instantiate a new object
        return Presidents (i, string, j, k);
    }

-Here it does not instantiate a new object of class Presidents. It calls the same Presidents method and recursive call happen.

So here you need to instantiate your method with the new keyword.

    return new Presidents (i, string, j, k);

It points to the class and constructor will be invoked.

Dhiral Kaniya
  • 1,941
  • 1
  • 19
  • 32
1

You should be creating a new object of the Presidents class when you were initializing the array because the array should be storing objects of type Presidents. Instead you have called the constructor of the class alone. Instead of doing this :

president [0] = Presidents(1, "George Washington", 1789, 1797);

You should be doing this :

president [0] = new Presidents(1, "George Washington", 1789, 1797);

Do this for the rest of the array indexes as well. And this method :

private Presidents Presidents(int i, String string, int j, int k) {         
    return Presidents (i, string, j, k);
}

This is a wrong way to define a method. The method shouldn't be having the same name as the class. Only constructor should have it. Also if you want this method to return an object of the Presidents class then the return statement should be as follows :

return Presidents (i, string, j, k);

But this method is pointless as you already have PresidentList() method returning the array. That being said it would be better to make the variables inside the class private and define getter methods to access them. Anyway, I have made some modifications to your code and this should work :

public class Presidents 
{   

public int presidentNumber;
public String presidentName;
public int startingYear;
public int endingYear;
Presidents [] president;

public Presidents(int PresidentNumber, String NameOfPresident, int StartingYear, int EndingYear)
{
    this.presidentNumber = PresidentNumber;
    this.presidentName = NameOfPresident;
    this.startingYear = StartingYear;
    this.endingYear = EndingYear;

}

public Presidents() {

}

public Presidents [] PresidentList () 
{           
     president= new Presidents [45];
    president [0] = new Presidents(1, "George Washington", 1789, 1797);
    president [1] = new Presidents(2, "John Adams", 1797, 1801);
    president [2] = new Presidents(3, "Thomas Jefferson", 1801, 1809);
    president [3] = new Presidents(4, "James Madison", 1809, 1817);
    president [4] = new Presidents(5, "James Monroe", 1817, 1825);
    president [5] = new Presidents(6, "John Quincy Adams", 1825, 1829);
    president [6] = new Presidents(7, "Andrew Jackson", 1829, 1837);
    president [7] = new Presidents(8, "Martin Van Buren", 1837, 1841);
    president [8] = new Presidents(9, "William Henry Harrison", 1841, 1841);
    president [9] = new Presidents(10, "John Tyler", 1841, 1845);
    president [10] = new Presidents(11, "James K. Polk", 1845, 1849);
    president [11] = new Presidents(12, "Zachary Taylor", 1849, 1850);
    president [12] = new Presidents(13, "Millard Fillmore", 1850, 1853);
    president [13] = new Presidents(14, "Franklin Pierce", 1853, 1857);
    president [14] = new Presidents(15, "James Buchanan", 1857, 1861);
    president [15] = new Presidents(16, "Abraham Lincoln", 1861, 1865);
    president [16] = new Presidents(17, "Andrew Johnson", 1865, 1869);
    president [17] = new Presidents(18, "Ulysses S. Grant", 1869, 1877);
    president [18] =new Presidents(19, "Rutherford B. Hayes", 1877, 1881);
    president [19] =new Presidents(20, "James A. Garfield", 1881, 1881);
    president [20] =new Presidents(21, "Chester A. Arthur", 1881, 1885);
    president [21] =new Presidents(22, "Grover Cleveland", 1885, 1889);
    president [22] =new Presidents(23, "Benjamin Harrison", 1889, 1893);
    president [23] =new Presidents(24, "Grover Cleveland", 1893, 1897);
    president [24] =new Presidents(25, "William McKinley", 1897, 1901);
    president [25] =new Presidents(26, "Theodore Roosevelt", 1901, 1909);
    president [26] =new Presidents(27, "William Howard Taft", 1909, 1913);
    president [27] =new Presidents(28, "Woodrow Wilson", 1913, 1921);
    president [28] =new Presidents(29, "Warren G. Harding", 1921, 1923);
    president [29] =new Presidents(30, "Calvin Coolidge", 1923, 1929);
    president [30] =new Presidents(31, "Herbert Hoover", 1929, 1933);
    president [31] =new Presidents(32, "Franklin D. Roosevelt", 1933, 1945);
    president [32] =new Presidents(33, "Harry S. Truman", 1945, 1953);
    president [33] =new Presidents(34, "Dwight D. Eisenhower", 1953, 1961);
    president [34] =new Presidents(35, "John F. Kennedy", 1961, 1963);
    president [35] =new Presidents(36, "Lyndon B. Johnson", 1963, 1969);
    president [36] =new Presidents(37, "Richard Nixon", 1969, 1974);
    president [37] =new Presidents(38, "Gerald Ford", 1974, 1977);
    president [38] =new Presidents(39, "Jimmy Carter", 1977, 1981);
    president [39] =new Presidents(40, "Ronald Reagan", 1981, 1989);
    president [40] =new Presidents(41, "George H. W. Bush", 1989, 1993);
    president [41] =new Presidents(42, "Bill Clinton", 1993, 2001);
    president [42] =new Presidents(43, "George W. Bush", 2001, 2009);
    president [43] =new Presidents(44, "Barack Obama", 2009, 2017);
    president [44] =new Presidents(45, "Donald Trump", 2017, 2018);

    return president;
}   

}

And the main class :

import java.util.Scanner;
import java.util.Random;


public class PresidentsQuiz {

public static void main (String [] args)    
{       
    System.out.println("Do you know the 45 presidents of the United States? Enter a number between 1 and 45");

    Scanner kb = new Scanner (System.in);           
    int input =  kb.nextInt();

    while (input < 1 ||  input > 45)        
    {
        System.out.println("Enter a number between 1 and 45");
        input =  kb.nextInt();
    }

    Presidents president = new Presidents ();                   
    Presidents [] presidentList = president.PresidentList();                        
    //System.out.println(presidentList [0].presidentName);

    for(int i=0;i<input;i++)
    {
        Presidents p = presidentList[i];
        System.out.println(p.presidentNumber+", "+p.presidentName+", "+p.startingYear+", "+p.endingYear);
    }

    kb.close();

}

}

I think you were trying to print all the presidents up to the number you have input. If not you could remove the for loop in the main method.

arunken
  • 415
  • 3
  • 15
0

Solution

  • The logic to create list of Presidents has to be moved to the main class

Reason

  • The logic are the responsibilities of the program
  • Assigning responsibilities to various classes is critical
  • An object could be responsible for creating a single instance of its own using constructor. But not responsible for creating list of instances of its own using another instance method.
  • From your code, you will have a President object without any value in the fields. That dummy instance is responsible for creating real list of Presidents.

Code

public class Presidents {   

public int presidentNumber;
public String presidentName;
public int startingYear;
public int endingYear;

public Presidents(int PresidentNumber, String NameOfPresident, int StartingYear, int EndingYear)
{
    this.presidentNumber = PresidentNumber;
    this.presidentName = NameOfPresident;
    this.startingYear = StartingYear;
    this.endingYear = EndingYear;

}
public Presidents() {}
}

import java.util.Scanner;
import java.util.Random;


public class PresidentsQuiz {

public static void main (String [] args)    
{       
    System.out.println("Do you know the 45 presidents of the United States? Enter a number between 1 and 45");

    Scanner kb = new Scanner (System.in);           
    int input =  kb.nextInt();

    while (input < 1 ||  input > 45)        
    {
        System.out.println("Enter a number between 1 and 45");
        input =  kb.nextInt();
    }

    Presidents [] president = new Presidents [45];
    president [0] = new Presidents(1, "George Washington", 1789, 1797);
    president [1] = new Presidents(2, "John Adams", 1797, 1801);
    president [2] = new Presidents(3, "Thomas Jefferson", 1801, 1809);
    president [3] = new Presidents(4, "James Madison", 1809, 1817);
    president [4] = new Presidents(5, "James Monroe", 1817, 1825);
    president [5] = new Presidents(6, "John Quincy Adams", 1825, 1829);
    president [6] = new Presidents(7, "Andrew Jackson", 1829, 1837);
    president [7] = new Presidents(8, "Martin Van Buren", 1837, 1841);
    president [8] = new Presidents(9, "William Henry Harrison", 1841, 1841);
    president [9] = new Presidents(10, "John Tyler", 1841, 1845);
    president [10] = new Presidents(11, "James K. Polk", 1845, 1849);
    president [11] = new Presidents(12, "Zachary Taylor", 1849, 1850);
    president [12] = new Presidents(13, "Millard Fillmore", 1850, 1853);
    president [13] = new Presidents(14, "Franklin Pierce", 1853, 1857);
    president [14] = new Presidents(15, "James Buchanan", 1857, 1861);
    president [15] = new Presidents(16, "Abraham Lincoln", 1861, 1865);
    president [16] = new Presidents(17, "Andrew Johnson", 1865, 1869);
    president [17] = new Presidents(18, "Ulysses S. Grant", 1869, 1877);
    president [18] = new Presidents(19, "Rutherford B. Hayes", 1877, 1881);
    president [19] = new Presidents(20, "James A. Garfield", 1881, 1881);
    president [20] = new Presidents(21, "Chester A. Arthur", 1881, 1885);
    president [21] = new Presidents(22, "Grover Cleveland", 1885, 1889);
    president [22] = new Presidents(23, "Benjamin Harrison", 1889, 1893);
    president [23] = new Presidents(24, "Grover Cleveland", 1893, 1897);
    president [24] = new Presidents(25, "William McKinley", 1897, 1901);
    president [25] = new Presidents(26, "Theodore Roosevelt", 1901, 1909);
    president [26] = new Presidents(27, "William Howard Taft", 1909, 1913);
    president [27] = new Presidents(28, "Woodrow Wilson", 1913, 1921);
    president [28] = new Presidents(29, "Warren G. Harding", 1921, 1923);
    president [29] = new Presidents(30, "Calvin Coolidge", 1923, 1929);
    president [30] = new Presidents(31, "Herbert Hoover", 1929, 1933);
    president [31] = new Presidents(32, "Franklin D. Roosevelt", 1933, 1945);
    president [32] = new Presidents(33, "Harry S. Truman", 1945, 1953);
    president [33] = new Presidents(34, "Dwight D. Eisenhower", 1953, 1961);
    president [34] = new Presidents(35, "John F. Kennedy", 1961, 1963);
    president [35] = new Presidents(36, "Lyndon B. Johnson", 1963, 1969);
    president [36] = new Presidents(37, "Richard Nixon", 1969, 1974);
    president [37] = new Presidents(38, "Gerald Ford", 1974, 1977);
    president [38] = new Presidents(39, "Jimmy Carter", 1977, 1981);
    president [39] = new Presidents(40, "Ronald Reagan", 1981, 1989);
    president [40] = new Presidents(41, "George H. W. Bush", 1989, 1993);
    president [41] = new Presidents(42, "Bill Clinton", 1993, 2001);
    president [42] = new Presidents(43, "George W. Bush", 2001, 2009);
    president [43] = new Presidents(44, "Barack Obama", 2009, 2017);
    president [44] = new Presidents(45, "Donald Trump", 2017, 2018);

    System.out.println(presidentList [0]);
    kb.close();

}

}