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 4 of 4

Thread: How to loop it to where the user may keep inserting links until they insert, "end."

  1. #1
    Member
    Join Date
    Aug 2020
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Post How to loop it to where the user may keep inserting links until they insert, "end."

    I'm messing around with different libraries in Java and decided to make a program that redirects the user to the link they input into the program. It works but works only once. I would like for the user to keep inserting as many links as they'd like, one at a time, and have the program only end when they insert the keyword, "end"

    What I have thus far:
    (In the code you don't see where I try to have it loop because I'm unsure of how to start, I thought about using a String for the keyword, "end" then implementing an if statement where should they enter "end" it'll then stop but I was unable to have it work)
    import java.awt.Desktop;
    import java.net.URI;
    import java.util.Scanner;
     
    public class ProgramURLLaucher {
     
    	public static void main(String[] args) throws Exception {
     
    		System.out.println("Input URL:");
    		Scanner scan =  new Scanner(System.in);
    		String link = scan.next();
    		Desktop userDesktop = Desktop.getDesktop();
    		userDesktop.browse(new URI(link));
     
    	}//end main
     
    }//end class

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to loop it to where the user may keep inserting links until they insert, "end."

    Where is the loop and the test for end?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Aug 2020
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: How to loop it to where the user may keep inserting links until they insert, "end."

    There is no loop in the code because that is part of my question. I don't know where to insert the loop and what parameters to use for it.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to loop it to where the user may keep inserting links until they insert, "end."

    Ok, what statements need to be inside the loop?
    Then what keeps the code executing the loop and when will the code be able to exit the loop?

    begin loop
    ask question
    get user response
    exit loop if user said to exit
    do some stuff
    end loop

    Have you seen the tutorial on loops: https://docs.oracle.com/javase/tutor...lts/while.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: August 20th, 2019, 07:07 AM
  2. Replies: 4
    Last Post: July 18th, 2014, 02:04 AM
  3. Replies: 2
    Last Post: May 22nd, 2014, 01:17 PM
  4. Trying to get an "if" statement to work in a "for loop".
    By JAKATAK in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 1st, 2013, 11:16 PM
  5. Replies: 1
    Last Post: January 15th, 2010, 01:32 AM