#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    1
    Rep Power
    0

    Assignment Writing and Outputting into another file


    1. Write a program which creates a file with all the numbers from 1 to 100.
    2. Read this file. Separate the odd numbers from even numbers and write the odd numbers to a new file called odd.dat, even numbers to even.dat.
    3. Read the odd.dat file, total all the numbers which are divisible by 3, find the average of these numbers and append to end of this file.
    I did first two but i cannot complete the third one . It ginning me an error
    Exception in thread "main" java.lang.NumberFormatException: For input string: "[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:492)
    at java.lang.Integer.parseInt(Integer.java:527)
    at javaapplication39.JavaApplication39.main(JavaApplication39.java:50)
    Java Result: 1
    Code:
    package javaapplication39;
    
    import java.util.*;
    import java.io.*;
    public class JavaApplication39 {
    
       
        public static void main(String[] args) throws IOException{
      int i,total=0,counter=0,average=0;
      List<Integer> oddNumbers=new ArrayList<Integer>();
      List<Integer> evenNumbers=new ArrayList<Integer>();
    
       PrintWriter number=new PrintWriter("number.txt");
       FileWriter fwriter=new FileWriter("odd.txt",true);
       PrintWriter odd=new PrintWriter(fwriter);
       PrintWriter even=new PrintWriter("even.txt");
      int [] array =new int [100];
      for ( i=0;i<array.length;i++){
        array[i]=i+1;
        number.println(array[i]);
      }
      number.close();
      File file = new File("number.txt");
      Scanner inputFile = new Scanner(file);
       while (inputFile.hasNext())  { 
    String tmp=inputFile.nextLine();
     int convertTmp=Integer.parseInt(tmp);
                    {
                if((convertTmp%2==0)){
                     evenNumbers.add(convertTmp);
                 }
                 else{
                   oddNumbers.add(convertTmp);
                 }
    
                    }       
    } 
    
    
      odd.println(oddNumbers);
      even.println(evenNumbers);
      inputFile.close();
      odd.close();
      even.close();
      File myFile=new File("odd.txt");
      Scanner keyboard=new Scanner(myFile);
      while (keyboard.hasNext()){
      String f=keyboard.nextLine();
    int convertF=Integer.parseInt(f);
     if (convertF%3==0){
         counter++;
         total+=convertF;
       
     }
    }
      average=total/counter;
      System.out.println(average);
      keyboard.close();
    }
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    You need to split that String up into separate, smaller Strings that only contain valid digits if you want to use the parseInt() method to get an int value.

    The String class has methods that will help you split the string into smaller Strings.

    Have you looked at the contents of the files that the program writes? Do they contain what you expect?
    Last edited by NormR; October 15th, 2013 at 08:16 PM.

IMN logo majestic logo threadwatch logo seochat tools logo