0

Hello I am very new to Java and I really don't know exactly what I am doing. I know a little bit of other programming languages so I'm trying my best to figure it out for myself but I am supposed to:
Write a Java Application which will read a text file of scores from college football games and calculate the average home field advantage for that data.

import java.io.*;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class HomeField
{
	public static void main(String [] args) 
	{
		String fileName;
		File inFile;
		Scanner kbd, input = null;
		String line;
                int LineCount = 0;
                int total = 0;

                //Create scanner object for keyboard input
		kbd = new Scanner(System.in);
                //Get the filename.
		System.out.print("Enter a filename: ");
		fileName = kbd.nextLine();
                //Open the file.
		inFile = new File(fileName);
        try {
            input = new Scanner(inFile);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(HomeField.class.getName()).log(Level.SEVERE, null, ex);
        }
        

		while(input.hasNextLine())
		{
                // read a line from text
                line=input.nextLine();
                // increment line counter
                LineCount++;
                // pull off the visting team score from col 39-42  (use substring(), trim(), parseInt()
                String vscore = line.substring(38,4);
                vscore=vscore.trim();
                int vnum = Integer.parseInt(vscore);
                // pull off the home team score from col 71-74
                String hscore=line.substring(70,4);
                hscore=hscore.trim();
                int hnum=Integer.parseInt(hscore);

                //  add the difference (home - vistor) to
                total=hnum-vnum;
                total +=total;
		}
                // compute average
                double average=total/LineCount;

                // print out the average with an appropriate label
                System.out.println("The average is: " + average);
	}
}

The error that I'm getting is
java.lang.NoClassDefFoundError: HomeField/HomeField
Caused by: java.lang.ClassNotFoundException: HomeField.HomeField
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: HomeField.HomeField. Program will exit.
Exception in thread "main" Java Result: 1

Please help if you can.

4
Contributors
10
Replies
11
Views
7 Years
Discussion Span
Last Post by NormR1
Featured Replies
  • 1

    This error message says there was an error on line 33 of your program. The index had a value of -34. Look at line 33 of your code and figure out how there could be an index of -34 This line assumes that the line has > 38 characters. You … Read More

0

when i was tried this program i found nothing wrong..

how do you give file name as input..

if u give just file name like "test.txt"..it thorws error..

but if u give exact path of the file like "c:\\temp\\test.txt"..it is fine..

check it once again..and post the exact error and how did you given input...

Edited by musthafa.aj: n/a

0

How are you executing the program? Are you using the java command?
The error message says you entered: java HomeField.HomeField as the commandline.
Try java HomeField

0

I tried both using Netbeans IDE and just the command prompt. I got the error the first time using Netbeans.

When I used the command prompt I did:
javac HomeField.java
java HomeField

I've tried typing the file with paths and without but it still comes up with this error on command prompt and Netbeans:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -34
at java.lang.String.substring<Unknown Source>
at HomeField.main<HomeField.java:38

This last one changes from 38 to 33, to 41 on various runs

I changed the code a little bit

import java.io.*;
import java.util.Scanner;

public class HomeField
{
	public static void main(String [] args) throws FileNotFoundException
	{
		String fileName;
		File inFile;
		Scanner kbd, input = null;
		String line;
                int LineCount = 0;
                int total = 0;

                //Create scanner object for keyboard input
		kbd = new Scanner(System.in);
                //Get the filename.
		System.out.print("Enter a filename: ");
		fileName = kbd.nextLine();
                //Open the file.
		inFile = new File(fileName);
                input = new Scanner(inFile);



		while(input.hasNextLine())
		{
                // read a line from text
                line=input.nextLine();
                // increment line counter
                LineCount++;
                // pull off the visting team score from col 39-42  (use substring(), trim(), parseInt()
                String vscore = line.substring(38,4);
                vscore=vscore.trim();
                int vnum = Integer.parseInt(vscore);
                // pull off the home team score from col 71-74
                String hscore=line.substring(70,4);
                hscore=hscore.trim();
                int hnum=Integer.parseInt(hscore);

                //  add the difference (home - vistor) to
                total=hnum-vnum;
                total +=total;
		}
                // compute average
                double average=total/LineCount;

                // print out the average with an appropriate label
                System.out.println("The average is: " + average);
	}
}
0

Check the text file you providing as input.
The error shows that it does not contain anything in the index specified
in the subString() method...

0

Are you still getting this error?

The error that I'm getting is
java.lang.NoClassDefFoundError: HomeField/HomeField

Can you run it again in the command prompt and copy the full contents of the command prompt window here?

0

I attached the text file I am using for the data.

This is exactly the error the command prompt gave

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -34
at java.lang.String.substring<Unknown Source>
at HomeField.main<HomeField.java:33>

Attachments
08/28/2008 Charleston Southern          7  Miami (Florida)             52 
08/28/2008 Eastern Illinois            12  Central Michigan            31 
08/28/2008 Eastern Kentucky             7  Cincinnati                  40 
08/28/2008 Hofstra                      3  Connecticut                 35 
08/28/2008 Indiana State                0  Eastern Michigan            52 
08/28/2008 Jacksonville State          14  Georgia Tech                41 
08/28/2008 North Carolina State         0  South Carolina              34 
08/28/2008 Northeastern                14  Ball State                  48 
08/28/2008 Oregon State                28  Stanford                    36 
08/28/2008 South Dakota State          17  Iowa State                  44 
08/28/2008 Texas-El Paso               17  Buffalo                     42 
08/28/2008 Troy                        31  Middle Tennessee State      17 
08/28/2008 Vanderbilt                  34  Miami (Ohio)                13 
08/28/2008 Wake Forest                 41  Baylor                      13 
08/29/2008 Southern Methodist          27  Rice                        56 
08/29/2008 Temple                      35  Army                         7 
08/30/2008 Akron                       17  Wisconsin                   38 
08/30/2008 Alabama                     34  Clemson                     10 @ Atlanta, GA
08/30/2008 Appalachian State           13  Louisiana State             41 
08/30/2008 Arkansas State              18  Texas A&M                   14 
08/30/2008 Boston College              21  Kent                         0 @ Cleveland, OH
08/30/2008 Bowling Green State         27  Pittsburgh                  17 
08/30/2008 Cal Poly-San Luis Obispo    29  San Diego State             27 
08/30/2008 California-Davis            10  San Jose State              13 
08/30/2008 Coastal Carolina            10  Penn State                  66 
08/30/2008 Delaware                     7  Maryland                    14 
08/30/2008 East Carolina               27  Virginia Tech               22 @ Charlotte, NC
08/30/2008 Eastern Washington          24  Texas Tech                  49 
08/30/2008 Florida Atlantic            10  Texas                       52 
08/30/2008 Florida International       10  Kansas                      40 
08/30/2008 Georgia Southern            21  Georgia                     45 
08/30/2008 Grambling State             13  Nevada                      49 
08/30/2008 Hawaii                      10  Florida                     56 
08/30/2008 Idaho                        0  Arizona                     70 
08/30/2008 Idaho State                  7  Boise State                 49 
08/30/2008 Illinois                    42  Missouri                    52 @ St. Louis, MO
08/30/2008 Illinois State              10  Marshall                    35 
08/30/2008 James Madison                7  Duke                        31 
08/30/2008 Louisiana-Lafayette         21  Southern Mississippi        51 
08/30/2008 Louisiana-Monroe             0  Auburn                      34 
08/30/2008 Maine                        3  Iowa                        46 
08/30/2008 McNeese State               27  North Carolina              35 
08/30/2008 Memphis                     24  Mississippi                 41 
08/30/2008 Michigan State              31  California                  38 
08/30/2008 Mississippi State           14  Louisiana Tech              22 
08/30/2008 North Texas                  6  Kansas State                45 
08/30/2008 Northern Arizona            13  Arizona State               30 
08/30/2008 Northern Illinois           27  Minnesota                   31 
08/30/2008 Northern Iowa               17  Brigham Young               41 
08/30/2008 Ohio                        20  Wyoming                     21 
08/30/2008 Oklahoma State              39  Washington State            13 @ Seattle, WA
08/30/2008 South Carolina State         0  Central Florida             17 
08/30/2008 Southern                     3  Houston                     55 
08/30/2008 Southern California         52  Virginia                     7 
08/30/2008 Southern Utah                7  Air Force                   41 
08/30/2008 Syracuse                    10  Northwestern                30 
08/30/2008 Tennessee-Chattanooga        2  Oklahoma                    57 
08/30/2008 Tennessee-Martin             7  South Florida               56 
08/30/2008 Texas Christian             26  New Mexico                   3 
08/30/2008 Towson                      13  Navy                        41 
08/30/2008 Tulsa                       45  Alabama-Birmingham          22 
08/30/2008 Utah                        25  Michigan                    23 
08/30/2008 Utah State                  17  Nevada-Las Vegas            27 
08/30/2008 Villanova                   21  West Virginia               48 
08/30/2008 Washington                  10  Oregon                      44 
08/30/2008 Western Illinois            24  Arkansas                    28 
08/30/2008 Western Kentucky            13  Indiana                     31 
08/30/2008 Western Michigan            24  Nebraska                    47 
08/30/2008 Youngstown State             0  Ohio State                  43 
08/31/2008 Colorado                    38  Colorado State              17 @ Denver, CO
08/31/2008 Kentucky                    27  Louisville                   2 
09/01/2008 Fresno State                24  Rutgers                      7 
09/01/2008 Tennessee                   24  UCLA                        27 
09/04/2008 South Carolina              17  Vanderbilt                  24 
09/05/2008 Navy                        23  Ball State                  35 
09/06/2008 Air Force                   23  Wyoming                      3 
09/06/2008 Akron                       42  Syracuse                    28 
09/06/2008 Alabama-Birmingham          34  Florida Atlantic            49 
09/06/2008 Brigham Young               28  Washington                  27 
09/06/2008 Buffalo                     16  Pittsburgh                  27 
09/06/2008 California                  66  Washington State             3 
09/06/2008 Central Michigan            17  Georgia                     56 
09/06/2008 Cincinnati                  26  Oklahoma                    52 
09/06/2008 Citadel                     17  Clemson                     45 
09/06/2008 Connecticut                 12  Temple                       9 
09/06/2008 Eastern Illinois            21  Illinois                    47 
09/06/2008 Eastern Michigan            10  Michigan State              42 
09/06/2008 Eastern Washington          24  Colorado                    31 
09/06/2008 Florida International        0  Iowa                        42 
09/06/2008 Furman                       7  Virginia Tech               24 
09/06/2008 Georgia Tech                19  Boston College              16 
09/06/2008 Houston                     37  Oklahoma State              56 
09/06/2008 Idaho State                 27  Idaho                       42 
09/06/2008 Kent                        28  Iowa State                  48 
09/06/2008 Louisiana Tech               0  Kansas                      29 
09/06/2008 Louisiana-Monroe            27  Arkansas                    28 @ Little Rock, AR
09/06/2008 Marshall                    14  Wisconsin                   51 
09/06/2008 Maryland                    14  Middle Tennessee State      24 
09/06/2008 Miami (Florida)              3  Florida                     26 
09/06/2008 Miami (Ohio)                 6  Michigan                    16 
09/06/2008 Minnesota                   42  Bowling Green State         17 
09/06/2008 Mississippi                 28  Wake Forest                 30 
09/06/2008 Montana State               10  Kansas State                69 
09/06/2008 Murray State                 3  Indiana                     45 
09/06/2008 Nevada-Las Vegas            21  Utah                        42 
09/06/2008 New Hampshire               28  Army                        10 
09/06/2008 Norfolk State                3  Kentucky                    38 
09/06/2008 Northern Colorado           10  Purdue                      42 
09/06/2008 Northern Illinois           26  Western Michigan            29 
09/06/2008 Northwestern                24  Duke                        20 
09/06/2008 Northwestern State           6  Baylor                      51 
09/06/2008 Ohio                        14  Ohio State                  26 
09/06/2008 Oregon State                14  Penn State                  45 
09/06/2008 Rice                        42  Memphis                     35 
09/06/2008 Richmond                     0  Virginia                    16 
09/06/2008 Sacramento State            20  Colorado State              23 
09/06/2008 San Diego State             13  Notre Dame                  21 
09/06/2008 San Jose State              12  Nebraska                    35 
09/06/2008 South Florida               31  Central Florida             24 
09/06/2008 Southeast Missouri State     3  Missouri                    52 
09/06/2008 Southeastern Louisiana      10  Mississippi State           34 
09/06/2008 Southern Mississippi        13  Auburn                      27 
09/06/2008 Stanford                    17  Arizona State               41 
09/06/2008 Stephen F. Austin            7  Texas Christian             67 
09/06/2008 Tennessee Tech              10  Louisville                  51 
09/06/2008 Texas                       42  Texas-El Paso               13 
09/06/2008 Texas A&M                   28  New Mexico                  22 
09/06/2008 Texas Southern              10  Arkansas State              83 
09/06/2008 Texas State                 36  Southern Methodist          47 
09/06/2008 Texas Tech                  35  Nevada                      19 
09/06/2008 Toledo                      16  Arizona                     41 
09/06/2008 Tulane                       6  Alabama                     20 
09/06/2008 Tulsa                       56  North Texas                 26 
09/06/2008 Utah State
1

This error message says there was an error on line 33 of your program.
The index had a value of -34.

Look at line 33 of your code and figure out how there could be an index of -34

This line assumes that the line has > 38 characters. You need to test that the line is long enough before trying to use the 38 index.
String vscore = line.substring(38,4);

Edited by NormR1: wrong line number

Votes + Comments
Great advice
0

Well the line goes all the way past 74. I know this is a basic question but when you say index I really don't understand how it could be -34 anyway. The exact file I'm using is included in my previous post

0

Ok nevermind I got it past that point by changing the line numbers on line 33 and 37.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.