Punya Munasinghe
Punya Munasinghe

Reputation: 285

How to put keyboard input values into an array using a loop?

Here I was trying to get two keyboard inputs from the user into a single array index position.

 /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package tour;

    import java.util.Scanner;
    import tour.City;

    /**
     *
     * @author dp
     */
    public class Tour {

        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here

            City[] city = new City[9];

            Scanner in = new Scanner(System.in);

            for(int i=0;i<city.length;i++)
            {
                int no = in.nextInt();
                String name = in.nextLine();

                city[i]= new City(no,name);
            }
        }

    }

When I run this code it'll give me the following exception.enter image description here

I'm very new to java and don't know how to solve this.

Upvotes: 1

Views: 3318

Answers (3)

Karam Jabareen
Karam Jabareen

Reputation: 301

just need to read the int to the last of line

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tour;

import java.util.Scanner;
import tour.City;

/**
 *
 * @author dp
 */
public class Tour {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        City[] city = new City[9];

        Scanner in = new Scanner(System.in);

        for(int i=0;i<city.length;i++)
        {
            int no = in.nextInt();
            in.nextLine();//read to the end of line 
            String name = in.nextLine();

            city[i]= new City(no,name);
        }
    }

}

Upvotes: 0

Darshan Mehta
Darshan Mehta

Reputation: 30809

You are using nextInt() and nextLine() methods to read user inputs, these methods read the next available token, so this is how the existing code is working:

  • It reads a number using nextInt() and assigns it to no
  • User then hits return and control reads an empty line (as next line is empty) and assigns it to name
  • City object gets created with 12 as no and <empty_string> as name. For loop starts it's second execution.
  • By this time, user has typed NY and hits return
  • As it expects the token to be an int (by calling nextInt()), it fails and throws an Exception.

If you want the control to read the two inputs separately (and wait until user hits return), use:

int no = Integer.parseInt(in.next());
String name = in.next();

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726489

Since 12 and NY are on different lines, when you do

String name = in.nextLine();

the String that you get back is empty. This is because the Scanner's "reading point" is positioned after 12, but before the end-of-line marker that follows it.

You can fix this by adding another nextLine, and dropping its result:

in.nextLine(); // Skip to end-of-line after the number
String name = in.nextLine();

Upvotes: 3

Related Questions