I dont know what I am doing wrong as when I write my code it seems to be no different than other sample pieces of code my instructor uses in teaching us Java. However I keep running into this same problem over and over again and I don't know what is causing this to happen.
The problem I keep having is this: I am writing code that contains a main method and sometimes one plus other methods that I myself am creating and then calling within my program. For some reason I keep running into a problem in which the methods that I have created cannot access the variables that were defined earlier in the program within the main method. From what I have learned thus far, any variable that is defined within the main method should be accessible to the rest of the program right? The scope of the variable is for the rest of the program or is it only for the main method? The real confusing part is that last night I wrote a program that pretty much followed the exact same format as one that is contained in my text book on Java. Only I cannot get the method to get the data from a variable in my program.
Could someone please take a look at this code and tell me what is wrong with it. The error I keep getting on compiling is "symbol not found" and it is pointing to the "lator" varialbe.
I am not getting why the symbol of lator cannot be found upon compiling??? If the "lator" variable is out of scope, then shouldnt the fact that I am passing it as an argument to the method overcome that
problem? The thing I really don't get is that I see example after example in my textbook that do things just like this, i.e. passing a variable to a method, and they have no issues with it. When passing a variable to a method as an argument doesnt that overcome the limitations of scope? And also, when dealing with scope, what exactly is the scope of variables when you are dealing with methods such as the ones earlier in my program. Another, thin g I had issues with was the scanner object. Even though It is created in the beginning of the program, when I try to use it in the totalPrinter method, it doesnt work. So I have to create another one. I fundamentally do not understand the way in which methods and the scope of variables interact. It seems to work randomly in some things and other things dont work.
If someone could explain what is wrong with my program and then explain this to me that would be great
Why use Windows when you can step through doors into a whole new world of computing without security holes and instability. Join us, and leave windows behind. Try Linux!!!
Oh thats right. I want remembering that that total name was actually how the variable is to be reffered to within the method.
Anyways now I have another issue that I cant figure out. When I run the program now, it asks the user to enter the filename, I enter a file name, and it repeats asking to enter the filename again. Then when I reenter it, it says the file already exists and then closes down. Never printing the data to the file or even creating it for that matter.
This is confusing as none of that code is part of a loop.
The program should test to see if the file exists and then print the data if it doesnt. The file name I enter does not exist and I cant figure out were the loop is coming in to play.
Shane Miller wrote:This is confusing as none of that code is part of a loop.
Does it make sense?
Joined: Oct 11, 2009
Yea I caught that right after I posted that message back to you. Thanks for the help...... Now if I can just understand how to nest loops and make them work the way I need them to. I have to write a program that is a guessing game. But not only is it a guessing game but it has to do the following things:
1. ask the user to pick number between 1 and 100
2. assign random number to a variable for the game
3. test users input and tell them there either too high or too low until they get it right.
4. keep a count of the number of guesses it takes to get it right and display the number after the user gets the number right
5. then ask them if they want to play again using 1 for yes and 0 for no.
I have the basics of the code down. I just cant seem to get it to work properly with all the pieces in place. Theres always one thing that isnt working right because of the way the loops are nested. I am trying to use while loops and if statements to accomplish this and somethings always goes wrong. I actually had it perfect almost at one point except for the fact that when it asked to enter 1 or 0 for whether or not they wanted to play again, the program would then ask them to pick a number between 1 and 100 again even if they picked 0 to quit. I couldnt figure out how to get that statement isolated but yet still in the loop without it being displayed even if the user wanted to quit. And in the process I saved over the code in which I had it almost perfect, now I am trying to get it back.
Can anyone tell me if I have the right idea though? Or do I have to use something else, I have tried all types of combos using the if, else, and while loops and just cant get it perfect. I just cant seem to figure out how it could be done using any thing else though. A do while loop wouldnt work, either would a for loop.
You already have done most of things needed for that in the previous code. Just need to iterate (with all the logic inside it for those checks needs to be performed) over and see how it works. If you can post what you have already It will be easy to give some ideas.
Joined: Oct 11, 2009
Well here is the code for the guessing game that I have thus far, the problem Im having right now is that the program is terminating even if the user inputs 1 to play again. I am trying to get that down before I worry about adding the acumulator to the message that is printed out after the player wins the game.
I should also add, if I use a while loop instead of a do while loop, when the user gets the guess right and then enters 1 to play again, the program uses the number 1 to restart the program and also uses it as the players initial guess for the second round of the game. I cant seem to figure out why this is as there are two different variables for the guess and the sentinel value. Also if I add another statement after asking the player if they want to play again the game will always say they got it right upon there second guess even if its the same number.
To get the code working just move the code after the "do.. while" inside the "do ..while" under another "else" block and you'll have to add the code to prompt the user to enter the value again & to generate the random number there too. And you should terminate the loop only user doesn't want to play again, shouldn't check for the guessed number. Of course you may refactor the code latrer to remove the duplicates etc..
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: I keep having a issue with variables and methods.