Cannot Be Resolved To A Variable Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us r cannot be resolved to a variable android eclipse error Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community
Cannot Be Resolved To A Variable Jsp
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Java error cannot be resolved to a variable processing message “Cannot be resolved to a variable”? up vote 3 down vote favorite ok so I'm kinda new to java and I'm trying to make a class which will be able to ask the user to input a 12 digit long upc
Cannot Be Resolved To A Variable Java
code, check to make sure its a valid code, and then display if it is or not. I've got quite a few errors with the current program that I have and I can't seem to figure it out. This is the code that I have so far: public class Upc { private long upc; public Upc(long upcs) { upc = upcs; } public long getUpc() { int m = (n2 + n4 + n6 + n8 + n10); long n = (n1 + n3 + cannot be resolved to a variable try catch n5 + n7 + n9 + n11); long r = (10 - (m + 3 * n) % 10); long n12 = (int) (upc % 10); upc /= 10; long n11 = (int) (upc % 10); upc /= 10; long n10 = (int) (upc % 10); upc /= 10; long n9 = (int) (upc % 10); upc /= 10; long n8 = (int) (upc % 10); upc /= 10; long n7 = (int) (upc % 10); upc /= 10; long n6 = (int) (upc % 10); upc /= 10; long n5 = (int) (upc % 10); upc /= 10; long n4 = (int) (upc % 10); upc /= 10; long n3 = (int) (upc % 10); upc /= 10; long n2 = (int) (upc % 10); upc /= 10; long n1 = (int) (upc % 10); if (r == n12) { return (upc + " is a feasible UPC code"); } else { return (upc + " is an invalid UPC code"); } } } and my errors are as follows: 13 errors found: File: C:\Users\Andrew\Downloads\Upc.java [line: 10] Error: n2 cannot be resolved to a variable File: C:\Users\Andrew\Downloads\Upc.java [line: 10] Error: n4 cannot be resolved to a variable File: C:\Users\Andrew\Downloads\Upc.java [line: 10] Error: n6 cannot be resolved to a variable File: C:\Users\Andrew\Downloads\Upc.java [line: 10] Error: n8 cannot be resolved to a variable File: C:\Users\Andrew\Downloads\Upc.java [line: 10] Error: n10 cannot be resolved to a variable File: C:\Users\Andrew\Downloads\Upc.java [line: 11] Error: n1 cannot be resolved to a variable File: C:\Users\Andrew\Downloads\Upc.java [line: 11] Error: n3 cannot
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about
Cannot Be Resolved To A Variable Eclipse Debug
hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask
Cannot Be Resolved To A Variable Talend
Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. string cannot be resolved to a variable Join them; it only takes a minute: Sign up Error: String cannot be resolved to a variable up vote 0 down vote favorite Can't figure out how to fix. I am very new to Java. import java.util.Scanner; public class http://stackoverflow.com/questions/8177913/java-error-message-cannot-be-resolved-to-a-variable Greetings { public static void main(String[] args) { System.out.println("Hello there, what is your name? "); Scanner input = new Scanner(System.in); String = input.nextLine(); System.out.println("Well then, welcome to Java" + input); } } java share|improve this question edited Oct 18 '15 at 6:20 Tom 5,65292138 asked Oct 16 '15 at 4:42 Sean 134 1 please declare variable in this line, String =input.nextLine(); because you need to store into a string type variable. String s=input.nextLine(); –Kogile Oct 16 '15 at http://stackoverflow.com/questions/33162701/error-string-cannot-be-resolved-to-a-variable 5:39 @Unheilig Please avoid adding unrelated tags, thanks. –Tom Oct 18 '15 at 6:17 add a comment| 4 Answers 4 active oldest votes up vote 0 down vote accepted You never actually defined a String variable, which is why the compiler is complaining. Use the code below for maximum results: public class Greetings { public static void main(String[] args) { System.out.println("Hello there, what is your name? "); Scanner input = new Scanner(System.in); String theInput = input.nextLine(); System.out.println("Well then, welcome to Java" + theInput); if (input != null) { input.close(); // close the Scanner once finished with it } } } share|improve this answer edited Oct 16 '15 at 5:08 answered Oct 16 '15 at 4:44 Tim Biegeleisen 59.1k42153 now I am getting this error with your changes: 1 warning found: [line: 11] Warning: Resource leak: 'input' is never closed –Sean Oct 16 '15 at 5:04 Add if (input != null) { input.close(); } to the end of the main method. Ideally you'd handle this in a finally block but I don't think you need to bother with that stuff at this stage. –James S Oct 16 '15 at 5:08 Thank you!!!!!! –Sean Oct 16 '15 at 5:16 It's my pleasure (and obligation) to help you. –Tim Biegeleisen Oct 16 '15 at 5:17 @TimBiegeleisen "... (and obligation) ..." - Really? That's g
error and eclipse is telling me my finalAmount variable can't be resolved. From looking at it I know it's outside of the scoop of the for statement and everytime I create a local variable and assign a value to it (it's https://teamtreehouse.com/community/java-cannot-be-resolved-to-a-variable zero) my program gets messed up. My question is, how would I declare the finalAmount variable https://coderanch.com/t/593940/Applets/java/variable-resolved-Variable-Java-Programming locally? Thanks import java.util.Scanner; public class CompoundInterest { public static void main(String[] args){ double rate; double amount; double year; System.out.println("This program, with user input, computes interest.\n" + "It allows for multiple computations.\n" + "User will input initial cost, interest rate and number of years."); Scanner input = new Scanner(System.in); System.out.println("What is the inital cost?"); amount = input.nextDouble(); System.out.println("What is the interest rate?"); rate = cannot be input.nextDouble(); rate = rate/100; System.out.println("How many years?"); year = input.nextDouble(); for(int x = 1; x < year; x++){ double finalAmount = amount * Math.pow(1.0 + rate, year); // the below works but the problem is, it prints the statement out many times. I don't want that. /* System.out.println("For " + year + " years an initial " + amount + " cost compounded at a rate of " + rate + " will grow to " + finalAmount); */ } cannot be resolved System.out.println("For " + year + " years an initial " + amount + " cost compounded at a rate of " + rate + " will grow to " + finalAmount); } } 1 Answer omars 9,006 Points omars omars 9,006 Points almost 3 years ago Shane, Q: "eclipse is telling me my finalAmount variable can't be resolved" A: This is because you are declaring 'finalAmount' within the for loop. Once your for loop exits, 'finalAmount' goes out of scope. Meaning, Java has no clue it ever existed. Q: "My question is, how would I declare the finalAmount variable locally?" A: From what I know, you cannot declare a variable within a loop of any kind if you want to retain the previous value. When you declare a variable within a loop this is what happens: Your loop begins with an initial value of 0. (This is before the calculation takes place, double finalAmount;) A value is calculated and assigned to finalAmount. Your loop ends. If you loop condition is still valid (x < year), repeat from step one (finalAmount is redeclared and initialized). Someone please correct me if I said anything wrong about the above steps. Here is my suggested change to your code, I hope this helps. import java.util.Scanner; public class CompoundInterest { public static void main(String[] args){ double rate; double amount; double year; System.out.println("This program, with user input, computes interest.\n" + "It allows for mu
New Topic programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums Forum: Applets