Results 1 to 6 of 6
  1. #1
    localhost is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default user input array

    hello folks! new to java, little help will be much appreciated. This code should simply echo what the user input (array).

    Java Code:
    import java.util.*;
    public class ScanArray {
    
        public static void main(String[] args) {
            System.out.println("Enter Value");
            Scanner scanner = new Scanner (System.in);
            int[] num = new int [5];
                for (int i = 0; i < num.length; i++)
                {
                    num[i]=scanner.nextInt();
                }
                    System.out.println("You have Entered : " + num[i]);
        }
    
    }

  2. #2
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    You have to move the last println statement inside the for loop. This is because the scope of the int variable i is inside the loop. Try the below code:

    Java Code:
    import java.util.*;
    public class ScanArray {
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner (System.in);
            int[] num = new int [5];
                for (int i = 0; i < num.length; i++)
                {
                    System.out.print("Enter Value " + (i+1) + ": ");
                    num[i] = scanner.nextInt();
                    System.out.println("You have Entered : " + num[i]);
                }
                    
        }
    
    }
    --user0--

  3. #3
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    You can also echo the numbers after you have input all the numbers. You can use a for each loop as follows. This will echo all the numbers after user input is complete.

    Java Code:
    import java.util.*;
    public class ScanArray {
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner (System.in);
            int[] num = new int [5];
                for (int i = 0; i < num.length; i++)
                {
                    System.out.print("Enter Value " + (i+1) + ": ");
                    num[i] = scanner.nextInt();
                }
                
                for(int n: num)	
                {
                	System.out.println("You have entered: " + n);
                }
                    
        }
    
    }
    --user0--

  4. #4
    localhost is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    thanks for you help.. btw, how do you echo input value in one line?
    Output
    Enter Value 1: 11
    Enter Value 2: 22
    Enter Value 3: 33
    Enter Value 4: 44
    Enter Value 5: 55
    You have entered: 11
    You have entered: 22
    You have entered: 33
    You have entered: 44
    You have entered: 55
    BUILD SUCCESSFUL (total time: 10 seconds)

    Like this:

    You have entered: 11 22 33 44 55
    Last edited by localhost; 12-30-2010 at 03:19 AM.

  5. #5
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    If you want it in one line you need to break the last System.out.println statement into two System.out.print statements as follows:

    Java Code:
    import java.util.*;
    public class ScanArray {
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner (System.in);
            int[] num = new int [5];
                for (int i = 0; i < num.length; i++)
                {
                    System.out.print("Enter Value " + (i+1) + ": ");
                    num[i] = scanner.nextInt();
                }
                
                System.out.print("You have entered: ");
                
                for(int n: num)	
                {
                	System.out.print(n + " ");
                }
                    
        }
    
    }
    --user0--

  6. #6
    localhost is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Need help getting input(first/last name) from user
    By nightrise420 in forum New To Java
    Replies: 11
    Last Post: 09-11-2010, 03:09 AM
  2. Read user input into integer array
    By varunb in forum New To Java
    Replies: 12
    Last Post: 07-09-2010, 12:50 PM
  3. User Input
    By brmcdani in forum New To Java
    Replies: 2
    Last Post: 02-05-2010, 01:59 AM
  4. Replies: 5
    Last Post: 11-26-2009, 07:01 PM
  5. cant take input from user
    By new_1 in forum New To Java
    Replies: 6
    Last Post: 12-25-2007, 07:38 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
  •