PDA

Click to See Complete Forum and Search --> : [RESOLVED] need help understanding error


Blue1974
Mar 27th, 2010, 11:03 AM
I'm working on creating a program called golf that reads data from a text file. I must be creating it in the wrong way but I don't know how to proceed at this point. I could use some help understanding whats going?

The compiling error says:
Golf.java:9: class golf is public, should be declared in a file named golf.java
public class golf
^
1 error




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

public class golf
{

public static void main (String[] args) throws IOException
{
int p1score=0, p2score=0, p3score=0, p4score=0, par=0;
int hole=0;
String strokes;

Scanner fileInput, strokesScan;

fileInput = new Scanner(new File("scores.txt"));

// Read and process each line of the file
while ( fileInput.hasNext() )
{
strokes = fileInput.nextLine();
System.out.print ("Strokes: " + strokes);

strokesScan = new Scanner (strokes);
strokesScan.useDelimiter("");


while (strokesScan.hasNext())
System.out.print (" " + fileInput.next());

System.out.println();

}
}
}

kfcSmitty
Mar 27th, 2010, 12:19 PM
If you have your class golf, it should be in a file called golf.java.

There are ways around that, but it makes things harder to read and not as maintainable.

So you either need to change your file to golf.java or your class to whatever the name of your file is.

Blue1974
Mar 27th, 2010, 01:40 PM
Ok, got the error corrected. Appreciate the tip. Another question though, probably another easy one, but when I'm displaying my table the columns for my data shift over a space when the counter hits double digits. Is there an easy fix for this or does it involves some kind of if statement?

This is what my code looks like now:

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

public class golf
{

public static void main (String[] args) throws IOException
{
int par=0, p1score=0, p2score=0, p3score=0, p4score=0;
int hole=1;


Scanner fileInput = new Scanner(new File("scores.txt"));

System.out.println("Hole# Par Player1 Player2 Player3 Player4");

// Check to if there is data in textfile
while ( fileInput.hasNext() )
{
//Input data from text file
par = fileInput.nextInt();
p1score = fileInput.nextInt();
p2score = fileInput.nextInt();
p3score = fileInput.nextInt();
p4score = fileInput.nextInt();

//Process data
System.out.println(" " + hole + " " + par + " " + p1score + " " + p2score + " " + p3score + " " + p4score);
hole += 1;
}

}
}


It's not aligning correctly in this post but you can see what happens when you get 10. Everything shifts over a space.

Hole# Par Player1 Player2 Player3 Player4
1 4 5 7 5 6
2 4 5 4 6 8
3 4 6 5 4 7
4 3 4 4 6 4
5 4 5 6 4 5
6 3 4 8 4 7
7 4 3 5 5 5
8 5 6 5 5 7
9 3 4 3 4 5
10 4 5 5 6 6
12 4 7 5 5 7
13 3 4 4 5 6
14 5 6 5 6 6
15 4 5 6 5 6
16 4 5 4 4 5
17 3 4 4 5 6
18 4 3 4 5 5

Any ideas on this?

kfcSmitty
Mar 27th, 2010, 05:33 PM
You'll want to use DecimalFormat.

Example:


int test = 5;

DecimalFormat df = new DecimalFormat("00");

System.out.println(df.format(test));


Will force the output to "02" instead of just 2. You'll need to import java.text.DecimalFormat;