Homework help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jonjon0688
    New Member
    • Mar 2008
    • 14

    #31
    Using the countChars() method from the previous answer, write a method called fileSize() declared as follows:
    public int fileSize (String name) {
    ...
    }
    which counts the number of characters in the file whose name is supplied as the "name" parameter. This method should return the number of characters in the file, or -1 if an IOException occurs.

    This is what i have done :
    public int fileSize(String name) throws IOException
    {
    try{
    int count = 0;
    File file = new File("name");
    Scanner scan = new Scanner(file);

    while( file.exists())
    {
    count++;
    }
    return count;
    }
    catch (IOException e) {
    return -1;
    }
    }

    error: cannot find the class scanner.
    Any suggestions would be much appreciated?

    Comment

    • Laharl
      Recognized Expert Contributor
      • Sep 2007
      • 849

      #32
      As much as I hate to implicitly condone thread necromancy, did you import java.util.Scann er?

      Comment

      • jonjon0688
        New Member
        • Mar 2008
        • 14

        #33
        same question ..
        public int fileSize(String name) throws IOException
        { try {
        InputStreamRead er isr = new InputStreamRead er(new FileInputStream ("name"));
        int charLength = 0;
        int c;
        while ((c = isr.read()) != -1)


        charLength++;

        return(charLeng th);
        }

        catch (Exception e) {
        return -1;
        }

        }


        error : Test 1 succeeded (0 marks):
        The compilation was successful


        Test 2 failed (exit code = 1):
        The output should have been:
        countChars

        This is what was actually produced:
        public int fileSize(String name) throws IOException
        { try {
        InputStreamRead er isr = new InputStreamRead er(new FileInputStream ("name"));
        int charLength = 0;
        int c;
        while ((c = isr.read()) != -1)


        charLength++;

        return(charLeng th);
        }

        catch (Exception e) {
        return -1;
        }

        }


        Test 3 failed (exit code = 1):
        Your converted value was -1
        It should have been 1446
        Try again!


        Test 4 failed (exit code = 1):
        Your converted value was -1
        It should have been 414
        Try again!


        Test 5 succeeded (1 mark):
        Success!

        help??

        Comment

        Working...