Results 1 to 4 of 4
  1. #1
    The_Steak is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default Help with an input.Line issue in a method I've created

    Hi all-

    I'm really new at Java, this being my first course and all. We're working with methods, and I've hit upon an issue that I can't seem to resolve.

    The error I'm getting is, "input cannot be resolved" in the method calcTotal. I realize the coding I've used probably isn't as efficient as it could be, but that's something I'm working on separately. I've pointed out exactly where the error is below.

    I'm not clear on why the error is being thrown here; is it because of the way I've set my parameter? Thanks in advance for any advice!

    System.out.println("Enter your letter grade, or lower-case 'x' twice to end: ");
    String inputLine = input.nextLine();
    letterGrade = inputLine.charAt(0);

    Java Code:
    import java.util.Scanner;
    
    
    public class GradeMethod2{
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
    
            //declare variables
            double gradeAvg;
            int counter;
            double gradeTotal;
            char letterGrade;
            double totalGrade;
            double gradeFinal;
            double gradeSum;
    
            //initialize values
            gradeTotal = 0;
            counter = 0;
            gradeAvg = 0;
            gradeFinal = 0;
            gradeSum = 0;
    
    
            System.out.println("Enter your letter grade, or lower-case 'x' to end: ");
            String inputLine = input.nextLine();
            letterGrade = inputLine.charAt(0);
    
     
            totalGrade = calcTotal( letterGrade, gradeTotal, counter, gradeSum );
    
      
            gradeAvg = calcGPA( gradeTotal, counter, gradeFinal );
    
    
            printGrade( gradeAvg );
    
    }       //end main
    
    public static double calcTotal( char letterGrade, double gradeTotal, int counter, double gradeSum) {
        while (true) {
            if (!"x".equals(letterGrade))
            {
                System.out.println("Enter your letter grade, or lower-case 'x' twice to end: ");
                String inputLine = input.nextLine();
                letterGrade = inputLine.charAt(0);
    
                if (letterGrade == 'a') {
                    gradeTotal = gradeTotal + 4;
                    counter = counter + 1;
                    } else if (letterGrade == 'A') {
                        gradeTotal = gradeTotal + 4;
                        counter = counter + 1;
                    } else if (letterGrade == 'b') {
                        gradeTotal = gradeTotal + 3;
                        counter = counter + 1;
                    } else if (letterGrade == 'B') {
                        gradeTotal = gradeTotal + 3;
                        counter = counter + 1;
                    } else if (letterGrade == 'c') {
                        gradeTotal = gradeTotal + 2;
                        counter = counter + 1;
                    } else if (letterGrade == 'C') {
                        gradeTotal = gradeTotal + 2;
                        counter = counter + 1;
                    } else if (letterGrade == 'd') {
                        gradeTotal = gradeTotal + 1;
                        counter = counter + 1;
                    } else if (letterGrade == 'D') {
                        gradeTotal = gradeTotal + 1;
                        counter = counter + 1;
                    } else if (letterGrade == 'f') {
                        gradeTotal = gradeTotal + 0;
                        counter = counter + 1;
                    } else if (letterGrade == 'F') {
                        gradeTotal = gradeTotal + 0;
                        counter = counter + 1;
                    }  else if (letterGrade == 'x') {
                      break;
                    }
                return gradeSum;
        }
        }
    }
    
    
    public static double calcGPA(double totalGrade, int counter, double gradeFinal) {
        gradeFinal = totalGrade/counter;
        return gradeFinal;
    }
    
    
    public static void printGrade ( double gradeAvg ) {
        System.out.printf("\nYour GPA is: %f", gradeAvg);
    
    }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Help with an input.Line issue in a method I've created

    It's a question of scope.

    When you say "Scanner input = new Scanner(System.in);" you are doing two things. (1) You are declaring the input variable and (2) you are giving it a value (a reference to a scanner reading from System.in)

    Now this variable scanner cannot be used everywhere, only when it is "in scope". The rules in Java are quite simple: scanner is in scope only up to the end of the block where it is declared. In your case this is the body of the main() method. So within main() you can use the variable input, but not anywhere else.

    This business of scope may seem an annoyance, but it does mean you can reuse variables in different methods.

    The solution in this case is to give input a larger scope: like the entire class. Of course it will have to be static so that it makes sense to refer to it within the static calcTotal() method.

    Java Code:
    public class Grade2 {
        //...
        private static Scanner input; // declare here
        //...
        public static void main(String[] args) {
            input = new Scanner(System.in); // assigned here, *not* declared
            // ...
        }
        public static double calcTotal(etc) {
            // scanner *can* be used here
        }
        // ...
    }

  3. #3
    The_Steak is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default Re: Help with an input.Line issue in a method I've created

    You know, I thought that might have been it- I tried to re-import the scanner within the method before, but it didn't seem to like it. :) I'll give it another shot, thanks!

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Help with an input.Line issue in a method I've created

    You're welcome. Of course the same remarks apply to any other variables you might wish to access from multiple methods.

Similar Threads

  1. Line number formatting issue
    By DarrylBurke in forum Suggestions & Feedback
    Replies: 5
    Last Post: 10-02-2011, 05:47 PM
  2. Console input issue
    By gaguevaras in forum New To Java
    Replies: 3
    Last Post: 05-01-2011, 03:42 AM
  3. Input a line from System.in without doing a CR/LF
    By inshallah in forum New To Java
    Replies: 4
    Last Post: 12-26-2010, 03:35 PM
  4. Urgent/ Help with average method in created class
    By johnjacob in forum New To Java
    Replies: 12
    Last Post: 12-07-2010, 02:47 AM
  5. Issue with printing line
    By Azndaddy in forum Advanced Java
    Replies: 1
    Last Post: 04-04-2008, 07:37 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
  •