JavaPilgrim
JavaPilgrim

Reputation: 77

Why can Java not find symbol

I want to take five names from the user, and collect them in a text file, have written this:

import java.io.*;
import java.util.*;
class writ {
    public static void main (String[] args) {
        Scanner sc=new Scanner(System.in);
        FileWriter fw= new FileWriter("namelist.txt");
        BufferedWriter bw= new BufferedWriter(fw);
        PrintWriter pw=new PrintWriter(bw);
        String[] names=String[5];
        Arrays.fill(names,"");
        System.out.println("Enter the names");
        for(int i=0; i<5; i++){
            names[i]=sc.nextLine();
            pw.println(names[i]);

        }
        pw.close();

}
}

It gives me an error. Says "cannot find symbol" at line 9. Why??

Upvotes: 0

Views: 131

Answers (1)

Aniruddh Dikhit
Aniruddh Dikhit

Reputation: 682

You need to call new for the array of String something like below

    String[] names= new String[5];

Upvotes: 7

Related Questions