christy_78
christy_78

Reputation: 53

Loop through String array to fill it with input from user

just a another noob to Java with a dumb question.
I am trying to create a function that receives a String array and fills it with text input from the user using Bufferedreader (which I currently want to use). I sort of have the idea in my head but it gives the error cannot find symbol when using the readline() property. How can I achieve this?

public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

public static void fill_array (String parray []){

        for(int i = 0; i < parray.length; i++){
            parray[i] = in.readline(); //Here it gives me the error
        }
    }

Upvotes: 1

Views: 64

Answers (2)

naveen
naveen

Reputation: 610

Hii Scanner is much more simpler than BufferedReader to read input, Let me give you an example :

import java.util.*;

public class Main
{
  public static void main(String arp[])
  {
    Scanner scanner = new Scanner(System.in);
    String address = scanner.nextLine(); // read string with spaces
    System.out.println("addres : "+ address);
    String name = scanner.next(); // read string without spaces
    System.out.println("name : "+ name);
    Integer age = scanner.nextInt();  // read Integer input
    System.out.println("age : "+ age);
  }
}

Scanner java api link : https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

Upvotes: 1

christy_78
christy_78

Reputation: 53

readLine() and not readline how embarrasing

Upvotes: 2

Related Questions