'variable not initialized in different case

//(old)Hello so I'm writing a program that ask for an option from the user and run the methods created for each one there are 2 input files "input1.txt" and "input2.txt" that is being created but I'm not going to include them here because they take up too much space it's not relevant to the question. so I've only worked up to option 2 so far but when I tested it on hackerank something is not right.

(new)below is my code(not complete code)

update:Hello below i'll post my full revised code

import static java.lang.System.*;
import java.io.FileNotFoundException;
import java.util.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.ArrayList;

public class Main
{
  public static void main (String[]args) throws IOException
  {
    PrintWriter pw1 = new PrintWriter ("input.txt");
      pw1.println ("Mickey Mouse CS 98.7 67.8 23.5");
      pw1.println ("Minnie Mouse ENG 45.6 98.3 94.7");
      pw1.println ("Donald Duck NET 56.8 74.2 78.4");
      pw1.println ("Bob Builder CS 78.5 89.4 82.5");
      pw1.println ("Snow White MAT 56.6 32.4 56.6");
      pw1.println ("Hellen Keller CHEM 78.8 23.1 99.6");
      pw1.println ("Daffy Duck ENG 67.4 55.5 89.5");
      pw1.println ("Fred Flinstone MAT 45.3 87.4 38.9");
      pw1.println ("Daffy Duck CS 76.5 22.2 88.5");
      pw1.println ("Bugs Bunny NET 68.4 89.7 95.6");
      pw1.println ("Winnie Pooh CHEM 77.5 89.4 98.2");
      pw1.close ();
    PrintWriter pw2 = new PrintWriter ("input2.txt");
      pw2.println ("Scrooge McDuck ACC 78.7 77.3 63.5");
      pw2.println ("Woody Woodbecker CS 65.6 78.3 84.7");
      pw2.println ("Scooby Doo MAT 56.8 78.2 88.4");
      pw2.println ("Spider Man CHEM 58.5 99.3 92.5");
      pw2.println ("Roger rabbit NET 66.9 39.6 86.6");
      pw2.println ("Wonder Woman CHEM 68.4 83.1 69.6");
      pw2.println ("Jane Jetson ENG 77.4 85.5 69.5");
      pw2.close ();


      options ();
    Scanner k = new Scanner (System.in);
    int choice = k.nextInt ();
k.nextLine();


    /*while (choice==1){

       ArrayList<student> student=uploadStudents("input.txt");
       //System.out.println(student);//[student@49e4cb85, student@2133c8f8, .....
       //bunch of reference?
       System.out.println("***Data uploaded successfully.\n");              
       options();

       choice=k.nextInt();

       }

       while (choice==2){

       printStudentData(in);    

       options();

       choice=k.nextInt();   
       } */

    while (choice != 6)
      {
    File in;
    switch (choice)
      {

      case 1:
        System.out.printf("What is the name of the file you want to upload data from?\n");
       
        String filename = k.nextLine ();
        switch (filename)
          {

          case "input.txt":
        in = new File (filename);
        java.util.ArrayList < student > student = uploadStudents (in);
        //System.out.println(student);//[student@49e4cb85, student@2133c8f8, .....
        //bunch of reference?
        System.out.println ("***Data uploaded successfully.\n");
        options ();
        choice = k.nextInt ();
                k.nextLine ();
        while(choice==2){
       
         printStudentData(in);    // if you don't ask again it will infinitely print;
         System.out.println("");
         
         options();
        
        choice=k.nextInt();
        k.nextLine();
        }
            
    break;

        case "input2.txt":
            in = new File (filename);
        java.util.ArrayList < student > student2 = uploadStudents (in);
        System.out.println ("***Data uploaded successfully.\n");
        options ();
        choice = k.nextInt ();
        k.nextLine();
        while(choice==2){
       
         printStudentData(in);    // if you don't ask again it will infinitely print;
         System.out.println("");
         
         options();
        
        choice=k.nextInt();
        k.nextLine();
        }    
        break;
          }
        break;
        
        case 2:
            out.printf ("no data found");   // if you don't ask again it will infinitely print;
          System.out.println ("");

          options ();

          choice = k.nextInt ();
                k.nextLine ();
          break;

        case 6:
            
            break;
default:
out.println("no data found");

      }
      }
  }

  public static void options ()
  {             //
    System.out.
      println
      ("Welcome to the Student Analyzer program. Please choose from the following options:");
    System.out.println ("1. Upload data");
    System.out.println ("2. View data");
    System.out.println ("3. Find Major");
    System.out.println ("4. Create statistics File");
    System.out.println ("5. Print Statistics File");
    System.out.println ("6. Exit the program");
  }             //


  public static ArrayList < student > uploadStudents (File inputfilename) throws IOException    ///
  {
    java.util.ArrayList < student > student = new java.util.ArrayList <> ();



    Scanner sc = new Scanner (inputfilename);

    while (sc.hasNext ())
      {
    //new student object??
    student s =
      new student (sc.next (), sc.next (), sc.next (), sc.nextFloat (),
               sc.nextFloat (), sc.nextFloat ());
      student.add (s);
      }



    return student;

  }             /// 

  public static void printStudentData (File inputfile) throws IOException
  {

    Scanner o = new Scanner (inputfile);
    while (o.hasNext ())
        System.out.
    printf ("Student: %s %s, Major: %s, Exam Grades: %.1f, %.1f, %.1f%n",
        o.next (), o.next (), o.next (), o.nextFloat (),
        o.nextFloat (), o.nextFloat ());
      o.close ();
  }



}


I was able to make it work by using switch statement in side the case however it's really unfortunate that I wasn't able to modify the code and make it functional; I tried initialize the file outsiede of the switch and while loop like this 'File in=new File("?"); but it simply returns Exception in thread "main" java.io.FileNotFoundException: (No such file or directory)

////(old)so basically when I run it on hackerank it says

student.java:104: error: variable in might not have been initialized printStudentData(in); ^ 1 error

how do rewrite the program so that the case 2 will use the file created in case 1 ?////



Solution 1:[1]

You would better use a default value for in before starting while . after that if you initialize in the case 1 you can use it in the case 2.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Habib