Results 1 to 4 of 4
- 11-06-2011, 03:18 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 2
- Rep Power
- 0
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); } }
- 11-06-2011, 03:34 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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 } // ... }
- 11-06-2011, 03:37 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 2
- Rep Power
- 0
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!
- 11-06-2011, 03:42 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Similar Threads
-
Line number formatting issue
By DarrylBurke in forum Suggestions & FeedbackReplies: 5Last Post: 10-02-2011, 05:47 PM -
Console input issue
By gaguevaras in forum New To JavaReplies: 3Last Post: 05-01-2011, 03:42 AM -
Input a line from System.in without doing a CR/LF
By inshallah in forum New To JavaReplies: 4Last Post: 12-26-2010, 03:35 PM -
Urgent/ Help with average method in created class
By johnjacob in forum New To JavaReplies: 12Last Post: 12-07-2010, 02:47 AM -
Issue with printing line
By Azndaddy in forum Advanced JavaReplies: 1Last Post: 04-04-2008, 07:37 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks