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

Thread: I get an error when the computer tries to read the String in a loop

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post I get an error when the computer tries to read the String in a loop

    public static void main(String [] args)
    {
    FilmData MyFilm[] = new FilmData[4];
    Scanner input = new Scanner(System.in);
    int count=0;
    for (int i=0; i<MyFilm.length; i++)
    {
    count=i+1;
    System.out.println("Details for Movie"+count);
    System.out.println("---------------------");
    System.out.println("Type the movie name");
    String movie=input.nextLine(); // Get error in the 2nd loop when data is input
    // I think this is an error in Java. But is there a way to handle this?
    System.out.println("Type the length in minutes");
    int length=input.nextInt();
    MyFilm[i] = new FilmData(movie,length);
    }
    }

  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Wink I get an error when the computer tries to read the String in a loop.Please help

    import java.util.Scanner;
    class test{
    public static void main(String [] args) 
            {
            String film[] = new String[4];
            int length[] = new int[4];
            Scanner input = new Scanner(System.in);
            int count=0;
            for (int i=0; i<film.length; i++) 
            {
            count=i+1;
            System.out.println("Details for Movie"+count);
            System.out.println("---------------------");
            System.out.println("Type the movie name");
            film[i]=input.nextLine();
            System.out.println("Type the length in minutes");
            length[i]=input.nextInt();
            // If I add input.nextLine here the code works. Why is that?
     
    }
            }
            }
    Last edited by anandck; March 30th, 2020 at 05:43 AM.

  3. #3
    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: I get an error when the computer tries to read the String in a loop

    Please copy the full text of the error message and paste it here. It has important info about the error.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Jun 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I get an error when the computer tries to read the String in a loop

    Thanks Sir
    Is this a Java compiler error? As I have faced this problem many times.

  5. #5
    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: I get an error when the computer tries to read the String in a loop

    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Mar 2020
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I get an error when the computer tries to read the String in a loop

    I see the issue you're having. This is because input.nextInt() does not read the newline character generated when you hit the return key. You can fix this by changing the last line to:

    length[i]=input.nextInt();
    input.nextLine();

    This will read the newline character and then proceed with your application as expected.

Similar Threads

  1. Replies: 3
    Last Post: June 26th, 2013, 02:12 AM
  2. How to read a string into an array?
    By javaiscool in forum Java Theory & Questions
    Replies: 5
    Last Post: March 26th, 2013, 08:36 PM
  3. Replies: 3
    Last Post: March 23rd, 2013, 07:20 PM
  4. Replies: 6
    Last Post: March 1st, 2013, 02:06 PM
  5. [SOLVED] Read double from console without having to read a string and converting it to double.
    By Lord Voldemort in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 26th, 2011, 08:08 AM

Tags for this Thread