• Post Reply Bookmark Topic Watch Topic
  • New Topic

stock system in Java  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi from my code you can and will probably realize that I'm quite a novice when it comes to programming but I have a question I'm making a stock system for the heck of it,so everything was going great until I hit the loop I am trying to use a for loop to add stock to the stock system,any way the question is maily about loops.How come I cannot declare a variable outside a loop and assign a value to it,for example I can declare as follows and I will not get an error,



but in my code(and yes its the only error I get I tested it) I get an error and can only declare and assign a value inside the loop,the reason for this is because I wanted to get the value from another class and use it in the loop ok so heres the code from both classes.



 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam

Could you please post the exact error message you are receiving?
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Boswell wrote:Adam

Could you please post the exact error message you are receiving?


Yes sorry I meant to post it,

mainn.java::38: error: cannot find symbol

b = first.wanted;

symbol: variable first
location:mainn
1 error

tool completed withy exit code 1
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam

The error is nothing to do with loops.

You have declared the variable first in your main method:


The error you are receiving is in the addstock method:


The compiler is telling you it cannot find the variable first as it has not been declared locally. I suspect you want to use the one declared in main but you can't as it is local to that method.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Boswell wrote:Adam

The error is nothing to do with loops.

You have declared the variable first in your main method:


The error you are receiving is in the addstock method:




The compiler is telling you it cannot find the variable first as it has not been declared locally. I suspect you want to use the one declared in main but you can't as it is local to that method.



Thanks for the reply but no that didn't work =( I tried to declare the variable or object outside the main method but go a lot more errors this time than one.still confused on why this isn't working
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:Thanks for the reply but no that didn't work =(

I suspect because you didn't understand what James was trying to tell you.

I tried to declare the variable or object outside the main method but go a lot more errors this time than one.still confused on why this isn't working

Did you declare that variable as static? If not, then main() won't be able to "see" it.

However, that's not a "solution path" you want to go down. Declaring static variables is almost invariably WRONG.

Basically, you need to create objects. Java is an Object-Oriented language, so it works best when it has objects to work with; so, in your case, you may need to set up a mainn object as a "launcher".

Alternatively, have a look at the MainIsAPain page for a slightly different approach.

Also:
1. you should read up on Java naming conventions. Classes should always start with a CAPITAL letter; fields and methods with a lower-case one.

2. Check out the StopCoding (←click) page. Right now, you seem to be bashing out code and expecting it to work; and that just won't happen. Programming requires thought - especially with something as involved as a stock system - so you need to plan how you're going to achieve your goals.

HIH

Winston

PS: Please UseCodeTags (←click) when you post code. I've added them for you this time - see how much better it looks?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are the errors you get after doing that? When you get an error, always include the exact error message in your post - error messages contain important information that explain what is wrong.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!