fileSize help

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

    fileSize help

    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.




    public int fileSize(String name) throws IOException
    {

    try {
    InputStreamRead er isr = new InputStreamRead er(new FileInputStream ("name"));
    int charLength = 0;

    while (isr.read() != -1)

    charLength++;

    return charLength;
    }

    catch (Exception e) {
    return -1;
    }
    }

    it always returns -1
    any know where i am going wrong?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Does a file named "name" in your current working directory exists? You can find
    your current working directory from Java as follows:

    [code=java]
    System.out.prin tln("cwd: "+System.getPro perty("user.dir "));
    [/code]

    kind regards,

    Jos

    Comment

    • jonjon0688
      New Member
      • Mar 2008
      • 14

      #3
      But does the question not state that the file is called "name"? it would not matter whether i have the file or not when i submit my work.
      Is it something to do with where i have placed try {

      thanks for the help

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by jonjon0688
        But does the question not state that the file is called "name"? it would not matter whether i have the file or not when i submit my work.
        Is it something to do with where i have placed try {

        thanks for the help
        Nope, your method is supposed to take a String argument which is supposed to
        be the name of a file. Not literally that the name of the file should be "name", i.e.
        if you have a file named "fronobulax.foo " in your current working directory pass
        that string to your method.

        kind regards,

        Jos

        Comment

        • jonjon0688
          New Member
          • Mar 2008
          • 14

          #5
          is this what you mean ?

          public int fileSize(String name) throws IOException
          {

          try {
          InputStreamRead er isr = new InputStreamRead er(new FileInputStream (System.getProp erty("user.dir" )));

          int charLength = 0;

          while (isr.read() != -1)

          charLength++;

          return charLength;
          }

          catch (Exception e) {

          return -1;
          }
          }

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            No that's not what I mean, use the parameter 'name' (without quotes):

            [code=java]
            InputStreamRead er isr = new InputStreamRead er(new FileInputStream (name));
            [/code]

            All I wanted to say is that you have to supply a name of an existing file; otherwise
            you get back -1 from that method which is what you complained about.

            kind regards,

            Jos

            Comment

            • jonjon0688
              New Member
              • Mar 2008
              • 14

              #7
              this is what i have:

              public int fileSize(String name) throws Exception
              {

              try {
              InputStreamRead er isr = new InputStreamRead er(new FileInputStream ("name.txt") );

              int charLength = 0;

              while (isr.read() != -1)

              charLength++;

              return charLength;
              }

              catch (IOException e) {

              return -1;

              }
              }

              it is not returning the right number of characters?

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                You're doing it again: the name of the file is supposed to be passed in the
                parameter 'name' (without the quotes). All you do is try to open an file name.txt.
                Don't do that; we'll get back to your logic error later.

                kind regards,

                Jos

                Comment

                • jonjon0688
                  New Member
                  • Mar 2008
                  • 14

                  #9
                  sorry i know im being really stupid, but i don't quite understand what you mean?

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by jonjon0688
                    sorry i know im being really stupid, but i don't quite understand what you mean?
                    Your method is always opening a file called name.txt. It is ignoring the parameter(calle d name) that is being passed. If I want to use it for my file called, say myFile.txt, it won't work because it is hardcoded to work only for a file called name.txt.

                    Comment

                    • jonjon0688
                      New Member
                      • Mar 2008
                      • 14

                      #11
                      Originally posted by r035198x
                      Your method is always opening a file called name.txt. It is ignoring the parameter(calle d name) that is being passed. If I want to use it for my file called, say myFile.txt, it won't work because it is hardcoded to work only for a file called name.txt.


                      ok i understand, so what should i do?

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by jonjon0688
                        ok i understand, so what should i do?
                        In new FileInputStream ("name.txt") );

                        use the String parameter that is passed to your method instead of "name.txt".

                        You do know what a parameter is right?

                        Comment

                        • jonjon0688
                          New Member
                          • Mar 2008
                          • 14

                          #13
                          like this ..

                          public int fileSize(String name) throws IOException
                          {

                          try {
                          InputStreamRead er isr = new InputStreamRead er(new FileInputStream (name));
                          int charLength = 0;

                          while (isr.read() != -1)

                          charLength++;

                          return charLength;
                          }

                          catch (Exception e) {
                          return -1;
                          }
                          }

                          the error i get know is that it does not give the right output..

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #14
                            Originally posted by jonjon0688
                            the error i get know is that it does not give the right output..
                            What is the output and what should it be?

                            kind regards,

                            Jos

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #15
                              Originally posted by jonjon0688
                              like this ..

                              public int fileSize(String name) throws IOException
                              {

                              try {
                              InputStreamRead er isr = new InputStreamRead er(new FileInputStream (name));
                              int charLength = 0;

                              while (isr.read() != -1)

                              charLength++;

                              return charLength;
                              }

                              catch (Exception e) {
                              return -1;
                              }
                              }

                              the error i get know is that it does not give the right output..
                              Read your problem specs again. Aren't you supposed to use a certain method ...

                              Comment

                              Working...