Page 2 of 2 FirstFirst 12
Results 21 to 27 of 27

Thread: Homework help

  1. #21
    rclausing is offline Member
    Join Date
    Oct 2009
    Location
    Oregon
    Posts
    22
    Rep Power
    0

    Default

    Alright, the run time error is fixed - as you pointed out I had placed a while (hasNext) instead of while (hasNextInt). However, the values from my .txt file aren't being stored in the arrays as I had thought they would. No values are stored in fact.

    Now, I need to have while(there is data to scan in the .txt file)
    scan next line of 5 items
    store each value in the line into an array[5]
    add the new value scanned to each variable
    print the totals.
    which is what I thought I did. As I look at it, I am missing where it scans the next line of 5 items. so I came up with:

    Java Code:
    import java.util.Scanner;
    import java.io.*;
    
    public class golfscores1
    
    {
    	public static void main (String[] args) throws IOException
    	{
    		int g1score=0, g2score=0, g3score=0, g4score=0, par=0;
    		String strokes;
    
    		Scanner fileScan, scoresScan;
    
    		fileScan = new Scanner (new File("scores.txt"));
    
    
    			while (fileScan.hasNext())
    			{
    				strokes = fileScan.nextLine();
    				scoresScan = new Scanner (strokes);
    				while (scoresScan.hasNextInt())
    				{
    
    
    					int[] scores = new int[5];
    					for (int i=0; i<scores.length; i++)
    					{
    
    						scores[i]=fileScan.nextInt();
    
    					}
    					scores[0] += par;
    					scores[1] += g1score;
    					scores[2] += g2score;
    					scores[3] += g3score;
    					scores[4] += g4score;
    				}
    
    			}
    
    
    
    
    
    
    					System.out.println("Par is: " + par);
    					System.out.println("Golfer 1 scored: " + g1score);
    					System.out.println("Golfer 2 scored: " + g2score);
    					System.out.println("Golfer 3 scored: " + g3score);
    					System.out.println("Golfer 4 scored: " + g4score);
    
    
    
    			System.out.println();
    			int small = g4score;
    			if (small > g1score)
    				small = g1score;
    			if (small > g2score)
    				small = g2score;
    			if (small > g3score)
    				small = g3score;
    
    			if (small == g1score)
    				System.out.println("Golfer 1 is the winner!");
    				if (small == g2score)
    					System.out.println("Golfer 2 is the winner!");
    					if (small == g3score)
    						System.out.println("Golfer 3 is the winner!");
    							if (small == g4score)
    								System.out.println("Golfer 4 is the winner!");
    		}
    
    
    
    
    
    }
    however this does not pass the data from the .txt file to the arrays either. I need some tylenol...

  2. #22
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    If you have to have them stored use 6 arrays of size 18, one for par, and one for each golfer. Or, it you know how, use a 2d array

  3. #23
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    Also, you are makeing a new array for each line: int[] scores = new int[5]; this should be outside the while loop. Also, why do you need arrays? Just say:
    while (scoresScan.hasNextInt())
    {
    par += scoresScan.nextInt();
    g1score += scoresScan.nextInt();
    g2score += scoresScan.nextInt();
    g3score += scoresScan.nextInt();
    g4score += scoresScan.nextInt();
    g5score += scoresScan.nextInt();
    }

  4. #24
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by collin389 View Post
    Also, you are makeing a new array for each line: int[] scores = new int[5]; this should be outside the while loop. Also, why do you need arrays? Just say:
    while (scoresScan.hasNextInt())
    {
    par += scoresScan.nextInt();
    g1score += scoresScan.nextInt();
    g2score += scoresScan.nextInt();
    g3score += scoresScan.nextInt();
    g4score += scoresScan.nextInt();
    g5score += scoresScan.nextInt();
    }
    you're suggesting that the OP do the same bad thing that I advised against above: checking for hasNextInt once and requesting nextInt 6 times. With Scanner, you should avoid request a token without checking it exists first.

  5. #25
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    Yeah, but he knows that there will be 5 ints comming up. the hasNextInt() is just checking if there is another line. he could've just made a counter and ran it 18 times but hasNextInt is faster. He will learn later, but I think his HW is due tonight and he's tired of working on it.

  6. #26
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Please see comments in code. In particular, if you are going to use a second Scanner to read each line, then actually USE the scanner. Don't just create it and ignore it. Also you'll need to set its delimiter so that it will use "/" to separate tokens.

    Java Code:
    public class GolfScore {
       //!! FILE_PATH will likely change for your program
       //!! and in fact, you may not even need this string
       private static final String FILE_PATH = "src/yr2009/m11/c/";
       private static final String FILE_NAME = "scores.txt";
       
       public static void main(String[] args) throws IOException {
          int g1score = 0, g2score = 0, g3score = 0, g4score = 0, par = 0;
          String strokes;
    
          Scanner fileScan, scoresScan;
    
          //!! Again, you may not need the FILE_PATH string here
          fileScan = new Scanner(new File(FILE_PATH + FILE_NAME));
          
          //!! while (fileScan.hasNext()) { 
          while (fileScan.hasNextLine()) { //!! again, must check for nextLine here
             strokes = fileScan.nextLine();
             scoresScan = new Scanner(strokes);
             
             //!! **********
             scoresScan.useDelimiter("/"); //!! if you are using a Scanner you must change the delimiter
             
             while (scoresScan.hasNextInt()) {
    
                //!! this is all farked up.
                int[] scores = new int[5];
                for (int i = 0; i < scores.length; i++) {
    
                   //!! scores[i] = fileScan.nextInt();  // you're using the wrong scanner here
                }

    Here I'm reading with the second delimiter.
    Java Code:
    import java.util.Scanner;
    import java.io.*;
    
    public class GolfScore {
       //!! FILE_PATH will likely change for your program
       //!! and in fact, you may not even need this string
       private static final String FILE_PATH = "src/yr2009/m11/c/";
       private static final String FILE_NAME = "scores.txt";
       
       public static void main(String[] args) throws IOException {
          String strokes;
    
          Scanner fileScan, scoresScan;
    
          //!! Again, you may not need the FILE_PATH string here
          fileScan = new Scanner(new File(FILE_PATH + FILE_NAME));
          
          //!! while (fileScan.hasNext()) { 
          while (fileScan.hasNextLine()) { //!! again, must check for nextLine here
             strokes = fileScan.nextLine();
             scoresScan = new Scanner(strokes);
             
             //!! **********
             scoresScan.useDelimiter("/"); //!! if you are using a Scanner you must change the delimiter
             
             int count = 0;
             while (scoresScan.hasNextInt()) {
                int score = scoresScan.nextInt();
                if (count == 0) {
                   System.out.print("Par: ");
                } else {
                   System.out.print("Player " + count + " = ");
                }
                System.out.print(score + ";   ");
                count++;
             }
             System.out.println();
    
          }
       }
    }
    Last edited by Fubarable; 11-24-2009 at 05:56 AM.

  7. #27
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    Java Code:
       import java.util.Scanner;
       import java.io.*;
    
        public class golfscores1
       
       {
           public static void main (String[] args) throws IOException
          {
             int g1score=0, g2score=0, g3score=0, g4score=0, par=0;
             String strokes;
          
             Scanner fileScan, scoresScan;
          
             fileScan = new Scanner (new File("scores.txt"));
             
             while (fileScan.hasNextLine())
             {
                strokes = fileScan.nextLine();
                scoresScan = new Scanner(strokes);
                scoresScan.useDelimiter("/");
                par += scoresScan.nextInt();
                g1score += scoresScan.nextInt();
                g2score += scoresScan.nextInt();
                g3score += scoresScan.nextInt();
                g4score += scoresScan.nextInt();
             }
          
          
             System.out.println("Par is: " + par);
             System.out.println("Golfer 1 scored: " + g1score);
             System.out.println("Golfer 2 scored: " + g2score);
             System.out.println("Golfer 3 scored: " + g3score);
             System.out.println("Golfer 4 scored: " + g4score);
          
          
          
             System.out.println();
             int small = g4score;
             if (small > g1score)
                small = g1score;
             if (small > g2score)
                small = g2score;
             if (small > g3score)
                small = g3score;
          
             if (small == g1score)
                System.out.println("Golfer 1 is the winner!");
             if (small == g2score)
                System.out.println("Golfer 2 is the winner!");
             if (small == g3score)
                System.out.println("Golfer 3 is the winner!");
             if (small == g4score)
                System.out.println("Golfer 4 is the winner!");
          }
       
       }
    this works if scores.txt looks like this:
    4/5/6/7/8
    1/2/3/4/5
    5/3/4/5/3
    etc...

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Please Help with Homework
    By theuser in forum Advanced Java
    Replies: 2
    Last Post: 07-30-2009, 03:37 PM
  2. help with homework
    By pinkdiamondgail in forum Advanced Java
    Replies: 7
    Last Post: 04-07-2009, 01:34 AM
  3. HELP Homework exercise
    By albi_87m in forum Java 2D
    Replies: 3
    Last Post: 04-04-2009, 05:10 AM
  4. Homework help...
    By robrobrob in forum New To Java
    Replies: 4
    Last Post: 10-17-2008, 04:24 AM
  5. Need help with homework.
    By JavaNewbie0000 in forum New To Java
    Replies: 2
    Last Post: 07-31-2008, 03:06 PM

Posting Permissions

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