Hello I am currently running into an error in one of my java programs and cannot
ID: 3801197 • Letter: H
Question
Hello I am currently running into an error in one of my java programs and cannot seem to determine why. Hopefully you can help me with this quesiton
Below is the program.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Education {
int schoodId;
String schoolName;
String stateAbbreviation;
String webAddress;
static Education [] edu = new Education[10];
int size;
int capacity = 10;
public Education(){
}
public Education(int schoodId,String schoolName, String stateAbbreviation,String webAddress){
this.schoodId = schoodId;
this.schoolName = schoolName;
this.stateAbbreviation = stateAbbreviation;
this.webAddress = webAddress;
}
public void add(Education x) {
if (size >= capacity){
Education[] temp = new Education[2*edu.length];
for(int i=0;i<size;++i){
temp[i] = edu[i];
}
this.capacity = 2*edu.length;
edu = temp;
edu[size++] = x;
return;
}
else
{
edu[size++] = x;
}
}
public String toString() {
String res = "";
for (int i = 0; i < size; ++i) {
Education ed = edu[i];
res = res + " " + ed.schoodId + " " + ed.schoolName + " " + ed.stateAbbreviation + " " + ed.webAddress + " ";
}
return res;
}
public static void main(String args[]) throws FileNotFoundException{
Scanner sc = new Scanner(new File("Post Secondary Education.txt"));
Education ed = new Education();
while (sc.hasNextLine()){
String readLine = sc.nextLine();
int index = readLine.lastIndexOf("www");
String string = "";
if(index!=-1)
string = readLine.substring(index, readLine.length());
int indexforAl = readLine.lastIndexOf("AL");
String val = "";
if(indexforAl!=-1)
val = readLine.substring(indexforAl, readLine.length());
//System.out.println(string);
String tokens[] = readLine.split(" +");
int schoodId = Integer.parseInt(tokens[0]);
String schoolName = "";
for(int i=1;i<tokens.length;++i){
if(tokens[i].equals("AL"))
break;
schoolName = schoolName + tokens[i];
}
String stateAbbreviation = "AL";
String webAddress = string;
Education education = new Education(schoodId,schoolName,stateAbbreviation,webAddress);
ed.add(education);
}
System.out.println(ed.toString());
}
}
Here is the error I am currently encountering, and cannot seem to fix.
Exception in thread "main" java.lang.NumberFormatException: For input string: "UNITID INSTNM STABBR WEBADDR"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Education.main(Education.java:86)
Here is the question this program is suppose to answer.
In this program, you are going to read in a post secondary education data set file, Post Secondary Education.txt. You are going to create a Education object which keeps track of the SchoolID, Name of the School, the State Abbreviation their Web address.
You are going to store it into an array of Education objects. The default starting size is 10. You are going mimic an ArrayList, and when you add items you will use an add method that you create. If you run out of space, you need to expand the array (double its size) and copy all the previous items into the new array when you run out of space.
Finally, you should print out each of the objects by using a get method that you create.
Use the Post Secondary Education.txt file in Moodle.
Specifics:
1. Read in the education file and create Education objects. 2. Store each object into the array you created. 3. Initialize the array size to 10. Please do not use an actual ArrayList. 4. Create an expand method when the number of items read in exceeds the size of the array. 5. Create a size method to keep track of how large your “ArrayList.” 6. Create an add method to add items to your “ArrayList.” 7. Create a get method to get items out of your “ArrayList.” 8. Print out all the students in your “ArrayList” at the end.
Post Secondary Education.txt file
UNITID INSTNM STABBR WEBADDR
100654 Alabama A & M University AL www.aamu.edu/
100663 University of Alabama at Birmingham AL www.uab.edu
100690 Amridge University AL www.amridgeuniversity.edu
100706 University of Alabama in Huntsville AL www.uah.edu
100724 Alabama State University AL www.alasu.edu/email/index.aspx
100733 University of Alabama System Office AL www.uasystem.ua.edu
100751 The University of Alabama AL www.ua.edu/
100760 Central Alabama Community College AL www.cacc.edu
100812 Athens State University AL www.athens.edu
100830 Auburn University at Montgomery AL www.aum.edu
100858 Auburn University AL www.auburn.edu
100937 Birmingham Southern College AL www.bsc.edu/
101028 Chattahoochee Valley Community College AL www.cv.edu
101073 Concordia College Alabama AL www.ccal.edu/
101116 South University-Montgomery AL southuniversity.edu
101143 Enterprise State Community College AL www.escc.edu
101161 James H Faulkner State Community College AL www.faulknerstate.edu
101189 Faulkner University AL www.faulkner.edu
101240 Gadsden State Community College AL www.gadsdenstate.edu
101277 New Beginning College of Cosmetology AL www.nbccosmetology.com
101286 George C Wallace State Community College-Dothan AL www.wallace.edu
101295 George C Wallace State Community College-Hanceville AL www.wallacestate.edu
101301 George C Wallace State Community College-Selma AL www.wccs.edu
101365 Herzing University-Birmingham AL www.herzing.edu/birmingham
101435 Huntingdon College AL www.huntingdon.edu
101453 Heritage Christian University AL www.hcu.edu
101462 J F Drake State Community and Technical College AL www.drakestate.edu
101471 J F Ingram State Technical College AL www.istc.edu
101480 Jacksonville State University AL www.jsu.edu/
101499 Jefferson Davis Community College AL www.jdcc.edu
101505 Jefferson State Community College AL www.jeffstateonline.com
101514 John C Calhoun State Community College AL www.calhoun.edu
101541 Judson College AL www.judson.edu
101569 Lawson State Community College-Birmingham Campus AL www.lawsonstate.edu
101587 University of West Alabama AL www.uwa.edu
101602 Lurleen B Wallace Community College AL www.lbwcc.edu
Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Education {
int schoodId;
String schoolName;
String stateAbbreviation;
String webAddress;
static Education [] edu = new Education[10];
int size;
int capacity = 10;
public Education(){
}
public Education(int schoodId,String schoolName, String stateAbbreviation,String webAddress){
this.schoodId = schoodId;
this.schoolName = schoolName;
this.stateAbbreviation = stateAbbreviation;
this.webAddress = webAddress;
}
public void add(Education x) {
if (size >= capacity){
Education[] temp = new Education[2*edu.length];
for(int i=0;i<size;++i){
temp[i] = edu[i];
}
this.capacity = 2*edu.length;
edu = temp;
edu[size++] = x;
return;
}
else
{
edu[size++] = x;
}
}
public String toString() {
String res = "";
for (int i = 0; i < size; ++i) {
Education ed = edu[i];
res = res + " " + ed.schoodId + " " + ed.schoolName + " " + ed.stateAbbreviation + " " + ed.webAddress + " ";
}
return res;
}
public static void main(String args[]) throws FileNotFoundException{
Scanner sc = new Scanner(new File("a.txt"));
Education ed = new Education();
sc.nextLine();
while (sc.hasNextLine()){
String readLine = sc.nextLine();
int index = readLine.lastIndexOf("www");
String string = "";
if(index!=-1)
string = readLine.substring(index, readLine.length());
int indexforAl = readLine.lastIndexOf("AL");
String val = "";
if(indexforAl!=-1)
val = readLine.substring(indexforAl, readLine.length());
//System.out.println(string);
String tokens[] = readLine.split(" ");
int schoodId = Integer.parseInt(tokens[0]);
String schoolName = "";
for(int i=1;i<tokens.length;++i){
if(tokens[i].equals("AL"))
break;
schoolName = schoolName + tokens[i];
}
String stateAbbreviation = "AL";
String webAddress = string;
Education education = new Education(schoodId,schoolName,stateAbbreviation,webAddress);
ed.add(education);
}
System.out.println(ed.toString());
}
}
Here is the working program.. The issue is with string tokenizer that is being used.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.