Results 1 to 1 of 1
  1. #1
    Join Date
    Mar 2009
    Posts
    31
    Rep Power
    0

    Default 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

  1. Major issues with code - Please help
    By aldorfski_17 in forum New To Java
    Replies: 3
    Last Post: 03-29-2010, 04:46 AM
  2. Major number
    By lobodelbosque in forum New To Java
    Replies: 1
    Last Post: 11-27-2009, 05:55 AM
  3. Replies: 1
    Last Post: 07-16-2009, 02:15 PM
  4. Software Engineer...Computer Science Major
    By giganews35 in forum Introductions
    Replies: 2
    Last Post: 09-14-2008, 09:19 AM
  5. Pls help with a code error.
    By saytri in forum New To Java
    Replies: 8
    Last Post: 12-24-2007, 08:10 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
  •