Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Filter-Standart Input

  1. #1
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Filter-Standart Input

    I'm writing a code that is supposed to filter a thread of integers so that repeated values are removed


    public class AboveBeyondPossible {
      public static void main(String[] args) {
     
        int N = Integer.parseInt(args[0]);
        int compare = Integer.parseInt(args[1]);
     
        N = StdIn.readInt();
     
     
        while (!StdIn.isEmpty())
        {
          N = StdIn.readInt();
          if (N != compare) {
            System.out.print(" " + N);
            compare = N;
          }
        }
     
    System.out.println(N);
      }
    }

    Error: Java.lang.ArrayIndexOutOfBoundsException:0


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Filter-Standart Input

    Java.lang.ArrayIndexOutOfBoundsException:0
    You get that error when trying to access the first element in an empty array.

    To put some elements in the args array, you need to put some arguments to on the commandline when the class is called:
    java AboveBeyondPossible The args go here
    This will pass 4 Strings in the array passed to the main() method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    einar123 (September 26th, 2013)

  4. #3
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Re: Filter-Standart Input

    When I put some integers in args I press enter, then ctr z and out comes the error:
    Exception in thread"Main java.util.NoSouch.ElementException at Java.util.Svanner.thrown for(Unknown Source)

    Why is that?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Filter-Standart Input

    Please copy the exact text of the error message and paste it here. Your made up verison is leaving out important parts of the message.

    --- Update ---

    Be sure to include the commandline that you are using to execute the program.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    einar123 (September 26th, 2013)

  7. #5
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Re: Filter-Standart Input

    Thanks. It's almost solved, so to speak. I have feeling the professor wont give correct for this code.

    I don't understand WHY do I need to input string(just some numbers) at the command line and press enter, then again put some integers onscreen(?) and then enter and then comes correct output.

    I use the command line to compile and run. I dont know how to copy/paste there. When I run it dr Java it's the same thing, everything works fine, except it seems to in infinite loop...

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Filter-Standart Input

    WHY do I need to input string(just some numbers) at the command line
    Because the code uses the String array passed to the main() method. The args from the commandline are placed in that String array. If there are no args, the array will be empty.

    On Windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    einar123 (September 26th, 2013)

  10. #7
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Re: Filter-Standart Input

    Is there another way of programming this, with the StdIn?

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Filter-Standart Input

    Yes. Instead of getting any user input from args passed in from the command line, use methods in a class to read the user's input from the console.
    I assume that: StdIn.readInt(); is a method of the StdIn class that reads user input from the console. I am not familiar with the StdIn class.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #9
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Re: Filter-Standart Input

    I'm thinking if Im using StdIn why do I need:

    int N = Integer.parseInt(args[0]);
    int compare = Integer.parseInt(args[1]);

  13. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Filter-Standart Input

    Why did you put those two lines of code in your program?
    If you don't want to look at the args from the commandline, don't use any of the elements of the String array that is passed to the main() method.
    If you don't understand my answer, don't ignore it, ask a question.

  14. The Following User Says Thank You to Norm For This Useful Post:

    einar123 (September 26th, 2013)

  15. #11
    Member
    Join Date
    Aug 2013
    Location
    Reykjavik, Iceland
    Posts
    51
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default Re: Filter-Standart Input

    Habit I guess.Thanks.

Similar Threads

  1. Use Of Filter in Web App
    By noorul11 in forum Web Frameworks
    Replies: 2
    Last Post: January 12th, 2013, 01:44 AM
  2. image filter
    By Shomora in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 5th, 2012, 06:36 AM
  3. image filter
    By wap in forum Java Theory & Questions
    Replies: 2
    Last Post: June 28th, 2012, 06:26 AM
  4. Word filter assignment help
    By normandygahn in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 15th, 2010, 07:42 AM
  5. Internet Filter to display some website
    By sundarjothi in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: May 15th, 2008, 05:03 AM