This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Cannot find symbol  RSS feed

 
Andrei Spirits
Greenhorn
Posts: 2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. This is my first post on these forums and I need help with a project of mine. Basically, our teacher gave us 5 questions regarding Java. One of them was to show the output of a program fragment if it were given certain input sequences. The problem I'm having is with the code itself. It doesn't work.

So can anyone tell me how to fix this little issue I'm having?
 
Charles D. Ward
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
-I don't see a variable "In" declared anywhere.

-Saying that something "doesn't work" doesn't help anybody. Please read this page => It Doesn't Work is Useless

-Please post code (using code tags) instead of screenshots.
 
Andrei Spirits
Greenhorn
Posts: 2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you can see on the picture, I was given the error that it couldn't find the symbol. What I expected to happen was that it would just compile with no errors. What I forgot to mention was that I wasn't the one who made the program but rather, my teacher and what we're supposed to do is to answer 5 questions for a project. I only need help regarding one of them which is:
"What would be the output by the program fragment shown below is it were given the following input sequence?"
We're only supposed to find out the output of this program when we input the numbers -5, 0, and 25.
Here's the code btw:
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wecome to the Ranch, Andrei.

When a programmer gets an error message from the compiler, it is important for that programmer to remember this rule:

The Compiler is Never Wrong

Your compiler is saying, "cannot find symbol - variable In." (Just for the heck of it, I tried my compiler on your teacher's code, and it gave me the exact same error message.)

Now, because the compiler is never wrong, we have to believe that it cannot find what it says it cannot find. In this case, it cannot find a symbol. Which symbol can't it find? It told us: it cannot find "variable In." As Charles point out, your teacher's code doesn't declare a variable named "In" anywhere. At Line 10 of your listing, your code is:

That line uses a variable named "In," but you can't use a variable you haven't declared. At Line 6, the program declares a variable named "howMany." It declares it to be of type "int," and also sets its initial value to zero (which, by the way, is pointless because that initial value is never used). Before the program can use In, it must declare that somewhere as well. Moreover, at Line 10, In is being treated in a way that says it isn't just any old variable, but is a variable that holds a reference to something that has a method called "getInt" (most likely, In is supposed to hold a reference to an instance of a class, though that's not the only possibility).

So, the compiler has told you the problem: you are using a symbol it can't find. If that's the entire listing of your program, it's not going to run, and that's all there is to it. My guess is that "In" is supposed to be declared (and defined, which is how it gets that "getInt" method) elsewhere, and your teacher has either neglected to tell you where that declaration (and definition) can be obtained, or, maybe, you've misunderstood some part of the assignment.

That's about the best we can do, if that's the whole problem. The compiler is never wrong, so, if that's the complete program, it just isn't going to run.

If you have more info, share it with us, and we'll see what we can do.
 
Sandun Suranga Wijenayaka
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrei Spirits wrote:....5 questions regarding Java. One of them was to show the output of a program fragment if it were given certain input sequences.

Here, you have noted, "Input sequences" and you already have imported java.util.* package.
So probably there may be a Scanner to be used, but missed by your teacher.

Either you should answer as "Compile time error occurs"
or Recorrect the question by entering,

So getInt() have to be replaced by nextInt() and the missing In may probably be the variable In, used as the Scanner input variable. But make sure to undergo the standards of Java:" variables are started with Simple letters".
(Scanner in or Scanner input).
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is your teacher's name Sweeney, by some chance?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!