Results 1 to 4 of 4

Thread: User input

  1. #1
    iLearnJava is offline Member
    Join Date
    Dec 2014
    Posts
    3
    Rep Power
    0

    Default User input

    Java Code:
    import java.util.Scanner;
    
    public class LearningJava {
    	public static void main(String[] args) {
    		Scanner Input = new Scanner(System.in);
    		System.out.println("How many words do you want to enter?");
    		int howManyWords = Input.nextInt();
    		String[] texts = new String[howManyWords];
    		System.out.println("Enter " + howManyWords + " words");
    		for(int i = 0; i < texts.length; i++) {
    			texts[i] = Input.nextLine();
    		}
    		for(String word : texts) {
    			System.out.println(word);
    		}
    	}
    }
    The user types how many words he want to enter and then he types the words but it lets him type only -1 the words he wanted to enter.
    For example I wanted to enter 5 words, it only lets me enter 4.

    If I add +1 to text.length
    Java Code:
    for(int i = 0; i < texts.length + 1; i++)
    or change to <=
    Java Code:
    for(int i = 0; i <= texts.length; i++)
    I get ArrayIndexOutOfBoundsException.
    Last edited by iLearnJava; 12-06-2014 at 08:07 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: User input

    The nextInt() method does exactly what its name suggests: it reads an int and not the 'enter' key that follows it, so a nextLine() method will read an empty line. To get rid of the 'enter' in the input, add a 'nextLine()' call after line #7.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    iLearnJava is offline Member
    Join Date
    Dec 2014
    Posts
    3
    Rep Power
    0

    Default Re: User input

    I don't understand how does that fix the problem.
    I thought the problem is with the 1st for loop.
    Here is what I entered and the output in case you didn't understand the question (just to be sure :))
    User input-xf2zcrz.png

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: User input

    If you don't believe me, put the following line before line #11:

    Java Code:
    System.out.println("About to input line #"+i);
    ... and this line after line #11:

    Java Code:
    System.out.println("You just put input line #"+texts[i]);
    ... and see for yourself.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Asking user for input twice
    By csanch11 in forum New To Java
    Replies: 4
    Last Post: 04-19-2014, 08:54 AM
  2. User Input???
    By jonytek in forum New To Java
    Replies: 8
    Last Post: 01-13-2013, 02:52 PM
  3. User input
    By the ole buc in forum New To Java
    Replies: 16
    Last Post: 12-11-2011, 07:08 PM
  4. 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
  5. cant take input from user
    By new_1 in forum New To Java
    Replies: 6
    Last Post: 12-25-2007, 07:38 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •