how to try catch this?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • safra
    New Member
    • Aug 2010
    • 5

    how to try catch this?

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

    Code:
    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:");
    
            String yeet2=yeet1.nextLine();
          
       
            
            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);
       }
     catch(FileNotFoundException e){
            JOptionPane.showMessageDialog (null,D_Play);
        
            System.exit(-1);
        }
    
    }
    }
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    Code:
    Scanner yeet1=new Scanner(System.in);
    System.out.println("Enter file that you want to backup:");
    String yeet2=yeet1.nextLine();
    Wrap this code in try catch.
    Take
    Code:
    String yeet2 = null;
    If nothing return or if you catch exception do not proceed to except the password.

    Regards
    Dheeraj Joshi

    Comment

    • safra
      New Member
      • Aug 2010
      • 5

      #3
      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.
      Code:
      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:");
              String yeet2 = null;
              yeet2=yeet1.nextLine();
        }
       catch(FileNotFoundException e){
              JOptionPane.showMessageDialog (null,D_Play);
              
              System.out.println("Enter password to secure your backup:");
      
              String yeet3=yeet1.nextLine();
      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

      Comment

      • Dheeraj Joshi
        Recognized Expert Top Contributor
        • Jul 2009
        • 1129

        #4
        Try something like this

        Code:
        Scanner yeet1=new Scanner(System.in);
        String yeet2 = null;
        String yeet3 = null;
        try{
            System.out.println("Enter file that you want to backup:");
        	yeet2=yeet1.nextLine();
        	if(null == yeet2){
        		System.out.println("File name missing");
        		System.Exit(0);
        	}
        	System.out.println("Enter password to secure your backup:");
        	yeet3=yeet1.nextLine();
        	if(null == yeet3){
        		System.out.println("Password missing");
        		System.Exit(0);
        	}
        	
        	// Rest of the code
        }
        catch(FileNotFoundException e){
        	//Check type of exception
        	//Hadle it accordingly
        }
        Regards
        Dheeraj Joshi

        Comment

        Working...