mellowmorgan Posted August 31, 2012 Share Posted August 31, 2012 OK... so I've just started programming in Java and I decided to write a little program where the user enters a number and the computer determines whether or not it's a prime. The math work behind it is functioning perfectly(I know because I ran it repeatedly without do-while), but my do-while loop isn't, for some reason it exits the loop after one go or gives me an error message about variables). Can you tell me what I'm doing wrong? Thanks! Oh, btw I'm writing it in Notepad++ and running it in command prompt. import java.util.Scanner; public class PrimeCalculator { public static void main(String[] args) { Scanner sc = new Scanner(System.in); do{ System.out.println("Please enter a number greater than 0"); int input; input = sc.nextInt(); int counter = input-1; double remainder=1.0; if (input==1) System.out.println("This is not a prime number"); else{ while ((counter > 1) && (remainder!=0)) { remainder= input%counter; counter--;} if (remainder==0) System.out.println("This is not a prime number"); else System.out.println("This is a prime number!"); } System.out.println("Would you like to continue? (Yes or No)"); String cont = sc.nextLine(); } while (cont=="Yes"); } } Link to comment Share on other sites More sharing options...
Cap'n Refsmmat Posted August 31, 2012 Share Posted August 31, 2012 It would help if you told us what the error message is. Link to comment Share on other sites More sharing options...
mellowmorgan Posted August 31, 2012 Author Share Posted August 31, 2012 It would help if you told us what the error message is. Sorry! OK, the error message is: PrimeCalculator.java:29: error: cannot find symbol while (cont == "Yes"); symbol: variable cont Link to comment Share on other sites More sharing options...
moth Posted August 31, 2012 Share Posted August 31, 2012 I'm not sure this will solve the problem, but the in the statement: "while(cont=="Yes);" it is not clear whether you are comparing the pointers (cont and "Yes") or the strings they point to. you might try replacing the == with a string member function to compare two strings. Link to comment Share on other sites More sharing options...
Greg H. Posted August 31, 2012 Share Posted August 31, 2012 (edited) You need to put the cout variable outside the inner loop. You're getting hung up on a scope problem. Also, never use == to compare two strings. Edit: I should have added, as your new to the language The reason it's failing is because your cont variable is only available INSIDE the do loop. It actually gets moved off to garbage collection and becomes unavailable the second the code reaches the closing braces of that loop, which is prior to the while statement. Think of each loop as a little mini program - the variables created inside that loop are not accessible outside the loop because they don't exist as far as the class is concerned. As for the == thing, when you compare two objects (such as Strings, Longs, Cars, or whatever), you should always use the object's .equals method to determine equivalence. Using == compares memory addresses, and will only return true if the memory addresses match. When comparing simple types, such as int or char, using == is perfectly fine. As an aside, it's considered good practice, when comparing string variables to string constants, to use the constant as the base i.e. "YES".equals(cont), rather than cont.equals("YES"). This helps avoid an occasioanlly hard to find Null Pointer Exception if the cont variable happens to have no value at the time the comparison is done. See the revised code below import java.util.Scanner; public class PrimeCalculator{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); String cont = "No"; do{ System.out.println("Please enter a number greater than 0"); int input; input = sc.nextInt(); int counter = input-1; double remainder=1.0; if (input==1){ System.out.println("This is not a prime number"); }else{ while ((counter > 1) && (remainder!=0)) { remainder= input%counter; counter--; } } if (remainder==0){ System.out.println("This is not a prime number"); }else{ System.out.println("This is a prime number!"); } System.out.println("Would you like to continue? (Yes or No)"); cont = sc.nextLine(); } while ("YES".equals(cont.toUpperCase()); } } Edited August 31, 2012 by Greg H. Link to comment Share on other sites More sharing options...
mellowmorgan Posted September 1, 2012 Author Share Posted September 1, 2012 You need to put the cout variable outside the inner loop. You're getting hung up on a scope problem. Also, never use == to compare two strings. Edit: I should have added, as your new to the language The reason it's failing is because your cont variable is only available INSIDE the do loop. It actually gets moved off to garbage collection and becomes unavailable the second the code reaches the closing braces of that loop, which is prior to the while statement. Think of each loop as a little mini program - the variables created inside that loop are not accessible outside the loop because they don't exist as far as the class is concerned. As for the == thing, when you compare two objects (such as Strings, Longs, Cars, or whatever), you should always use the object's .equals method to determine equivalence. Using == compares memory addresses, and will only return true if the memory addresses match. When comparing simple types, such as int or char, using == is perfectly fine. As an aside, it's considered good practice, when comparing string variables to string constants, to use the constant as the base i.e. "YES".equals(cont), rather than cont.equals("YES"). This helps avoid an occasioanlly hard to find Null Pointer Exception if the cont variable happens to have no value at the time the comparison is done. See the revised code below import java.util.Scanner; public class PrimeCalculator{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); String cont = "No"; do{ System.out.println("Please enter a number greater than 0"); int input; input = sc.nextInt(); int counter = input-1; double remainder=1.0; if (input==1){ System.out.println("This is not a prime number"); }else{ while ((counter > 1) && (remainder!=0)) { remainder= input%counter; counter--; } } if (remainder==0){ System.out.println("This is not a prime number"); }else{ System.out.println("This is a prime number!"); } System.out.println("Would you like to continue? (Yes or No)"); cont = sc.nextLine(); } while ("YES".equals(cont.toUpperCase()); } } Thank you for your reply. I changed my code according to your revisions, and now there are no errors, but it still isn't letting me enter YES or NO, it just ends the program after printing "Would you like to continue?" :/ Your explanation was, to be honest, a bit beyond me because I have a poor grasp of pointers. This is inexcusable on my part because I've taken a prior programming class where we learned about pointers, and so I should know these things. Link to comment Share on other sites More sharing options...
moth Posted September 1, 2012 Share Posted September 1, 2012 (edited) It's hard to tell for sure if nextLine blocks, waiting for input, or just returns. If it just returns before you can enter a line the while condition will never be met. It does throw an exception if it cant find a line to return so you might want to set up a try/catch block (or whatever java calls exception handling) After looking at the java docs, hasNextLine is a better choice than an exception handler. Just replace nextLine with hasNextLine. When hasNextLine returns true, use nextLine to get the input into cont. Edited September 1, 2012 by moth Link to comment Share on other sites More sharing options...
mellowmorgan Posted September 1, 2012 Author Share Posted September 1, 2012 You need to put the cout variable outside the inner loop. You're getting hung up on a scope problem. Also, never use == to compare two strings. Edit: I should have added, as your new to the language The reason it's failing is because your cont variable is only available INSIDE the do loop. It actually gets moved off to garbage collection and becomes unavailable the second the code reaches the closing braces of that loop, which is prior to the while statement. Think of each loop as a little mini program - the variables created inside that loop are not accessible outside the loop because they don't exist as far as the class is concerned. As for the == thing, when you compare two objects (such as Strings, Longs, Cars, or whatever), you should always use the object's .equals method to determine equivalence. Using == compares memory addresses, and will only return true if the memory addresses match. When comparing simple types, such as int or char, using == is perfectly fine. As an aside, it's considered good practice, when comparing string variables to string constants, to use the constant as the base i.e. "YES".equals(cont), rather than cont.equals("YES"). This helps avoid an occasioanlly hard to find Null Pointer Exception if the cont variable happens to have no value at the time the comparison is done. See the revised code below import java.util.Scanner; public class PrimeCalculator{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); String cont = "No"; do{ System.out.println("Please enter a number greater than 0"); int input; input = sc.nextInt(); int counter = input-1; double remainder=1.0; if (input==1){ System.out.println("This is not a prime number"); }else{ while ((counter > 1) && (remainder!=0)) { remainder= input%counter; counter--; } } if (remainder==0){ System.out.println("This is not a prime number"); }else{ System.out.println("This is a prime number!"); } System.out.println("Would you like to continue? (Yes or No)"); cont = sc.nextLine(); } while ("YES".equals(cont.toUpperCase()); } } It works now! Yay! I replaced the cont == "Yes" with "Yes".equals(cont) I was so confused why my original didn't work because in c++ it seems to work fine to do this with strings but I guess strings are quite different in java? Link to comment Share on other sites More sharing options...
doG Posted September 1, 2012 Share Posted September 1, 2012 It works now! Yay! Great. Going forward I suggest you get an IDE like Eclipse that you can use to debug your work. It will allow you to set breakpoints and run your code interactively in debug mode. It makes troubleshooting much easier. Link to comment Share on other sites More sharing options...
mellowmorgan Posted September 2, 2012 Author Share Posted September 2, 2012 Great. Going forward I suggest you get an IDE like Eclipse that you can use to debug your work. It will allow you to set breakpoints and run your code interactively in debug mode. It makes troubleshooting much easier. I have Visual Studio and Eclipse but my mean professor won't let us use them in class! Link to comment Share on other sites More sharing options...
doG Posted September 2, 2012 Share Posted September 2, 2012 Learn to use jdb then. It should have come with the jdk you are using. It supports breakpoints and stepping. Link to comment Share on other sites More sharing options...
Greg H. Posted September 2, 2012 Share Posted September 2, 2012 It works now! Yay! I replaced the cont == "Yes" with "Yes".equals(cont) I was so confused why my original didn't work because in c++ it seems to work fine to do this with strings but I guess strings are quite different in java? The reason is that in Java when you compare objects (and nearly everything in Java is an object, except for the primitive variable types), == compares the memory location instead of the value stored in that memory location. That may be different from C++, I'm not sure. If you want to compare the value of an object (like a String) you should use .equals() at all times. I have Visual Studio and Eclipse but my mean professor won't let us use them in class! If your professor won't let you use Eclipse to learn to write Java I question his methods. Every Java class I have ever taken has used Eclipse as the editor. It saves a ton of time running and debugging applications. That aside, I am glad it's working for you now. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now