Search code examples
javaarraysmethodsimportjoptionpane

Import .txt into Array, output W/ Dialog boxes


So I've been working on this code for awhile now and before adding the JOptionPane I tested it and there were 0 errors, then I added the JOptionPane and got between 27-38. So I clearly did something wrong in my JOptionPane but I'm not sure what, I only have experience using Scanner for the most part so any help would be appreciated. Thank you!

import javax.swing.JOptionPane;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class TestScores
{
    public static void main(String args[]) throws IOException {
        String filename = ("scores.txt");
        File file = new File(filename);
        Scanner inputFile = new Scanner(file);

        while (inputFile.hasNextInt()) {
            String line = inputFile.nextLine();
            ArrayList<Integer> scores = new ArrayList<Integer>();
            Scanner scanner = new Scanner(line);
            scanner.useDelimiter(",");

            while(scanner.hasNextInt()) {
                scores.add(scanner.nextInt());
            }

            scanner.close();
        }
    }

    public static int averageScore(int[] numbers) {
        int total = 0;
        for (int i : numbers) {
            total += i;
        }
        return total/(numbers.length);
    }

    public static int modeOfScores(int[] numbers) {
        int modeCount = 0;
        int mode = 0;     
        int currCount = 0;    
        int currElement;

        for (int candidateMode : numbers) {
            currCount = 0;

            for (int element : numbers) {
                if (candidateMode == element) {
                    currCount++;
                }
            }

            if (currCount > modeCount) {
                modeCount = currCount;
                mode = candidateMode;
            }
        }

        return mode;
    }

    public static int lowestScore(int[] numbers) {
        int lowest = numbers[0];

        for(int i = 1; i > numbers.length; i++) {
            if(numbers[i] < lowest) {
                lowest = numbers[i];
            }
        }
        return lowest;
    }

    public static int largestScore(int[] numbers) {  
        int largest = numbers[0];  
        for(int i = 1; i < numbers.length; i++) {  
            if(numbers[i] > largest) {  
                largest = numbers[i];  
            }
        }  
        return largest;
    }
    JOptionPane.showMessageDialog (null, "The Average number is: " + total);
    JOptionPane.showMessageDialog (null, "The Mode number is: " + mode);
    JOptionPane.showMessageDialog (null, "The Lowest number is: " + lowest);
    JOptionPane.showMessageDialog (null, "The Largest number is: " + largest);
}

EDIT: Here are all the errors I got, all coming from the JOptionPane portion

TestScores.java:86: error: <identifier> expected
   JOptionPane.showMessageDialog(null, "The Average number is: " +(averageScore));
                                ^
TestScores.java:86: error: illegal start of type
   JOptionPane.showMessageDialog(null, "The Average number is: " +(averageScore));
                                 ^
TestScores.java:86: error: illegal start of type
   JOptionPane.showMessageDialog(null, "The Average number is: " +(averageScore));
                                       ^
TestScores.java:86: error: ')' expected
   JOptionPane.showMessageDialog(null, "The Average number is: " +(averageScore));
                                                                ^
TestScores.java:86: error: ';' expected
   JOptionPane.showMessageDialog(null, "The Average number is: " +(averageScore));
                                                                  ^
TestScores.java:86: error: <identifier> expected
   JOptionPane.showMessageDialog(null, "The Average number is: " +(averageScore));
                                                                               ^
TestScores.java:86: error: illegal start of type
   JOptionPane.showMessageDialog(null, "The Average number is: " +(averageScore));
                                                                                ^
TestScores.java:86: error: <identifier> expected
   JOptionPane.showMessageDialog(null, "The Average number is: " +(averageScore));
                                                                                 ^
TestScores.java:86: error: ';' expected
   JOptionPane.showMessageDialog(null, "The Average number is: " +(averageScore));
                                                                                  ^
TestScores.java:87: error: illegal start of type
   JOptionPane.showMessageDialog(null, "The Mode number is: " +(modeOfScores));
              ^
TestScores.java:87: error: illegal start of type
   JOptionPane.showMessageDialog(null, "The Mode number is: " +(modeOfScores));
                                 ^
TestScores.java:87: error: illegal start of type
   JOptionPane.showMessageDialog(null, "The Mode number is: " +(modeOfScores));
                                       ^
TestScores.java:87: error: ')' expected
   JOptionPane.showMessageDialog(null, "The Mode number is: " +(modeOfScores));
                                                             ^
TestScores.java:87: error: ';' expected
   JOptionPane.showMessageDialog(null, "The Mode number is: " +(modeOfScores));
                                                               ^
TestScores.java:87: error: <identifier> expected
   JOptionPane.showMessageDialog(null, "The Mode number is: " +(modeOfScores));
                                                                            ^
TestScores.java:87: error: illegal start of type
   JOptionPane.showMessageDialog(null, "The Mode number is: " +(modeOfScores));
                                                                             ^
TestScores.java:87: error: <identifier> expected
   JOptionPane.showMessageDialog(null, "The Mode number is: " +(modeOfScores));
                                                                              ^
TestScores.java:87: error: ';' expected
   JOptionPane.showMessageDialog(null, "The Mode number is: " +(modeOfScores));
                                                                               ^
TestScores.java:88: error: illegal start of type
   JOptionPane.showMessageDialog(null, "The Lowest number is: " +(lowestScore));
              ^
TestScores.java:88: error: illegal start of type
   JOptionPane.showMessageDialog(null, "The Lowest number is: " +(lowestScore));
                                 ^
TestScores.java:88: error: illegal start of type
   JOptionPane.showMessageDialog(null, "The Lowest number is: " +(lowestScore));
                                       ^
TestScores.java:88: error: ')' expected
   JOptionPane.showMessageDialog(null, "The Lowest number is: " +(lowestScore));
                                                               ^
TestScores.java:88: error: ';' expected
   JOptionPane.showMessageDialog(null, "The Lowest number is: " +(lowestScore));
                                                                 ^
TestScores.java:88: error: <identifier> expected
   JOptionPane.showMessageDialog(null, "The Lowest number is: " +(lowestScore));
                                                                             ^
TestScores.java:88: error: illegal start of type
   JOptionPane.showMessageDialog(null, "The Lowest number is: " +(lowestScore));
                                                                              ^
TestScores.java:88: error: <identifier> expected
   JOptionPane.showMessageDialog(null, "The Lowest number is: " +(lowestScore));
                                                                               ^
TestScores.java:88: error: ';' expected
   JOptionPane.showMessageDialog(null, "The Lowest number is: " +(lowestScore));
                                                                                ^
TestScores.java:89: error: illegal start of type
   JOptionPane.showMessageDialog(null, "The Largest number is: " +(largestScore));
              ^
TestScores.java:89: error: illegal start of type
   JOptionPane.showMessageDialog(null, "The Largest number is: " +(largestScore));
                                 ^
TestScores.java:89: error: illegal start of type
   JOptionPane.showMessageDialog(null, "The Largest number is: " +(largestScore));
                                       ^
TestScores.java:89: error: ')' expected
   JOptionPane.showMessageDialog(null, "The Largest number is: " +(largestScore));
                                                                ^
TestScores.java:89: error: ';' expected
   JOptionPane.showMessageDialog(null, "The Largest number is: " +(largestScore));
                                                                  ^
TestScores.java:89: error: <identifier> expected
   JOptionPane.showMessageDialog(null, "The Largest number is: " +(largestScore));
                                                                               ^
TestScores.java:89: error: illegal start of type
   JOptionPane.showMessageDialog(null, "The Largest number is: " +(largestScore));
                                                                                ^
TestScores.java:89: error: <identifier> expected
   JOptionPane.showMessageDialog(null, "The Largest number is: " +(largestScore));
                                                                                 ^
TestScores.java:89: error: ';' expected
   JOptionPane.showMessageDialog(null, "The Largest number is: " +(largestScore));
                                                                                  ^
TestScores.java:90: error: reached end of file while parsing
}

^


Solution

  • Two problems, first. Your last 4 method calls are loose in the class.

    JOptionPane.showMessageDialog(null, "The Average number is: " +(total));
    JOptionPane.showMessageDialog(null, "The Mode number is: " +(mode));
    JOptionPane.showMessageDialog(null, "The Lowest number is: " +(lowest));
    JOptionPane.showMessageDialog(null, "The Largest number is: " +(largest));
    

    They need to be inside a method in this case.

    Second total, mode, lowest and largest are only locally defined within their respectively method scope.

    You need to store them as members of TestScores.

    Edit #1

    Small note, Java is written with lowerCamelCase http://en.wikipedia.org/wiki/CamelCase

    Edit #2

    I went through the whole code and noticed quite a few misstakes.

    1. An array in Java is not the same as an ArrayList. So you have to pick one, in this case an ArrayList will do the trick for you, as you can handle it more elegant.
    2. ArrayList does not have indexes as an array has. Instead you have to use its method get(index) which is accessed by the . operator.
    3. As the methods returns the values of total, mode, lowest and largest why not call them directly in your showMessageDialog()?

    Edit #3

    I don't have the scores.txt for testing right now. This gives no compilation errors.

    import javax.swing.JOptionPane;
    import java.io.*;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    public class TestScores
    {
        public static void main(String args[]) throws IOException { 
            String filename = ("scores.txt");
            File file = new File(filename);
            Scanner inputFile = new Scanner(file);
    
            List<Integer> scores = new ArrayList<Integer>();
    
            while (inputFile.hasNextInt()) {
                String line = inputFile.nextLine();
    
                Scanner scanner = new Scanner(line);
                scanner.useDelimiter(",");
    
                while(scanner.hasNextInt()) {
                    scores.add(scanner.nextInt());
                }
    
                scanner.close();
            }
    
            JOptionPane.showMessageDialog (null, "The Average number is: " + averageScore(scores));
            JOptionPane.showMessageDialog (null, "The Mode number is: " + modeOfScores(scores));
            JOptionPane.showMessageDialog (null, "The Lowest number is: " + lowestScore(scores));
            JOptionPane.showMessageDialog (null, "The Largest number is: " + largestScore(scores));
        }
    
        public static int averageScore(List<Integer> numbers) {
            int total = 0;
            for (int i : numbers) {
                total += i;
            }
            return total/(numbers.size());
        }
    
        public static int modeOfScores(List<Integer> numbers) {
            int modeCount = 0;
            int mode = 0;     
            int currCount = 0;    
            int currElement;
    
            for (int candidateMode : numbers) {
                currCount = 0;
    
                for (int element : numbers) {
                    if (candidateMode == element) {
                        currCount++;
                    }
                }
    
                if (currCount > modeCount) {
                    modeCount = currCount;
                    mode = candidateMode;
                }
            }
    
            return mode;
        }
    
        public static int lowestScore(List<Integer> numbers) {
            int lowest = numbers.get(0);
    
            for(int i = 1; i > numbers.size(); i++) {
                if(numbers.get(i) < lowest) {
                    lowest = numbers.get(i);
                }
            }
            return lowest;
        }
    
        public static int largestScore(List<Integer> numbers) {  
            int largest = numbers.get(0);  
            for(int i = 1; i < numbers.size(); i++) {  
                if(numbers.get(i) > largest) {  
                    largest = numbers.get(i);  
                }
            }  
            return largest;
        }
    }