Results 1 to 4 of 4

Thread: [RESOLVED] need help understanding error

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Posts
    166

    Resolved [RESOLVED] need help understanding error

    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




    Code:
    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();
    		
    		}
       }
    }

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: need help understanding error

    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.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Posts
    166

    Re: need help understanding error

    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:

    Code:
    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?

  4. #4
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: need help understanding error

    You'll want to use DecimalFormat.

    Example:

    Code:
    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;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width