0

I've been spinning my wheels on a homework assignment for the last 3 hours and am not making very much progress. I was wondering if someone could help nudge me in the right direction.

The assignment is to read data from a file and convert it to something that is more reader friendly. The data file looks something like this:

0.30 0.30 0.40

161

3333 70 60 50

4444 50 50 50

5555 80 90 80

0

162

1212 90 85 92

6666 60 80 90

7777 90 90 90

8888 95 87 93

9999 75 77 73

0

263

2222 90 65 75

8989 60 40 60

9090 70 80 30

0

The file contains 5 different types of numbers.
1. The three digit numbers are course numbers.
2. The four digit numbers are student numbers.

  1. The two digit numbers are scores for assignments, mid terms, and finals for each student.
  2. The decimals across the top are used to find the weighted average for the scores.
  3. zeros are a break between courses.

I am supposed to read all of the data in the file and then format and output it so it's easy to read:

Grade Data For Class 161

 ID   Programs  Midterm  Final  Weighted Average  Programs grade
 --   --------  -------  -----  ----------------  --------------
3333     70       60      50         59.00        Pass
4444     50       50      50         50.00        Fail
5555     80       90      80         83.00        Pass
Class Average:  64.00

I have the scanner setup and am able to parse out the numbers from the text, my problem is that they are all saved as Strings so I can't do any mathematical checks on them. I'm starting to wonder if this is even the best way to go about this.

import java.util.Scanner;
import java.io.File;
import java.io.IOException;

public class classAverage
{
    public static void main(String[] args) throws IOException
    {
        //in variable equals entire file
        Scanner in = new Scanner(new File("courseData.txt"));

        int classID;



        System.out.println("ID  Programs  Midterm  Final  Weighted Average  Programs Grade");
        System.out.println("--  --------  -------  -----  ----------------  --------------");

        while(in.hasNextLine())   //While file has new lines
        {
            //line equals each line of text
            String line = in.nextLine();
            Scanner lineParser = new Scanner(line);
            System.out.println(line);

            for(; lineParser.hasNext();)
            {
                //number = each number
                String number = lineParser.next();
                System.out.println(number);



                if(number < 1 && number > 0)
                {
                    double programsAverage = number.nextDouble();
                    double midtermAverage = number.nextDouble();
                    double finalAverage = number.nextDouble();
                    System.out.println(programsAverage);
                    System.out.println(midtermAverage);
                    System.out.println(finalAverage);

                }

            }

        }



    }

}

Update I've included my updated code. My problem now is my conditions in my for statements. These should be checking the values of the incoming Scanner data to see if the data is:

  • a weight calculator(0,1)
  • a score(1,100),
  • a classNumber (100,400),
  • a studentNumber(1000,9999),
  • a class separator (0).

I am thinking something like:

for(in.next(); in < 100 && in > 1; next());

This isn't quite doing it though.

import java.util.Scanner;
import java.io.File;
import java.io.IOException;

/**
 * Write a description of class classAverage here.
 * 
 * @author 
 * @version 
 */
public class classAverage
{
    public static void main(String[] args) throws IOException
    {
        //in variable equals entire file
        Scanner in = new Scanner(new File("courseData.txt"));

        int classNumber;
        int studentNumber;
        int programs;
        int midterm;
        int finals;


        double programWeight = in.nextDouble();
        double midtermWeight = in.nextDouble();
        double finalWeight = in.nextDouble();

        //System.out.println(programWeight +  " " + midtermWeight + " " + finalWeight);
        for(int k = 0; k < 3; k++)
        {
            for(int i = 0; i <= 0; i++)
            {
                classNumber = in.nextInt();

                System.out.println("Grades for class: " + classNumber);
                System.out.println(" ID    Programs  Midterm  Final  Weighted Average  Programs Grade");
                System.out.println(" --    --------  -------  -----  ----------------  --------------");
            }

            int studentCount = 0;
            double sumAverage = 0.0;

            for(int j = 0; j <= 2; j++)
            {
                studentNumber = in.nextInt();
                programs = in.nextInt();
                midterm = in.nextInt();
                finals = in.nextInt();
                studentCount++;
                double weightedAverage = (programWeight * programs) + (midtermWeight * midterm) + (finalWeight * finals);
                sumAverage += weightedAverage;

                System.out.printf("%d      %d        %d      %d        %.2f          ", studentNumber,programs,midterm,finals,weightedAverage);
                if(programs >= 70)
                {
                    System.out.print("PASS");
                } else {
                    System.out.print("FAIL");
                }
                System.out.println();
            }
            double classAverage = sumAverage / studentCount;
            System.out.printf("Class average is: %.2f", classAverage); 
            System.out.println("\n\n");

        }



    }

}

1 Answer 1

1

You have 2 options here :

  1. Use Scanner#hasNextInt() and nextInt() instead of nextLine() to read Integers directly instead of numbers in String format.

  2. Keep your reading / scanning code intact. Use Integer.parseInt() to convert String to Integers. --> Preferred solution. As it is more efficient.

1
  • 1
    exactly, this way you do not need lineParser object May 6, 2015 at 6:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.