开发者

Java Programming Help

im a bit confused on this and i dont know how to solve this question that i have been asked, id be grateful if you could assist me on this question, maybe try to tell me what needs to be done, and how. the question is:

Write a method called countChars which takes an InputStream as a parameter, reads the stream and returns the number of characters it contains as an int. Any IOExceptions which might occur in the method should be passed back to the method's caller.

Note that the method header should show that it is possible that an exception might occur.

i attempted this question with the following code:

public class countChars

{


public int countChars(int InputStream)
{
  return InputStream;

}


}

and i get an error message saying :

Main.java:26: cannot find symbol
symbol  : 开发者_StackOverflow中文版method countChars(java.io.InputStream)
location: class Main
        s1 = "" + countChars(f1);
                  ^
1 error


public class CounterUtility
{
    public static Integer countChars(InputStream in)
    {
        Integer counter = 0;

        /* your code here */

        return counter;
    }
}

s1 = CounterUtility.countChars(f1).toString();


You have a couple things mixed up. For one your function is going to take in an InputStream and return an int. You have your function set up to take in an int called InputStream and return an int.

InputStream has a function, read(), which loads the next character of the stream (or -1 if there are no remaining characters). You'll need to define an integer counter and then call read() as many times as it takes to get that response of -1. Once you see a -1 you know that you're at the end of the stream and you can return the counter (which will have a value equal to the number of characters).

//take in an InputStream object and return an int
public int countChars(InputStream input){
   int counter = 0; //start counting at zero
   while (input.read() != -1){
       //as long as there are more characters, keep incrementing the counter
       counter++; //add one to the counter
   }
   return counter; //return the result
}

I never attempted to compile the above code and I haven't written in Java since college, so there may be syntactical errors.


Your countChars method is inside of a class called countChars.
When you call the method, you need an instance of the class.

You need to make the method static, then change the call to countChars.countChars(f1).


The error is probably due to the fact that the countChars method was not successfully compiled. If you review the rest of the output of the compiler you'll probably see it complaining on the return statement. Replace the stub return, with return 0 and it should go through.

Besides, you should not use InputStream as variable name. Use is or something similar.

Furthermore, if the method should take an InputStream as argument, the signature should be

public int countChars(InputStream is) { ...


Seems like you may be compiling the wrong file. The code example you included does not contain the code from the error message. I therfore conclude that the compiler is not acting on the source file that you believe it is (assuming you have accurately copy/pasted the code you are using).


First things first: In Java, class names should always be capitalized.

Second, the code you are running is in the Main class. Your countChars method is in the countChars class. You need to create a countChars object to call it on, or make countChars a static method, which would be called via countChars.countChars(intHere).

Third, int InputStream would be an integer value with the name InputStream, not an InputStream object.


A few points. Generally it is considered good practice to Capitalize all Java Classes. You aren't forced to do so, but when you don't it makes readability much more difficult for the rest of the world.

So public class countChars becomes public class CountChars.

Second, you are attempting to call a method on an object, but you don't specify which object you want to use. Generally you can't use a method all by itself in object oriented programming, you must specify the class and then the method to call on the class. This is done one of two ways:

  1. By creating an Object (instance of the class) and the calling the method on that Object, like so:
CountChars counter = new CountChars();
s1 = "" + counter.countChars(i);
  1. By declaring the method to be static, which means it will be a method on the "Class" Object, CountChars. To call a static method, the class name is used, like so:
s1 = "" + CountChars.countChars(i);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜