Results 1 to 4 of 4
  1. #1
    Kanada is offline Member
    Join Date
    Mar 2017
    Posts
    14
    Rep Power
    0

    Default Question on how to get user to input a start value and end to run a simple loop

    I was writing a simple loop trying to automate something
    and I'm not sure how to set it up as an automation.
    My code:

    Java Code:
     
    	public static void main(String[] args) {
    		for (int i = 1; i < 5; i++) {
    
    		    System.out.println("image." + i);
    		}
    What I want to do is have it ask the user to input the initial "i"
    and also ask the user which number to stop at.
    then it prints out my image number.

    I want to achieve this where I can run the java file without going in and changing the inside
    contents of the code everytime. Sort of like a program.
    Last edited by Kanada; 03-10-2017 at 11:18 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Question on how to get user to input a start value and end to run a simple loop

    There are several ways to do it.

    1. Pass two arguments via the args variable in main. Convert to integers using Integer class method.

    Java Code:
    int v = Integer.parseInt(args[0]);
    2. Use the Scanner class to read input form the console.

    Java Code:
    Scanner input = new Scanner(System.in);
    int v = input.nextInt();
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Kanada is offline Member
    Join Date
    Mar 2017
    Posts
    14
    Rep Power
    0

    Default Re: Question on how to get user to input a start value and end to run a simple loop

    I'm actually nearly a complete newbie and I'm not sure what all this means.
    So I'm trying the scanner class thing and I
    import java.util.Scanner; at the very top of the code, right?
    How do I use
    Java Code:
    Scanner input = new Scanner(System.in);
    int v = input.nextInt();
    Where does v come in?
    I put it in my code right above my loop and I tried replacing it with i but it doesn't work?
    Is this meant to ask me for input in the compiler bit?
    I have no idea what I'm doing...

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Question on how to get user to input a start value and end to run a simple loop

    If you don't know what v is then you need to take a course or read a book or look at a tutorial. The folks on this forum
    are more than happy to help you debug your program. But it is not really a forum for learning how to program in Java.
    You may want to search the forum for threads which discuss various tutorial outlets. I prefer books but I understand there
    are some good tutorials on YouTube.

    But to answer your question, v is a primitive of type integer. It can be used as either the start of end of your for loop values.

    Java Code:
    int v = input.nextInt();
    int x = input.nextInt();
    for (int i = v; i <= x; i++) {
       // do something
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. having issue with user input and using while loop
    By barqcider in forum New To Java
    Replies: 4
    Last Post: 10-09-2012, 01:06 AM
  2. Simple user input not working
    By wdh321 in forum New To Java
    Replies: 9
    Last Post: 05-01-2012, 09:45 AM
  3. [SOLVED] User Input - loop
    By new person in forum New To Java
    Replies: 4
    Last Post: 02-22-2009, 10:02 PM
  4. loop when there is no user-input
    By becky in forum New To Java
    Replies: 12
    Last Post: 02-02-2009, 10:02 PM
  5. Scanner Issues (User Input: Very Simple)
    By carlodelmundo in forum New To Java
    Replies: 8
    Last Post: 10-31-2008, 02:44 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
  •