Results 1 to 7 of 7
  1. #1
    ras_pari is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Question need urgent hepl!!!!!!!!!!!

    hi ...
    I have a program which is showing errror regarding exception handling....I am not able to find it out ..plz help me!!!!!!!!
    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    
     class ProbParser {
    
     
                       public final char OPEN_PARENTHESES = '(';
    	public final char CLOSE_PARENTHESES = ')';
    	public final char OPEN_BRACKET = '[';
    	public  final char CLOSE_BRACKET = ']';
    
    
    		String recordNumberFromUser = "10";
    		File inputDataFile = new File("C:/java1/prob.txt");
    try
    {
                                 String record = findRecord(recordNumberFromUser, inputDataFile);
    }
    
    catch(FileNotFoundException ex)
    {
    }
    catch(IOException ex)
    {
    }
    
    		String output = processRecord(record);
                                          String output1 = processRecord1(record);
    
    
    		
    	                                                                                                                                    
    	
    
    	private String processRecord(String record) {
    		String[] splits1 = record.split(" \\s+.");
    		String output = null;
                                   
    		for (int j = 0; j < splits1.length; j++) {
    			if (splits1[j].length() > 0) {
    				if (hasValidbrakets(splits1[j])) {
    					output = parseValueWithinDelimiter(splits1[j],OPEN_BRACKET, CLOSE_BRACKET);
    					
    				}  
    			}
    
    		}
    return output;
    		
    	}
    
    //method for other bracket retrival
    private  String processRecord1(String record) {
    		String[] splits1 = record.split(" \\s+.");
    		
                                    String output1=null;
    		for (int j = 0; j < splits1.length; j++) {
    			if (splits1[j].length() > 0) {
    				
    					
    				if (hasValidParentheses(splits1[j])) {
    				output1 = parseValueWithinDelimiter(splits1[j],
    				OPEN_PARENTHESES, CLOSE_PARENTHESES);
    					
    				}
    			}
    		}
    		return output1;
    	}
    
    	                 private  boolean hasValidParentheses(String slice) {
    		 int beginParentheses = slice.indexOf(OPEN_PARENTHESES);
    		 int endParentheses =   slice.indexOf(CLOSE_PARENTHESES);
    
    		if (beginParentheses != -1 && (endParentheses > beginParentheses)) {
    			return true;
    		}
    		return false;
    	}
    
    
    	private  String parseValueWithinDelimiter(String text,
    			char openDelimiter, char closeDelimiter) {
    		int begin = text.indexOf(openDelimiter);
    
                             int end = text.indexOf(closeDelimiter);
    
    
    
    
    int begin1=begin+1;
    int end1=end;
    		if (text.length() != (end + 1))
    			end++;
    		String parsedText = text.substring(begin1, end1);
    		return parsedText;
    	}
    
    
    
    	private  boolean hasValidbrakets(String slice) {
    		int beginBracket = slice.indexOf(OPEN_BRACKET);
    		int endBracket = slice.indexOf(CLOSE_BRACKET);
                                   // int beginBracket1= slice.indexOf(OPEN_PARENTHESES);
    		//int endBracket1= slice.indexOf(CLOSE_PARENTHESES);
    
    		if (beginBracket != -1 && (endBracket > beginBracket)) {
    			return true;
    		}
    		return false;
    	}
    
    	private  String findRecord(String recordNumberFromUser,
    			File inputDataFile) throws FileNotFoundException,IOException{
    		BufferedReader reader = new BufferedReader(
    				new FileReader(inputDataFile));
    		while (true) {
    			String currentRecord = readSingleRecord(reader);
    			if (currentRecord == null)
    				return null;
    			if (currentRecord.startsWith(recordNumberFromUser)) {
    				return currentRecord;
    			}
    		}
    
    	}
    
    	private    String  readSingleRecord(BufferedReader reader)throws IOException
                 {
    
    		String record = reader.readLine();
    		return record;
    	}
    
    }
    The program is showing two error:
    17.illegal start of type
    try
    29:<identifier expected>
    THanx in advance!!!!!!!!!!

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    You cannot have a try block in member declaration area. It has to be contained within a method or a static block.

  3. #3
    ras_pari is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Default

    thanx for ur reply...I understand...i change code a little bit...I have declare try inside a function name findrecord AND readsinglerecord...but a new error is coming which is as follows:
    unreported exception java.io.FileNotFoundException;must be caught or declare to be thrown!!!!!!!!
    my code is:
    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    
     class ProbParser {
    
     
                       public final char OPEN_PARENTHESES = '(';
    	public final char CLOSE_PARENTHESES = ')';
    	public final char OPEN_BRACKET = '[';
    	public  final char CLOSE_BRACKET = ']';
    
    
    		String recordNumberFromUser = "10";
    		File inputDataFile = new File("C:/java1/prob.txt");
    
                                 String record = findRecord(recordNumberFromUser, inputDataFile);
    
    
    		String output = processRecord(record);
                                          String output1 = processRecord1(record);
    
    
    		
    	                                                                                                                                    
    	
    
    	private String processRecord(String record) {
    		String[] splits1 = record.split(" \\s+.");
    		String output = null;
                                   
    		for (int j = 0; j < splits1.length; j++) {
    			if (splits1[j].length() > 0) {
    				if (hasValidbrakets(splits1[j])) {
    					output = parseValueWithinDelimiter(splits1[j],OPEN_BRACKET, CLOSE_BRACKET);
    					
    				}  
    			}
    
    		}
    return output;
    		
    	}
    
    //method for other bracket retrival
    private  String processRecord1(String record) {
    		String[] splits1 = record.split(" \\s+.");
    		
                                    String output1=null;
    		for (int j = 0; j < splits1.length; j++) {
    			if (splits1[j].length() > 0) {
    				
    					
    				if (hasValidParentheses(splits1[j])) {
    				output1 = parseValueWithinDelimiter(splits1[j],
    				OPEN_PARENTHESES, CLOSE_PARENTHESES);
    					
    				}
    			}
    		}
    		return output1;
    	}
    
    	                 private  boolean hasValidParentheses(String slice) {
    		 int beginParentheses = slice.indexOf(OPEN_PARENTHESES);
    		 int endParentheses =   slice.indexOf(CLOSE_PARENTHESES);
    
    		if (beginParentheses != -1 && (endParentheses > beginParentheses)) {
    			return true;
    		}
    		return false;
    	}
    
    
    	private  String parseValueWithinDelimiter(String text,
    			char openDelimiter, char closeDelimiter) {
    		int begin = text.indexOf(openDelimiter);
    
                             int end = text.indexOf(closeDelimiter);
    
    
    
    
    int begin1=begin+1;
    int end1=end;
    		if (text.length() != (end + 1))
    			end++;
    		String parsedText = text.substring(begin1, end1);
    		return parsedText;
    	}
    
    
    
    	private  boolean hasValidbrakets(String slice) {
    		int beginBracket = slice.indexOf(OPEN_BRACKET);
    		int endBracket = slice.indexOf(CLOSE_BRACKET);
                                   // int beginBracket1= slice.indexOf(OPEN_PARENTHESES);
    		//int endBracket1= slice.indexOf(CLOSE_PARENTHESES);
    
    		if (beginBracket != -1 && (endBracket > beginBracket)) {
    			return true;
    		}
    		return false;
    	}
    
    	private  String findRecord(String recordNumberFromUser,
    			File inputDataFile) {
    		BufferedReader reader = new BufferedReader(
    			new FileReader(inputDataFile));
    try
    {
    		while (true) {
    			String currentRecord = readSingleRecord(reader);
    			if (currentRecord == null)
    				return null;
    			if (currentRecord.startsWith(recordNumberFromUser)) {
    				return currentRecord;
    			}
    		}
    }
    catch(IOException ex)
    {
    }
    
    
    	}
    
    	private    String  readSingleRecord(BufferedReader reader)
                 {
    try
    {
    		String record = reader.readLine();
    	
    }
    catch(IOException ex)
    {
    }
    	return record;
    	}
    
    }

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default

    File inputDataFile = new File("C:/java1/prob.txt"); ..this line throws FileNotFoundException.Anyway this line is used in findRecord.move this to
    findRecord.

    in findRecord put try catch block and catch fileNotFoundException...

    One more suggestion.ur code is not readable and not properly formatted...
    Ramya:cool:

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default

    Just as the compiler message says: you are using a method that can throw an exception and your code ignores it; you should embrace calling that method with a try { ... } catch block or tell the compiler that your method ignores the exception by defining a throws clause in the header of the method.

    btw. the indentation of your code stinks and don't use that many exclamation marks in your message text.

    kind regards,

    Jos

  6. #6
    AlbertoPL is offline Member
    Join Date
    Sep 2009
    Posts
    22
    Rep Power
    0

    Default

    You removed the try catch block from that line, which is no good. You need to have that try catch in order to have proper error-handling. The reason you were having trouble before hand is that you are defining variables inside of your try block:

    try
    {
    String record = findRecord(recordNumberFromUser, inputDataFile);
    }
    record is being defined in the try block, which means its scope exists only there. You should try declaring the variable outside of that block if you wish to use it outside of the try.

  7. #7
    ras_pari is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Question

    thanx all!!!Now I got my answer.........Thank u very much....

Similar Threads

  1. Help Urgent pls pls pls!!!!
    By jammyjamsticy in forum Java 2D
    Replies: 5
    Last Post: 05-06-2009, 10:55 PM
  2. Help Urgent
    By jammyjamsticy in forum Java 2D
    Replies: 2
    Last Post: 04-06-2009, 07:31 AM
  3. I need Help Urgent..
    By vidhyaprakash85 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 02-18-2009, 06:49 AM
  4. Help. It's urgent
    By monir6464 in forum AWT / Swing
    Replies: 2
    Last Post: 08-24-2008, 05:57 PM
  5. please help urgent
    By ananas7777 in forum AWT / Swing
    Replies: 2
    Last Post: 12-25-2007, 08:30 AM

Posting Permissions

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