Results 1 to 11 of 11
  1. #1
    adamg106 is offline Member
    Join Date
    Apr 2010
    Posts
    9
    Rep Power
    0

    Default Repeating Do-While Statement

    Hi,

    I'm trying to create a program that continues to ask for new values, as long as the entered value does not equal a given value. However, I am told that the variable i is a duplicate.

    Any and all help would be greatly appreciated.

    Java Code:
    public class FindRange extends ConsoleProgram {
    	
    	private static final int SENTINEL = 0;
    	
    	public void run() {
    		println("This will find the smallest and largest value.");	
    		int i;
    		do {
    			int i = readInt("?");
    		} while(i != SENTINEL);
    	}
    }

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    You have declared the variable outside the loop, so you don't declare it again inside -- you just use it.

    -Gary-

  3. #3
    adamg106 is offline Member
    Join Date
    Apr 2010
    Posts
    9
    Rep Power
    0

    Default

    Gary,

    Thanks so much for the help. I'm not sure I follow, though. Should I not declare the variable outside the loop? Sorry for being such a newbie.

    -Adam

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by adamg106 View Post
    Gary,

    Thanks so much for the help. I'm not sure I follow, though. Should I not declare the variable outside the loop? Sorry for being such a newbie.

    -Adam
    He already told you what to do: don't re-declare it inside the loop.

  5. #5
    adamg106 is offline Member
    Join Date
    Apr 2010
    Posts
    9
    Rep Power
    0

    Default

    Understood. But then when do I declare
    Java Code:
    int i = readInt("?")
    ?

    Thanks,
    Adam

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You don't. You declare the "i" before the loop and use it without re-declaring it in the loop. You will not declare and use it on the same line.

    A site that might help you is the Sun tutorials. You can find them here: The Really Big Index

  7. #7
    adamg106 is offline Member
    Join Date
    Apr 2010
    Posts
    9
    Rep Power
    0

    Default

    I'm sorry, I'm not following. I've tried the sun tutorials, but don't really like then.

    Assume I'm an idiot. How would you explain this to me?

  8. #8
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Java Code:
                    i = readInt("?");
    -Gary-

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by adamg106 View Post
    I'm sorry, I'm not following. I've tried the sun tutorials, but don't really like then.
    Like any intellectual endeavor, these make more sense the more you use them. I strongly strongly suggest you keep on plugging away at them and learn to get very familiar with them. With time you will eventually like them, trust me.

    Assume I'm an idiot. How would you explain this to me?
    What you're doing in your loop is declaring a variable and using it at the same time, and you shouldn't be doing this in this situation:
    Java Code:
    int i = someMethod();

    This is how you declare a variable:
    Java Code:
    int i;

    and this is how you use a variable without declaring it:
    Java Code:
    i = someMethod();
    Happy studying.

  10. #10
    adamg106 is offline Member
    Join Date
    Apr 2010
    Posts
    9
    Rep Power
    0

    Default

    Thank you Fubrable. I'll keep plugging away. It's killing me, but, well, I know it's good for me.

  11. #11
    karno is offline Member
    Join Date
    Mar 2010
    Posts
    16
    Rep Power
    0

Similar Threads

  1. Non-Repeating Random Integers
    By Psyclone in forum New To Java
    Replies: 5
    Last Post: 01-31-2010, 10:04 PM
  2. for statement help
    By helpisontheway in forum New To Java
    Replies: 5
    Last Post: 11-14-2009, 05:14 PM
  3. Replies: 1
    Last Post: 07-04-2008, 06:39 PM
  4. adjacent repeating letters
    By artemis_r2 in forum New To Java
    Replies: 1
    Last Post: 11-17-2007, 05:48 AM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 05:45 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
  •