Results 1 to 8 of 8
  1. #1
    argus is offline Member
    Join Date
    Oct 2010
    Posts
    11
    Rep Power
    0

    Default jump to args-field in Main method

    Hello guys,

    i have a Main method something like this:
    Java Code:
    public static void main(String[] args) throws  IOException {
    ...args[0]=String x; ...
    }
    Is it possible to return to the point where i declared args[0] if the IOException is caught?
    So kinda like this:
    Java Code:
    catch (IOException e){
    Main.main(args[0]);
    }
    This not working! I get an Error.
    And if i do this:
    Java Code:
    Main.main(args);
    The whole Main method is starting again.
    But i would like to go to a certain point in the Main method or call a variable if the Exception is thrown.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,318
    Rep Power
    25

    Default

    I get an Error.
    Please post the full text of the error message and the code that caused the error.

    The whole Main method is starting again.
    Yes that looks like a recursive call that would start at the begining.

    You probably need to use a loop.
    Can you describe more fully what the problem is you are trying to solve?

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Why are you assinging things to arg?
    It's purpose is simply to take parameters for your program, not as some variable holder.

  4. #4
    argus is offline Member
    Join Date
    Oct 2010
    Posts
    11
    Rep Power
    0

    Default

    I'm trying to open first a directorie and second a file.The paths to both are defined through user input in the console.
    In both cases,first the user is asked to define the paths with a message like "define Directorie: " (Message#1) and "define file: "(Message)#2.
    If the user-input is bullshit the programm show an Error and the user is asked again to define the directorie or the file path,depending on which of these caused the Error.
    For every case the depending message should appear.

    Here is the code where i'm trying to do this:
    Java Code:
    private static Scanner scanner;
    private static BufferedReader reader;
    
        public static void main(String[] args) throws  IOException ,FileNotFoundException {
    
            try{
                    int c = 0;
                    args=new String[2];
    
    	        reader=new BufferedReader(new InputStreamReader(System.in));
                    
    //DEFINE DIRECTORIE
    	        System.out.println("Define Directorie path: "); //MESSAGE#1
                    args[0]=reader.readLine();
                    File dir = new File(args[0]);
                    File[] fileList = dir.listFiles();
    //DEFINE FILE
                    System.out.println("Define File path: ");       //MESSAGE#2
                    args[1]=reader.readLine();
                    scanner =new Scanner (new File (args[1]));
    
                                   for (File f : fileList){
                                           c=c+1;
                                           System.out.println(c);
                                           System.out.println(f);
                                         }
                     scanner.close();
            }
    
    // IN THIS CASE THE PROGRAMM SHOULD ASK THE USER TO DEFINE THE FILE PATH AGAIN
    catch (FileNotFoundException e){
        System.out.println("ERROR!");     //FIRST THE ERROR IF SOMETHING IS WRONG
                                      //HERE I SHOULD BE ABLE TO CALL A FIELD FROM THE MAIN -METHOD
        Main.main(args[1]);   //THIS CAUSE A WARNING:"Main cannot be applied to given types"
                                     //AND AN ERROR THAT IT CANNOT BE COMPILED
    }
    
    
            }
    Exact Error is:
    Java Code:
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code
            at referralcodes_to_mp4files.ref.main(ref.java:49)
    ERROR!
    Java Result: 1
    For me the important thing to know is how i can call Array-fields from the Main-method in other methods.
    This Code is just an Example modified to show roughly what i mean.I hope it's clear.:p
    I know i could try a loop.But it just seemed very comfortable to call Main-method-fields from like everywhere in the class.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,318
    Rep Power
    25

    Default

    Main.main(args[1])
    Where is the Main class? Your posted code doesn't show what class it is in.
    If the posted code is in the Main class, then its main method takes a String array as its argument.
    args[1] is not an array. It is a String

    Why are you using the args array as a method variable? You should create you own variable.

    user is asked again to define the directorie or the file path
    The way the user can change the values of args is if your program exits and the user types in a new command line.

    The logic for this program needs to be reworked.

  6. #6
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default

    Quote Originally Posted by Norm View Post
    The logic for this program needs to be reworked.
    Agreed. I personally am very uncomfortable with this recall to "main". I would recommend using a do while loop instead. Its just better coding.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    And do not reuse args the way you are.
    If you need something to hold Strings in then create a new one...

  8. #8
    argus is offline Member
    Join Date
    Oct 2010
    Posts
    11
    Rep Power
    0

    Default

    yes,probably should declare my own Array and then put the methods in the Array-Elements.
    Oh and i may have confused the words "Array" and "field".By "field" i meant an element of a specific "Array".Sorry for that.

Similar Threads

  1. Why can't we write main without String args[]
    By shailender in forum New To Java
    Replies: 6
    Last Post: 06-17-2013, 09:28 AM
  2. Passing values to main(String args[]) ?
    By al_Marshy_1981 in forum New To Java
    Replies: 6
    Last Post: 02-21-2010, 10:16 PM
  3. Public static void main (String args[])
    By arefeh in forum New To Java
    Replies: 12
    Last Post: 01-28-2010, 11:58 AM
  4. Replies: 9
    Last Post: 11-22-2008, 11:03 PM
  5. Eclipse- jump to method definition
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-07-2007, 03:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •