Explain please

import java.io.*;

public class Chooser{

private static String students[][] = new String[5][2];

private static int row = 0;

public static void main(String args[]){

try{

BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter your name: ");

String s = input.readLine();

System.out.print(students[0][0]);

}catch(Exception e){}

}

//This compiles ok.

import java.io.*;

public class Chooser{

private static String students[][] = new String[5][2];

private static int row = 0;

public static void main(String args[]){

try{

BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

}catch(Exception e){}

System.out.print("Enter your name: ");

String s = input.readLine();

System.out.print(students[0][0]);

}

//This one does not.

Chooser.java:13: cannot resolve symbol

symbol : variable input

location: class Chooser

String s = input.readLine();

^

1 error

> Execution finished.

Why ?

[1180 byte] By [Itachi_Uchiha_Go] at [2007-9-30 17:55:17]
# 1

Your input variable is declared inside try block , so out side cannot access it . you can declare input out side of the try scope. Somhow I change the your code to work. See this what you want ?

import java.io.*;

public class Chooser{

private static String students[][] = new String[5][2];

private static int row = 0;

public static void main(String args[]){

BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter your name: ");

try{

String s = input.readLine();

students[0][0]=s;

}catch(IOException e)

{

e.printStackTrace();

}

System.out.println(students[0][0]);

}

}

jayalalk at 2007-7-6 14:31:26 > top of Java-index,Administration Tools,Sun Connection...
# 2

import java.io.*;

public class Chooser{

private static String students[][] = new String[5][2];

private static int row = 0;

public static void main(String args[]){

BufferedReader input = null;

try{

input = new BufferedReader(new InputStreamReader(System.in));

}catch(Exception e){}

System.out.print("Enter your name: ");

String s = input.readLine();

System.out.print(students[0][0]);

}

}

The problem was...

input was local to the try-catch block.

in this code i have made it accessable both inside and outside this block.

note: readLIne() should be in Try-Catch block. or your function should be:

"public static void main(String args[]) throws Exception"

kapilChhabra

kapilChhabra at 2007-7-6 14:31:26 > top of Java-index,Administration Tools,Sun Connection...