Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default For loop with scanner and strings

    I have this for loop

    Java Code:
    int i;
            
            for (i = 1; i <= 4; i++)
            
            System.out.println("Attempt " + i);
    what I want to do is allow the user to enter a number each time the loop makes a round

    Attempt 1
    (User input)
    Attempt 2
    (User input)
    ......

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: For loop with scanner and strings

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: For loop with scanner and strings

    Thank you but that is one step behind me. That's how I got the for loop in the first post. Now I need to implement the user input aspect.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: For loop with scanner and strings

    Look at the Scanner class. It has several methods you can use to read data from a user via the System.in connection to the console/keyboard.
    If you search on the forum, you will find many code samples.


    BTW When coding a loop or if statement you should always enclose the following statement(s) in {}s
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: For loop with scanner and strings

    OK, I need to repeat a section of code in the for loop to do the exact same thing outside of it. How can this be done without retyping the same code over somewhere else?

  6. #6
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: For loop with scanner and strings

    Make a method with that code inside, which you can call when ever you need

  7. #7
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: For loop with scanner and strings

    I made this method

    Java Code:
     public static void Method()
        {  
            Scanner keyb = new Scanner(System.in);
            int x = keyb.nextInt();
     
            if (x <= 0)
            System.out.println("This is not a positive integer.");
            else 
            System.out.println("This is a positive integer.");
        }
    Now I'm trying to call it in a for loop. How can this be done?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: For loop with scanner and strings

    Write a loop and put the calling code inside the loop, leave the definition(s) outside of the loop.

    When coding a loop or if statement you should always enclose the following statement(s) in {}s
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: For loop with scanner and strings

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: For loop with scanner and strings

    If a string occurs twice in a for loop how can you break out of it?

  11. #11
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: For loop with scanner and strings

    here's my code

    Java Code:
     if (positive.contains(positive))
                {
                  break;  
                }
    I only want the for loop to break when String positive occurs. I'm not to familiar with how break works.

  12. #12
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: For loop with scanner and strings

    Looks like you need equals() method instead of contains()

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: For loop with scanner and strings

    Quote Originally Posted by jwl View Post
    I'm not to familiar with how break works.
    Branching Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: For loop with scanner and strings

    if (positive.contains(positive))
    what is that statement supposed to test for? When will it ever be false?
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: For loop with scanner and strings

    I figured out how to make to break work.

    Java Code:
        if (x <= 0)
                     System.out.println(incorrect);
                else 
                {
                    System.out.println(positive);
                    break;
                }
    Now I need to say if this happens good else this. In the else part I need to say that if else happens twice, break.

  16. #16
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: For loop with scanner and strings

    You can introduce an int variable as a counter and increase it in the else statement.

    Java Code:
    int counter
    if (condition)            
        print something        
    else if (counter is lower than 2 or 1 depending on your requirement)
         print something
         counter++
         break the loop
    else // you can even make sure to break
         break
    or you can put this code in while loop...
    Last edited by milovan; 09-10-2012 at 12:00 AM. Reason: more adequate behavior

  17. #17
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: For loop with scanner and strings

    My code is in a for loop. What I want to do is possibly create a nested if statement.
    Java Code:
                for (i ...... i++)
               {
                int num = keyb.nextInt();
           
                if (num <= 0)
                     System.out.println(incorrect);
                else 
                {
                    System.out.println(positive);
             
                    // this is where I want to break if positive string has occurred two times. 
                    System.out.println("Success!");
                    break;             
                } 
                }
    What happens is after the user inputs a number a string appears. If the user meets the requirements to print the string two times then print .... and it's done.
    What I want to know is how to tell if the string has been initialized twice.

    I've tried if ((num <= 0) *2) methods
    if ( positive.equals...) methods

    I just don't know how to do it.

  18. #18
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: For loop with scanner and strings

    What I want to know is how to tell if the string has been initialized twice.
    Without looking at any of your (pseudo)code --
    -- use a boolean flag, initially set to false
    -- at initialization, test the flag
    -- if false, set it to true
    -- if already true, it's the second initialization

    In case 'the String' has any one of a set of values, you can use a Set<String> implementation that supports the optional add(...) operation and test the boolean returned by the method to determine whether the Set already contained an equal element.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  19. #19
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: For loop with scanner and strings

    Quote Originally Posted by DarrylBurke View Post
    Without looking at any of your (pseudo)code --
    -- use a boolean flag, initially set to false
    -- at initialization, test the flag
    -- if false, set it to true
    -- if already true, it's the second initialization

    In case 'the String' has any one of a set of values, you can use a Set<String> implementation that supports the optional add(...) operation and test the boolean returned by the method to determine whether the Set already contained an equal element.

    db
    Good, I tried to set a boolean flag

    Java Code:
     boolean occursTwice = false;
    
                for (int i .....i++) 
                
                    int num = keyb.nextInt();
    
                    if (num <= 0)
                        System.out.println(incorrect);
                    else 
                    {
                        System.out.println(posInt);
                        
                        if (occursTwice = true)
                        {
                            break;
                        }    
                        
                        if (occursTwice = false)
                            occursTwice = true;                
                    }
                }
    This breaks when one positive integer has been inputted by the user. Any input or direction to help the code work would be great. It needs to break after two positive integers have been inputted.

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: For loop with scanner and strings

    Java Code:
    if(occursTwice = false)
    = assigns a value.
    Use == to compare the values of two operands
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 03-23-2011, 11:16 AM
  2. Replies: 13
    Last Post: 05-16-2010, 12:09 AM
  3. Scanner Loop
    By Phenomena in forum New To Java
    Replies: 2
    Last Post: 04-01-2010, 06:47 PM
  4. Problem with scanner in loop
    By grifan526 in forum New To Java
    Replies: 2
    Last Post: 07-21-2009, 12:06 AM
  5. Scanner-While Loop
    By hyunski in forum New To Java
    Replies: 2
    Last Post: 03-12-2009, 02:15 AM

Posting Permissions

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