I have a error in my program that says this Exception in thread \"main\" java.la
ID: 3561865 • Letter: I
Question
I have a error in my program that says this
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method load(Party[], String) in the type PartyDriver is not applicable for the arguments (String[], String)
at PartyDriver.main(PartyDriver.java:19) and this is the driver program
// java.io classes, used for file i/o
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
// load party data from file and
public class PartyDriver
{
public static void main(String[] args) throws IOException
{
String[] parties = new String[10];
int partyCount = 0; // actual number of parties
partyCount = (new PartyDriver()).load(parties, "data/input.txt");
// 2. process parties
// 2.0 test print
System.out.println(partyCount + " parties loaded successfully");
for (int i = 0; i<partyCount; i++)
{
System.out.printf("Party[%d]: hosted by %s ", i, parties[i]);
}
// 2.1 more processing
}
/**
* Fills party array with data from text file.
*
*
* @param parties array of Party to hold parties
* @param fileName name of input text file
* @return number of parties loaded
* @throws IOException any exception during file opening, reading, and closing
*/
@SuppressWarnings("unused")
public int load(Party[] parties, String fileName)
throws IOException
{
Scanner fileIn = null; // scanner object to connect to file
int partyCount = 0; // track # of parties
try {
// open input file
fileIn = new Scanner(new BufferedReader(new FileReader(fileName)));
// set up Scanner object fileIn to read one
// line at a time, and ignore extra whitespace
// characters (space, tab, newline character)
fileIn.useDelimiter("\s* \s*");
// loop through multiple parties
while (fileIn.hasNext())
{
// start one party
// 1. host name
String hostName = fileIn.next();
parties[partyCount] = new Party(partyCount, hostName);
// 2. party capacity
int partyMaxSize = fileIn.nextInt();
// 3. actual # of guests
int guestNum = fileIn.nextInt();
// Test print part 1: display party level data
//System.out.printf("Host: %s, Max: %d, Actual: %d ",
//hostName, partyMaxSize, guestNum);
// 4. that many # of guests
for (int i=0; i<guestNum; i++)
{
String guestName = fileIn.next();
// Test print part 2: print each guest name
//System.out.println(guestName);
}
// 5. end reading one party.
// increment party counter
partyCount++;
// end one party
}// end while: reading all parties
}
finally // close file
{
if ( fileIn != null)
{ // close if was connected to a file
fileIn.close();
}
}
// end file input
return partyCount;
}
}// end load()
this is the party class
public class Party
{
private final int size; // constant to specify maximum number of guest allowed
private String hostName; // name of party host
private String[] guestList; // List of party guest names
private int guestCount; // number of guest recorded
public Party (int size, String hostName)
{
this.size = size;
this.hostName = hostName;
guestList = new String[size];
guestCount = 0;
}// end constructor
public String gethostName()
{
return hostName;
} // end get hostName
public int getsize()
{
return size;
} // end get size
public int getguestCount()
{
return guestCount;
} // end getguestCount
public void addGuest (String guestName)
{
if(guestCount >= size) //full
{
System.out.println(guestName + "cannot come to the party. The guest list is full");
}
else if (isOnList(changeNameOrder(guestName))) //duplicate
{
System.out.println( guestName + " is already on the guest list.");
}
else // add to list
{
int i =guestCount-1;
while (i>=0 && guestList[i].compareToIgnoreCase(guestName) > 0)
{
guestList[i +1] = guestList[i];
i--;
}
guestList[i +1] = guestName;
guestCount++;
}
} // end addGuest
//*************************************************
//This method checks whether an given guest is on the list
public void printParty()
{
System.out.println("Guest list for " + hostName + "'s party:" );
for (int i=0; i<guestCount; i++)
{
System.out.println(guestList[i]);
}
} // end printGuestList
// *************************************************
private String changeNameOrder(String nameFirstLast)
{
String firstname, lastname;
firstname = nameFirstLast.substring(0, nameFirstLast.lastIndexOf(' '));
lastname = nameFirstLast.substring(nameFirstLast.lastIndexOf(' ')+ 1,nameFirstLast.length());
return (lastname + ", " + firstname);
} // end changeNameOrder
//*************************************************
private boolean isOnList(String guestName)
{
if (guestCount==0) // empty list
return false;
for (int i=0; i<guestCount; i++)
{
if (guestName.equalsIgnoreCase(guestList[i]))
{
return true; // yes already on list
}
} // end of for loop
return false; // not found
} // end of isOnList
}// end class
what is the problem?
Explanation / Answer
//Corrected and pasted executable program
You have done two mistakes
1) parties variable shoulbe of class Party
//String[] parties = new String[10];
Party[] parties = new Party[10];
2) Comment missing the following line, i added it
//this is the party class
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
// load party data from file and
public class PartyDriver
{
public static void main(String[] args) throws IOException
{
//String[] parties = new String[10];
Party[] parties = new Party[10];
int partyCount = 0; // actual number of parties
partyCount = (new PartyDriver()).load(parties, "data/input.txt");
// 2. process parties
// 2.0 test print
System.out.println(partyCount + " parties loaded successfully");
for (int i = 0; i<partyCount; i++)
{
System.out.printf("Party[%d]: hosted by %s ", i, parties[i]);
}
// 2.1 more processing
}
/**
* Fills party array with data from text file.
*
*
* @param parties array of Party to hold parties
* @param fileName name of input text file
* @return number of parties loaded
* @throws IOException any exception during file opening, reading, and closing
*/
@SuppressWarnings("unused")
public int load(Party[] parties, String fileName)
throws IOException
{
Scanner fileIn = null; // scanner object to connect to file
int partyCount = 0; // track # of parties
try {
// open input file
fileIn = new Scanner(new BufferedReader(new FileReader(fileName)));
// set up Scanner object fileIn to read one
// line at a time, and ignore extra whitespace
// characters (space, tab, newline character)
fileIn.useDelimiter("\s* \s*");
// loop through multiple parties
while (fileIn.hasNext())
{
// start one party
// 1. host name
String hostName = fileIn.next();
parties[partyCount] = new Party(partyCount, hostName);
// 2. party capacity
int partyMaxSize = fileIn.nextInt();
// 3. actual # of guests
int guestNum = fileIn.nextInt();
// Test print part 1: display party level data
//System.out.printf("Host: %s, Max: %d, Actual: %d ",
//hostName, partyMaxSize, guestNum);
// 4. that many # of guests
for (int i=0; i<guestNum; i++)
{
String guestName = fileIn.next();
// Test print part 2: print each guest name
//System.out.println(guestName);
}
// 5. end reading one party.
// increment party counter
partyCount++;
// end one party
}// end while: reading all parties
}
finally // close file
{
if ( fileIn != null)
{ // close if was connected to a file
fileIn.close();
}
}
// end file input
return partyCount;
}
}// end load()
//this is the party class
class Party
{
private final int size; // constant to specify maximum number of guest allowed
private String hostName; // name of party host
private String[] guestList; // List of party guest names
private int guestCount; // number of guest recorded
public Party (int size, String hostName)
{
this.size = size;
this.hostName = hostName;
guestList = new String[size];
guestCount = 0;
}// end constructor
public String gethostName()
{
return hostName;
} // end get hostName
public int getsize()
{
return size;
} // end get size
public int getguestCount()
{
return guestCount;
} // end getguestCount
public void addGuest (String guestName)
{
if(guestCount >= size) //full
{
System.out.println(guestName + "cannot come to the party. The guest list is full");
}
else if (isOnList(changeNameOrder(guestName))) //duplicate
{
System.out.println( guestName + " is already on the guest list.");
}
else // add to list
{
int i =guestCount-1;
while (i>=0 && guestList[i].compareToIgnoreCase(guestName) > 0)
{
guestList[i +1] = guestList[i];
i--;
}
guestList[i +1] = guestName;
guestCount++;
}
} // end addGuest
//*************************************************
//This method checks whether an given guest is on the list
public void printParty()
{
System.out.println("Guest list for " + hostName + "'s party:" );
for (int i=0; i<guestCount; i++)
{
System.out.println(guestList[i]);
}
} // end printGuestList
// *************************************************
private String changeNameOrder(String nameFirstLast)
{
String firstname, lastname;
firstname = nameFirstLast.substring(0, nameFirstLast.lastIndexOf(' '));
lastname = nameFirstLast.substring(nameFirstLast.lastIndexOf(' ')+ 1,nameFirstLast.length());
return (lastname + ", " + firstname);
} // end changeNameOrder
//*************************************************
private boolean isOnList(String guestName)
{
if (guestCount==0) // empty list
return false;
for (int i=0; i<guestCount; i++)
{
if (guestName.equalsIgnoreCase(guestList[i]))
{
return true; // yes already on list
}
} // end of for loop
return false; // not found
} // end of isOnList
}// end class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.