Results 1 to 6 of 6
  1. #1
    popeye is offline Member
    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Default Real new guy stuck using user input in for loop

    Im trying to self train. Taking a self paced online course.

    Cant "see" how to use the user input in my for statement.

    The problem

    "Can you complete the exercise below such that it prints the sum of 1 to n numbers. The int n is taken as input from the user. Use a for loop to complete this exercise.

    You will need a variable where you can store the new sum from each iteration. We have provided a variable called sum of the type int, for this purpose."

    Java Code:
               for (int count=1; count <= 10; count++) {  
                    System.out.println("the value of count is " + count);  
                    sum += count;  
                    System.out.println("the value of sum is " + sum);  
              }   
                System.out.println(sum);  
            }
    Thanks for help or direction to a resource that explains how to refer to user input.

  2. #2
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    165
    Rep Power
    2

    Default Re: Real new guy stuck using user input in for loop

    To do user input, you can use the Scanner class. Here is a link to the JavaDoc for the Scanner class, Scanner (Java Platform SE 7 )

  3. #3
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Real new guy stuck using user input in for loop

    The basics:

    Java Code:
    Scanner scan = new Scanner( System.in );
    Creates a scanner object named "scan"

    Java Code:
    String input = scan.nextLine();
    Asks for input from user, which will be read as a string and saved to the string "input"

    Java Code:
    int input = scan.nextInt();
    Will ask for input and read it as an int. The program won't be happy(it'll throw an exception) if you try to input characters or floats)

    Java Code:
    double input = scan.nextDouble();
    Reads input as a double

  4. #4
    popeye is offline Member
    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Default Re: Real new guy stuck using user input in for loop

    I guess I should have posted the entire class.

    Java Code:
    
    import java.util.Scanner;
    
    class SumOfNumbers {
    
        public static void main(String args[]) {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Enter an int from 5 to 100: ");
            int n = keyboard.nextInt();
            int sum = 0;
            
            // my for loop 
            for (int count=1; count <= 10; count++) {
    // Id write down all the numbers one per line
                System.out.println("the value of count is " + count);
    // then Id add the first number to sum and continue to do so until I reached the last number.
                sum += count;
                System.out.println("the value of sum is " + sum);
    } 
            System.out.println(sum);
        }    
    }
    The problem Im having, is using the user input instead of the constant count <= 10;. I looked back in the course material four times, Im headed back for a fifth and dont see where this is explained.

    Im willing to try and figure it out, just dont know where to go for an explanation and/or examples (ofcourse Ive been looking on the web).

    Thanks in advance !

  5. #5
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    165
    Rep Power
    2

    Default Re: Real new guy stuck using user input in for loop

    Quote Originally Posted by popeye View Post
    I guess I should have posted the entire class.

    Java Code:
    
    import java.util.Scanner;
    
    class SumOfNumbers {
    
        public static void main(String args[]) {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Enter an int from 5 to 100: ");
            int n = keyboard.nextInt();
            int sum = 0;
            
            // my for loop 
            for (int count=1; count <= 10; count++) {
    // Id write down all the numbers one per line
                System.out.println("the value of count is " + count);
    // then Id add the first number to sum and continue to do so until I reached the last number.
                sum += count;
                System.out.println("the value of sum is " + sum);
    } 
            System.out.println(sum);
        }    
    }
    The problem Im having, is using the user input instead of the constant count <= 10;. I looked back in the course material four times, Im headed back for a fifth and dont see where this is explained.

    Im willing to try and figure it out, just dont know where to go for an explanation and/or examples (ofcourse Ive been looking on the web).

    Thanks in advance !
    It's really quite simple. In you for loop just replace the 10 with n. That should solve your problems!

  6. #6
    popeye is offline Member
    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Default Re: Real new guy stuck using user input in for loop

    Yeah .. thats what I thought .. to do that exactly .. but I guess maybe my {} were messed up. I was getting n already defined in main error.

    It is working now ... chaulk anudder one up to the dang rookies !!! Thank you !!

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. Replies: 26
    Last Post: 05-10-2011, 12:31 AM
  3. Replies: 5
    Last Post: 11-27-2010, 03:49 AM
  4. [SOLVED] User Input - loop
    By new person in forum New To Java
    Replies: 4
    Last Post: 02-22-2009, 10:02 PM
  5. loop when there is no user-input
    By becky in forum New To Java
    Replies: 12
    Last Post: 02-02-2009, 10:02 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
  •