• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

stock system in Java

 
Ranch Hand
Posts: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adam

Could you please post the exact error message you are receiving?
 
Adam Chalkley
Ranch Hand
Posts: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic