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.

Recommended Answers

All 10 Replies

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...

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

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);
	}
}

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...

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?

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>

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);

commented: Great advice +1

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

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

Ok, problem solved?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.