Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have this code it works and it open the PDF file but it opens only one file li

ID: 3604997 • Letter: I

Question

I have this code it works and it open the PDF file but it opens only one file like it does not go to the other one if I type the other PDF name I made IF statement but I need help with that to make the coices work so I will be having a choice what to open.

Thanks

import java.io.File;
import java.awt.Desktop;
import java.util.Scanner;


public class PDF {
public static void main (String args[])throws Exception
{
  
  
System.out.println("Enter the PDF file Name");

  
try{
int number1 =1;
int number2 =2;
int number3 =3;
Scanner number = new Scanner(System.in);
String name = number.nextLine();
System.out.println(name);
  
if (number1 ==1)
{
  
Desktop.getDesktop().open(new File("C:\Test.pdf"));
}
else if (number2 ==2)
  
Desktop.getDesktop().open(new File("C:\Test1.pdf"));
  
else if (number3 ==3){
  
Desktop.getDesktop().open(new File("C:\Tes3.pdf"));
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}

Explanation / Answer

The variables number1 number2 number3 are not REQUIRED.
You just need to match the input string in the if statement. If the name matches with the input string, the corresponding file is opened. We use the equals method.


The output screens have been provided.

If you have any doubts kindly you can comment. I'll reply as soon as possible.
Kindly rate the answer.All the best. :)
*********************************************************************
Code:

import java.io.File;
import java.awt.Desktop;
import java.util.Scanner;

public class PDF {
   public static void main(String args[]) throws Exception {

       System.out.println("Enter the PDF file Name");

       try {
           //THESE VARIABLES ARE NOT REQUIRED
           /*int number1 = 1;
           int number2 = 2;
           int number3 = 3;*/
           Scanner number = new Scanner(System.in);
           String name = number.nextLine();
           System.out.println(name);

           if (name.equals("Test.pdf")) {

               Desktop.getDesktop().open(new File("C:\Test.pdf"));
           } else if (name.equals("Test1.pdf"))

               Desktop.getDesktop().open(new File("C:\Test1.pdf"));

           else if (name.equals("Tes3.pdf")) {

               Desktop.getDesktop().open(new File("C:\Tes3.pdf"));
           }
       } catch (Exception e) {
           System.out.println(e);
       }
   }
}

*********************************************************************
Sample output 1:

[Test1.pdf gets opened. User input is Test1.pdf]

***********************************************************************

I hope this helps you.
If you find my answer helpful,
Kindly rate the answer.
All the best :)