Results 1 to 7 of 7
  1. #1
    poupas is offline Member
    Join Date
    Nov 2010
    Posts
    11
    Rep Power
    0

    Question how to repeat my method?

    hi guys.. i need help im my program..what i need to do is a method that has a scanner and if the user puts a number that i dont want it gives an error and starts over the method. Example:

    public static int[] numbers();
    int n1 = scanner.nextInt();
    if (n1 < 1 || n1 > 49)
    {
    System.out.println ("Error!");
    // i dont know what to put here;
    }
    if i put break; it gives me error...and i think i need to put return but i dont know how to make it right :S

    well i apreciated if u guys could help me as soon as possible but either way thanks for everything..

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

    Default

    First you should create a method -- I don't see you doing this. Next any time you think you need to repeat something in code, think of using a loop. Since you don't know in advance how many times you'll want to loop before the loop begins, then a do-while loop seems like a good fit, since you know that it will call its code at least once.

    Best of luck!

  3. #3
    poupas is offline Member
    Join Date
    Nov 2010
    Posts
    11
    Rep Power
    0

    Default

    how can i use a do-wihle if i want to see if the numbers that the user puts are correct? the thing is i want the user to put different numbers and if the number is the same, the method goes back to the beginning and starts over,asking again for the number that was wrong...sorry for my english xD

  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 poupas View Post
    how can i use a do-wihle if i want to see if the numbers that the user puts are correct? the thing is i want the user to put different numbers and if the number is the same, the method goes back to the beginning and starts over,asking again for the number that was wrong...sorry for my english xD
    That's what the boolean statement of the while is for -- to decide whether to loop or not. So you check the value of the number input in the while's boolean statement, and if it is not within the correct range, you do not exit the loop. Please read the tutorial on using while and do-while loops in the Java tutorials: while loops

  5. #5
    poupas is offline Member
    Join Date
    Nov 2010
    Posts
    11
    Rep Power
    0

    Default

    i try to do it...but it gave me an output: 000000 :S
    my program is:

    Java Code:
    public class olaa {    
    		public static void main (String[]args)
    		{
    			Scanner scanner = new Scanner (System.in);
    			boolean loop = true;
    			while(loop)
    			{
    				int n = scanner.nextInt ();
    				int [] soma = new int [6];
    				for (int i = 0; i < soma.length;i++)
    				{
    					n = soma[i];
    					System.out.println (soma[i]);
    				}
    				if (n<1 || n>49)
    					loop = false;
    			}
    		}
    	}


    whats wrong? :S
    Last edited by Fubarable; 11-20-2010 at 02:18 PM. Reason: Moderator Edit: code tags added

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

    Default

    Again, you've not created a separate method as you stated you need. You also seem to be getting all your input inside of a for loop and not using this method to check the method. In other words, you're doing all your checking after you've collected all your data and printed it out.

    I suggest you concentrate on one thing and one thing only: create a simple method that accepts user input and continues to repeat until the number input is correct, and then the method should return that int.

    Once you've solved this, then use it in your for loop to get a bunch of numbers that fulfill your criteria.

    Also, if you use code tags when posting code, your code will be easier to read. I've added tags to your post above, and to learn to do this yourself, you'll want to click on the first link in my signature links.

  7. #7
    poupas is offline Member
    Join Date
    Nov 2010
    Posts
    11
    Rep Power
    0

    Default

    Fubarable thanks for everything and i know you are trying to help to get there by myself,but honestly im not getting :S could you give me a example please? :S and my english is not perfect as yours so its a little hard for me to undestand.. i really apreciated if you could put an example of what you are saying.
    Last edited by poupas; 11-20-2010 at 07:39 PM.

Similar Threads

  1. can't make ui:repeat work
    By kaghamdi in forum JavaServer Faces (JSF)
    Replies: 6
    Last Post: 12-31-2010, 12:43 PM
  2. turning off key-repeat
    By gib65 in forum Advanced Java
    Replies: 4
    Last Post: 08-25-2010, 03:58 PM
  3. Repeat a letter twice in java.
    By ustar in forum New To Java
    Replies: 3
    Last Post: 03-23-2010, 10:56 AM
  4. Help with a repeat
    By leeavital in forum New To Java
    Replies: 1
    Last Post: 01-01-2010, 10:42 PM
  5. Repeat while answer yes - do while loop
    By AJ2009 in forum New To Java
    Replies: 1
    Last Post: 01-10-2009, 07:19 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
  •