try/catch..help needed soon as possible
dear all, i want a message sayin file does not exist..BEFORE it asks for the password.but wen i do that..it says variable yeet1 cannot be found..because the brackets are closing.im not that good in java.please help me.my assignment is due in 3 days.thanx alot
Code java:
import java.util.Scanner;
import javax.swing.*;
import java.io.*;
public class BackItUpTxt
{
public static void main(String agrs[]) throws IOException
{
String D_Play = "File does not exist!";
try
{
Scanner yeet1=new Scanner(System.in);
System.out.println("Enter file that you want to backup:");
yeet2=yeet1.nextLine();
}
catch(FileNotFoundException e){
JOptionPane.showMessageDialog (null,D_Play);
System.out.println("Enter password to secure your backup:");
String yeet3=yeet1.nextLine();
for (int count =6; count > 0 && yeet3 != "acbt"; count--)
{
System.out.println("Wrong password. " + (count-1) + " attempts left.Enter password again:");
yeet3=yeet1.nextLine();
}
Encrip.encrip(yeet3,yeet2);
Backup.backup(yeet2);
System.exit(-1);
}
}
}
Re: try/catch..help needed soon as possible
Ok, I'm confused with your code. Why do you ask for the password to the backup when FileNotFound Exception is thrown? Also, the reason yeet1 cant be found is because of scope.
Code java:
Scanner yeet1=new Scanner(System.in);
This is in your try statement. However, you are trying to use this variable in your catch statement:
Code java:
String yeet3=yeet1.nextLine();
...
yeet3=yeet1.nextLine();
That cannot be done. Either you need to declare yeet1 before the try/catch statement (and you can still initialize it inside the try statement), or you have to recreate the yeet1 variable inside the catch statement.