Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: How do I loop this if statement?

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How do I loop this if statement?

    How do I loop this if statement from my program?


    if (fileNames.contains(".txt")) {

    System.out.println("Text File:" + fileNames);
    fileNames = scan.nextLine();
    }

    else {

    System.out.println("");
    fileNames = scan.nextLine();

    }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,354
    Thanks
    182
    Thanked 832 Times in 775 Posts
    Blog Entries
    5

    Default Re: How do I loop this if statement?


  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    21
    My Mood
    Confused
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: How do I loop this if statement?

    I think you need to add a for loop and how many times does it need to loop, what's the purpose of the code?

    Try putting the if/else statement between this:

    for (int i=?; i<? ;i++)
    {
    }

    Question marks depend on number of times you want it to loop.
    int i probably equals 0.

    Sorry if this does not help I am new to this too.

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: How do I loop this if statement?

    How do I loop this if statement from my program?
    What exactly you want to do?
    We can't just read minds. Though we can assume but you not assumptions will never always lead you to the solution. So ask a proper question and get proper answer. Well, if you don't really know the loops, see Reply # 02 to your thread and you will get your answer.

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    I suggest you to take a look to while loop. It makes possible to iterate code while a statement is true (for example while your file has next lines to be read).

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,354
    Thanks
    182
    Thanked 832 Times in 775 Posts
    Blog Entries
    5

    Default Re: How do I loop this if statement?

    Quote Originally Posted by felimblack View Post
    I suggest you to take a look to while loop. It makes possible to iterate code while a statement is true (for example while your file has next lines to be read).
    Given we do not know what the original poster truly needs due to lack of information, I can't see why a while loop would be better than any other loop. Lack of information shouldn't necessitate guessing.

Similar Threads

  1. Help with Loop Structures, If-else statement, and String
    By javabeg123 in forum Loops & Control Statements
    Replies: 2
    Last Post: December 6th, 2011, 10:01 PM
  2. Do you add objects to an array when using switch statement with a For loop?
    By TheWhopper858 in forum Collections and Generics
    Replies: 2
    Last Post: November 13th, 2011, 01:28 PM
  3. Switch statement inside a for loop need help.
    By TheWhopper858 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 12th, 2011, 11:50 PM
  4. Nested loop if statement help
    By CSUTD in forum Loops & Control Statements
    Replies: 7
    Last Post: November 8th, 2011, 02:05 PM
  5. How do I loop a switch Statement?
    By Arkeshen in forum Java Theory & Questions
    Replies: 7
    Last Post: July 19th, 2011, 07:25 AM