0

I have to use Scanner objects in different scopes as presented below:

public String fidentifier (String u)
{
    try {
        Scanner t = new Scanner( new File( "ubasic.dat") );
        //Some Statements
    } catch( FileNotFoundException e ){
            System.out.println( "Exception : " + e );
    }
}

public String didentifier(String cat)
{
    try {
        if( cat.equals("Government") )
                Scanner s = new Scanner( new File("ugov.dat") );
        else
              Scanner s = new Scanner( new File("uhc.dat") );
        //Some Statements
    } catch( FileNotFoundException e ) {
            System.out.println( "Exception : " + e );
    }
}

As I clearly declared Scanner objects in two different methods, I am still getting error pointing out that Scanner object declaration is not allowed in method didentifier().

Point me out where I'm wrong.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Ritwik Jamuar
  • 351
  • 1
  • 4
  • 17
  • 1
    Make a [mcve] and [ask], you can't have that error with what you posted... – Tunaki Apr 16 '16 at 17:59
  • You can't have that `if` statement with the two scanner things. SImply just do... `Scanner s;` and then inside the if statements do `s = new Scanner(...);` – 3kings Apr 16 '16 at 18:06
  • @Tunaki If you are asking for statements written after the comment `//Some Statements`, then these Scanner objects are reading lines from the file and the read line is then split with a delimiter to check for individual Strings. – Ritwik Jamuar Apr 16 '16 at 18:09
  • No. Read the page [mcve] carefully. – Tunaki Apr 16 '16 at 18:09
  • @3kings Thanks for the tip. That eradicated the error. – Ritwik Jamuar Apr 16 '16 at 18:14

2 Answers2

1

You can use and declare as much Scanners as you want...(it is a bad practice but there is not technical limitation for that...)

...Point me out where I'm wrong....

The error is: Scanner cannot be resolved to a variable The reason of the error is that you are trying to declare an object in a if else scope but no are using curly braces { }

replace the code in the method for this:

if (cat.equals("Government")) {
    Scanner s = new Scanner(new File("ugov.dat"));
} else {
    Scanner s = new Scanner(new File("uhc.dat"));
    // Some Statements
}

and everything will work fine...


at the end you can have one global Scanner object and you just can change the reference of the object.

public String didentifier(String cat)
{
    try
    {
        if( cat.equals("Government") )
              s = new Scanner( new File("ugov.dat") );
        else
              s = new Scanner( new File("uhc.dat") );
        //Some Statements
    }catch( FileNotFoundException e ) {
            System.out.println( "Exception : " + e );
    }
//your Return here...
}

enter image description here

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

The statement after an if has it's own scope. It doesn't make sense to declare a variable which doesn't exist on the next line as it is out of scope. The simplest thing to do is to use a variable or ?:

String file;
if( cat.equals("Government") )
     file = "ugov.dat";
else
     file = "uhc.dat";
Scanner s = new Scanner( new File(file) );

or

Scanner s = new Scanner( new File(cat.equals("Government") ? "ugov.dat": "uhc.dat" ) );
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130