Results 1 to 11 of 11
Thread: Try and Catch's
- 03-15-2012, 12:03 AM #1
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
- 03-15-2012, 12:18 AM #2
Member
- Join Date
- Jan 2012
- Posts
- 45
- Rep Power
- 0
Re: Try and Catch's
Show me some code baby! :)
- 03-15-2012, 12:19 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Re: Try and Catch's
I'm not sure I understand this. Methods, in Java, are constrained to only ever return a single thing: a primitive value (like an int) or a reference value (String etc).Because I getting an output of three integer outputs, when I only want one integer returned.
You will get whatever output a method creates - with System.out.println() or similar - and this output has nothing to do with what the method returns.
Perhaps you could post some code that illustrates your problem. Something brief, that compiles but does not do, at runtime, what you intend.
- 03-15-2012, 12:28 AM #4
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
Re: Try and Catch's
Ok. This is the method, but it needs other classes to get it executing, so don't mind what the method does. Code is below:
So in each if statement is a try-catch block to check it if a portNumber is the same as the number in an array, if not, catch it and do nothing. At least one of the those if statements will be correct and will return the value i need. Instead for an example its returning 0,0,4, where I just want the 4.?Java Code://Method to get the Process ID of a port number public int getPID(String portAdd) { convertTextFile(); int pid=0; //loop through each line in the text file for(String line: textfile) { fields = p.split(line); for(String currField: fields) { if(portAdd.equalsIgnoreCase(currField)) { try { String portAddr = fields[KEY_ADDR_P]; pid = Integer.valueOf(fields[3]); myPIDs.put(portAddr, pid); } catch(NumberFormatException nfe) { } } if(portAdd.equalsIgnoreCase(currField)) { try { String portAddr = fields[KEY_ADDR_P]; pid = Integer.valueOf(fields[4]); myPIDs.put(portAddr, pid); } catch(NumberFormatException nfe) { } catch(ArrayIndexOutOfBoundsException e) { } } if(portAdd.equalsIgnoreCase(currField)) { try { String portAddr = fields[KEY_ADDR_P]; pid = Integer.valueOf(fields[5]); myPIDs.put(portAddr, pid); System.out.println(pid); } catch(NumberFormatException nfe) { } catch(ArrayIndexOutOfBoundsException e) { } } } } return pid; }
ThanksLast edited by pbrockway2; 03-15-2012 at 01:40 AM. Reason: code tags added
- 03-15-2012, 01:41 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Re: Try and Catch's
Please use code tags.
- 03-15-2012, 02:02 AM #6
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
Re: Try and Catch's
Yes sorry, I always forget using code tags.
But do you understand my problem in the code?
- 03-15-2012, 02:41 AM #7
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
Re: Try and Catch's
I am confused as an int will not return 0,0,4.Instead for an example its returning 0,0,4, where I just want the 4
- 03-15-2012, 02:55 AM #8
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
Re: Try and Catch's
what are these lines supposed to be doing?
Java Code:if(portAdd.equalsIgnoreCase(currField)) { try { String portAddr = fields[KEY_ADDR_P]; pid = Integer.valueOf(fields[3]); myPIDs.put(portAddr, pid); } catch(NumberFormatException nfe) { } }
- 03-15-2012, 03:13 AM #9
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
Re: Try and Catch's
Sorry for the confusion. Ive found my problem. Thanks for your help :D
- 03-15-2012, 03:19 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Re: Try and Catch's
Not really - it really would help if what you posted would compile and if you said exactly what you expected it to output (possibly multiple things) and what it should return (always a single thing). Such a SSCCE takes more work than posting an extract of your code, but it is more productive.
- 03-15-2012, 03:23 AM #11
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Similar Threads
-
Try... Catch?
By Callofdudey in forum New To JavaReplies: 3Last Post: 10-06-2011, 09:29 PM -
how to catch two exceptions in one catch()?
By arnab321 in forum New To JavaReplies: 1Last Post: 11-06-2008, 10:54 AM -
try catch...
By MarkWilson in forum New To JavaReplies: 8Last Post: 06-27-2008, 05:39 PM -
try catch!?
By Joe2003 in forum Advanced JavaReplies: 2Last Post: 01-28-2008, 07:51 PM -
Use try and catch
By zoe in forum New To JavaReplies: 2Last Post: 07-25-2007, 07:50 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks