473,461 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to try catch this?

5
dear all, i want to try catch this. i want a message to appear before it asks for the password..please help me out..

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. import javax.swing.*;
  4.  
  5. import java.io.*;
  6.  
  7. public class BackItUpTxt
  8. {
  9.     public static void main(String agrs[]) throws IOException
  10.  
  11.     {
  12.         String D_Play = "File does not exist!";
  13.         try
  14.         {
  15.         Scanner yeet1=new Scanner(System.in);
  16.  
  17.         System.out.println("Enter file that you want to backup:");
  18.  
  19.         String yeet2=yeet1.nextLine();
  20.  
  21.  
  22.  
  23.         System.out.println("Enter password to secure your backup:");
  24.  
  25.         String yeet3=yeet1.nextLine();
  26.  
  27.  
  28.             for (int count =6; count > 0 && yeet3 != "acbt"; count--)
  29.             {
  30.             System.out.println("Wrong password. " + (count-1) + " attempts left.Enter password again:");
  31.  
  32.             yeet3=yeet1.nextLine();
  33.         }
  34.  
  35.  
  36.         Encrip.encrip(yeet3,yeet2);
  37.  
  38.         Backup.backup(yeet2);
  39.    }
  40.  catch(FileNotFoundException e){
  41.         JOptionPane.showMessageDialog (null,D_Play);
  42.  
  43.         System.exit(-1);
  44.     }
  45.  
  46. }
  47. }
  48.  
  49.  
Sep 18 '10 #1
3 1542
Dheeraj Joshi
1,123 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. Scanner yeet1=new Scanner(System.in);
  2. System.out.println("Enter file that you want to backup:");
  3. String yeet2=yeet1.nextLine();
  4.  
Wrap this code in try catch.
Take
Expand|Select|Wrap|Line Numbers
  1. String yeet2 = null;
  2.  
If nothing return or if you catch exception do not proceed to except the password.

Regards
Dheeraj Joshi
Sep 19 '10 #2
safra
5
i dont exactly understand what you meant.but i did try it out the way i understood.it still dint do what i expected it 2 do.
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. import javax.swing.*;
  4.  
  5. import java.io.*;
  6.  
  7. public class BackItUpTxt
  8. {
  9.     public static void main(String agrs[]) throws IOException
  10.  
  11.     {
  12.         String D_Play = "File does not exist!";
  13.         try
  14.         {
  15.         Scanner yeet1=new Scanner(System.in);
  16.  
  17.         System.out.println("Enter file that you want to backup:");
  18.         String yeet2 = null;
  19.         yeet2=yeet1.nextLine();
  20.   }
  21.  catch(FileNotFoundException e){
  22.         JOptionPane.showMessageDialog (null,D_Play);
  23.  
  24.         System.out.println("Enter password to secure your backup:");
  25.  
  26.         String yeet3=yeet1.nextLine();
  27.  
when i do this.it says you cant find the variable yeet1..how do i try catch this?im not so good in java.and my assignment is due in 3 days.please help me out soon as possible.thanx alot
Sep 19 '10 #3
Dheeraj Joshi
1,123 Expert 1GB
Try something like this

Expand|Select|Wrap|Line Numbers
  1. Scanner yeet1=new Scanner(System.in);
  2. String yeet2 = null;
  3. String yeet3 = null;
  4. try{
  5.     System.out.println("Enter file that you want to backup:");
  6.     yeet2=yeet1.nextLine();
  7.     if(null == yeet2){
  8.         System.out.println("File name missing");
  9.         System.Exit(0);
  10.     }
  11.     System.out.println("Enter password to secure your backup:");
  12.     yeet3=yeet1.nextLine();
  13.     if(null == yeet3){
  14.         System.out.println("Password missing");
  15.         System.Exit(0);
  16.     }
  17.  
  18.     // Rest of the code
  19. }
  20. catch(FileNotFoundException e){
  21.     //Check type of exception
  22.     //Hadle it accordingly
  23. }
Regards
Dheeraj Joshi
Sep 20 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: D. Alvarado | last post by:
Hi, Basically, I want to run an action every time I detect a new file appears in a particular directory. I realize I could poll the directory every 5-10 seconds or so, but I was hoping there was...
2
by: Brian | last post by:
I am new to lint and have been experimenting with splint. An initialization problem I was caught on recently was not found by splint. Would pc-lint catch this problem: /* test program for splint...
6
by: ChrisB | last post by:
Hello All: I notice that when using try/catch blocks in C#, variables declared in the try block go out of scope in the finally block. So, for example, the following code generates a compiler...
3
by: will | last post by:
Hi all. I've got an question about how to catch an exception. In Page_Load, I place a DataGrid, dg1, into edit mode. This will call the method called GenericGridEvent. GenericGridEvent will call...
0
by: Zorpiedoman | last post by:
This is going to be hard to explain, but here it goes... I have a class (usercontrol, actually) that has a property called "Value" of type Object. It is set up to be bindable, and in the SET...
2
by: Mardy | last post by:
Hi all, I have written a slightly modified version of the CGIHTTPServer; the difference with the module included in the standard python distrubution is that, since I know that the CGI to be...
3
by: Howard | last post by:
A non-integer value is entered as student id /grades.aspx?studentid=1283128sdf Input string was not in a correct format. Description: An unhandled exception occurred during the execution of the...
22
by: Amali | last post by:
I'm newdie in c programming. this is my first project in programming. I have to write a program for a airline reservation. this is what i have done yet. but when it runs it shows the number of...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.