Results 1 to 1 of 1
Thread: Major code error need help!!!
- 03-29-2010, 09:43 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 31
- Rep Power
- 0
Major code error need help!!!
When the for loop places values in the array (below) it seems to work fine. However, when the program gets out of the for loop, for whatever reason, only the last phone number is written to the file by the output stream. Could this be something to do with my toString() method? Please help! I don't know what's going on with this. I've included the Constructor that is called in the for loop and the toString method that is used to write back to the file.
Java Code:inputStream.close();//closes inputStream from earlier usage. TelephoneNumber[]array = new TelephoneNumber[10]; inputStream = new Scanner(new File(fileName)); for(int i = 0;inputStream.hasNextLine();i++){ String line = inputStream.nextLine(); array[i]=new TelephoneNumber(line); }//end for PrintWriter outputStream = new PrintWriter(fileName); for(int i=0; i<array.length;i++){ outputStream.println(array[i]); }//end for outputStream.close();Java Code:public TelephoneNumber(String phoneNumber){ Pattern pattern = Pattern.compile("(\\d{3})-(\\d{3})-(\\d{4})"); Matcher matcher = pattern.matcher(phoneNumber); try { if(!matcher.matches()){ pattern = Pattern.compile("(\\d{3})-(\\d{4})"); matcher = pattern.matcher(phoneNumber); if (!matcher.matches()) throw new InvalidPhoneException(); else { areaCode = 0; exchangeCode = Integer.parseInt(matcher.group(1)); number = Integer.parseInt(matcher.group(2)); } } else { areaCode = Integer.parseInt(matcher.group(1)); exchangeCode = Integer.parseInt(matcher.group(2)); number = Integer.parseInt(matcher.group(3)); } } catch(InvalidPhoneException e){ System.out.println(e.getMessage()); System.exit(0); } }//end constructor public String toString(){ if(areaCode!=0) return areaCode+"-"+exchangeCode+"-"+number; else return exchangeCode+"-"+number; }Last edited by SteroidalPsycho; 03-29-2010 at 10:56 AM.
Similar Threads
-
Major issues with code - Please help
By aldorfski_17 in forum New To JavaReplies: 3Last Post: 03-29-2010, 04:46 AM -
Major number
By lobodelbosque in forum New To JavaReplies: 1Last Post: 11-27-2009, 05:55 AM -
Major help needed with drawing rectangles using JFrame and Mouse Events
By DamienCurr in forum New To JavaReplies: 1Last Post: 07-16-2009, 02:15 PM -
Software Engineer...Computer Science Major
By giganews35 in forum IntroductionsReplies: 2Last Post: 09-14-2008, 09:19 AM -
Pls help with a code error.
By saytri in forum New To JavaReplies: 8Last Post: 12-24-2007, 08:10 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks