Results 1 to 8 of 8
  1. #1
    Masken2 is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default Need help with code

    Can someone help with this code and tell me what and why its wrong

    for(int i = 0; i <= 5; i++){
    int lastNumber ="";
    lastNumber = i;
    }

    System.out.println(lastNumber);


    Thx for the help Masken2

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default

    How about you tell us what's wrong? What's happening? Are you getting an Exception? What did you expect to happen? What happened instead? Be specific.

    When posting code, make sure you use the code tags. Check out other posts for examples of how to do that.

    But here are the first couple of problems that pop out at me: you're trying to assign a String value to an int variable, then you're trying to print that variable outside of its scope.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Masken2 is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Thx for the reply but thats the whole code i got from my teacher and she told me to find whats wrong with the code and make it to work

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default

    Quote Originally Posted by Masken2 View Post
    Thx for the reply but thats the whole code i got from my teacher and she told me to find whats wrong with the code and make it to work
    So you want us to do your homework for you?

    What happens when you put this into a main function and compile and run it?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Masken2 is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    hehe not like that.
    I dont understand were to begin to solve that


    what i get in is the error messeage: lastNumber cannot be resolved to a variable

    i have been trying to change the code but nothing helps.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default

    Quote Originally Posted by Masken2 View Post
    hehe not like that.
    I dont understand were to begin to solve that


    what i get in is the error messeage: lastNumber cannot be resolved to a variable

    i have been trying to change the code but nothing helps.
    Okay, so what do you find when you google the error message?

    I'll give you a hint: if you declare a variable inside a block (such as an if statement), you can't access it outside of that block. Just like you can't access a variable you declare in one method from another method.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Masken2 is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Ok Thx I got it when you said it. I i had google it before but i relly didnt get then.
    Thx again you have been really helpful

  8. #8
    Debugger is offline Member
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default

    Checking out the scope of variables is important, google for that, and it will save you hours of debugging later just to find out something was declared in the wrong spot. Also think ahead, and when declaring a variable decide whether and where you made need to use it again

Similar Threads

  1. Code to check if a piece of code is legal.
    By vahshir in forum New To Java
    Replies: 3
    Last Post: 08-30-2010, 04:21 AM
  2. Replies: 1
    Last Post: 03-04-2010, 11:19 AM
  3. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:14 AM
  4. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •