Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: how do i try/catch this?

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how do i try/catch this?

    hey people!

    im entirely new to java..i need a little bit of help with my assignment code..i want to use try catch. but i dont exactly know where to insert it.i keep getting errors when i insert it after "enter file name". but it says i cannot do that.i want to get a message saying the file isnt existing when wrong file names are inputted by the user.

     
    /**
     * to back up txt files
     * @author (Dinushke Arthnayake) 
     * @version (1.0.0)
     */
    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);
        }
     
    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,195
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: how do i try/catch this?

    Sorry, I don't see this "enter file name" in your code. Have you posted the wrong code?
    it says i cannot do tha
    If you get errors, please copy and paste the full text of the message here.

    What Exception do you want to catch? What method throws that exception?

    yeet3 != "acbt"

    Use the equals method to compare Strings. Not == or !=
    Last edited by Norm; September 18th, 2010 at 05:16 PM.

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how do i try/catch this?

    how exactly do you use the equals method here? please do help me..it stil says that variable yeet1 cannot be found because of the closing bracket before the 'catch' statement..what do you think i should do?how can i access the variable yeet1,yeet2 and yeet3??

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,195
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: how do i try/catch this?

    .it stil says that variable yeet1 cannot be found
    Please copy the full text of the error message and paste it here.

    It sounds like you are having a problem with scope. Variables defined inside of {} are NOT visible or usable outside of the {}. You need to define the variables outside of the {} and give them null or 0 values and then change their values inside of the {}. The new values will then be available when you come out of the {}.

  5. #5
    Junior Member Rex the king's Avatar
    Join Date
    Oct 2011
    Location
    INDIA
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how do i try/catch this?

    Encrip.encrip(yeet3,yeet2);

    Backup.backup(yeet2);

    are causing some problems

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,195
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: how do i try/catch this?

    are causing some problems
    Please explain. If you get error messages, copy and paste here the full text.

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,267
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: how do i try/catch this?

    Quote Originally Posted by Rex the king
    are causing some problems
    This thread is over a year old. I presume the original poster has solve the issue or moved on.

Similar Threads

  1. (while) insted of (try and catch)
    By b109 in forum Java Theory & Questions
    Replies: 6
    Last Post: May 13th, 2010, 09:01 PM
  2. catch all
    By silverspoon34 in forum Exceptions
    Replies: 1
    Last Post: November 29th, 2009, 01:18 PM
  3. try/catch
    By rsala004 in forum Exceptions
    Replies: 5
    Last Post: July 19th, 2009, 03:20 PM

Tags for this Thread