stilson
stilson

Reputation: 13

FileReader class, interface, or enum expected

I am trying to add up different lines of a text file, however, not all the lines are doubles (some lines have special characters), I have to add up all the double values. However, I keep getting this

ERROR:class, interface, or enum expected

for the isDouble method. Any help would be greatly appreciated!

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

public class quiz4
{
    public static void main(String[] args)
    {
        File input = new File("mystery.txt");
        try
        {
            double answer = 0;
            FileReader fr = new FileReader(input);
            BufferedReader br = new BufferedReader(fr);
            String currentLine = br.readLine();
            while (currentLine != null)
            {
                currentLine = br.readLine();
                if (isDouble(currentLine))
                {
                    double temp = Double.parseDouble(currentLine);
                    answer = currentLine + temp;
                }
                else
                {
                    continue;
                }
            }
        }
        catch (Exception e)
        {
        }
    }

    public static boolean isDouble(String cu)
    {
        try
        {
            double value = Double.parseDouble(cu);
            return true;
        }
        catch (NumberFormatException e)
        {
            return false;
       }
    }
}

Upvotes: 1

Views: 132

Answers (3)

zhumengzhu
zhumengzhu

Reputation: 778

First of all, I really suggest you use an IDE like IntelliJ IDEA, Eclipse, etc. They'll help you check your syntax error and give you very precise suggestions.

As the other answers suggest, your program has several problems.

  1. The first letter of your class name should be a capital;
  2. You can't assign a String to a Double;
  3. Because of unbalanced curly brackets, your isDouble method is outside of the class

The following code should solve your problems.

    public class Quiz4 {
        public static void main(String[] args) {
            File input = new File("mystery.txt");
            try {
                double answer = 0;
                FileReader fr = new FileReader(input);
                BufferedReader br = new BufferedReader(fr);
                String currentLine = br.readLine();
                while (currentLine != null) {
                    currentLine = br.readLine();
                    Double curDouble = tryParseDouble(currentLine);
                    if (curDouble != null) {
                        answer += curDouble;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public static Double tryParseDouble(String str) {
            try {
                return Double.parseDouble(str);
            } catch (Exception e) {
                return null;
            }

        }
    }

Upvotes: 1

Gerardo
Gerardo

Reputation: 979

This will solve your problems

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

public class quiz4{
    public static void main(String[] args){
        File input = new File("mystery.txt");
        try{
            double answer = 0;
            FileReader fr = new FileReader(input);
            BufferedReader br = new BufferedReader(fr);
            String currentLine = null;
            while((currentLine = br.readLine()) != null){
                if(isDouble(currentLine)){
                    double temp = Double.parseDouble(currentLine);
                    answer = Double.parseDouble(currentLine) + temp;
                }
                else{
                    continue;
                }
            }
        }catch(Exception e){

        }
    }

        public static boolean isDouble(String cu){
            try {
            double value = Double.parseDouble(cu);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

}

Upvotes: 0

Naman
Naman

Reputation: 32028

Your isDouble(String cu) method is not within the class :

catch(Exception e){}
    } //closing brace for main method
} //closing brace for the class
public static boolean isDouble(String cu){

Just make sure the closing braces are balanced after this method.

Upvotes: 0

Related Questions