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 4 of 4

Thread: Build a method that prints only a specific section of a file based array in java

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Build a method that prints only a specific section of a file based array in java

    Good day guys. I have to build a method that prints only a section of an array.

    I set it up like this:

    printArray1( intList, countOfInts, 3,  6 );

    because it denotes that it prints inclusively between array[3] and array[6].

    My problem is that I do not know how to build into the method the sectioning off part.

    This is what I've come up with.

    static public int printArray1( int[] intList, int countofInts, ???, ???)
    {
        if (countofInts >= 0 && (intList.length-9 <= countofInts && countofInts >= (intList.length-6) ))
        {
            for (int i = 0; i < countofInts; i++) 
            {
                System.out.print(intList[i] + "\t ");
            }
        }
     
    return countofInts;
    }

    How to incorporate this sectioning off into the method?

    Earlier in the program it reads a file then stores the info into an array. How do I get the method to only print that certain section of the array?
    the numbers 3, 6 refer to the position of the array not the information stored in the array slot.


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Build a method that prints only a specific section of a file based array in java

    Quote Originally Posted by WeekzGod View Post
    My problem is that I do not know how to build into the method the sectioning off part.

    static public int printArray1( int[] intList, int countofInts, ???, ???)
    First of all, the countofInts is not necessary. An array has a length property but in your case is not even necessary.
    Second, the other two parameters can be simple int values, named e.g. start and end.
    Third, in this method you have only to do a simple for where an index i goes from start to end (inclusive).


    P.S. static public or public static are technically the same for compiler. But please, you should prefer the latter.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Build a method that prints only a specific section of a file based array in java

    I'm a beginner.
    "Third, in this method you have only to do a simple for where an index i goes from start to end (inclusive)."
    you lost me at that

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Build a method that prints only a specific section of a file based array in java

    Don't use being a beginner as an excuse. Man up and move on.

    The point is that you don't iterate the whole array from the first element to the last. You only have to iterate from the element you want to begin printing (element at index start) through the last, (element at index end).

Similar Threads

  1. Replies: 1
    Last Post: March 15th, 2013, 03:04 AM
  2. how to add information for specific data based on it's ID
    By banana in forum JDBC & Databases
    Replies: 0
    Last Post: August 23rd, 2012, 08:54 AM
  3. How to write to specific part of an html file using java
    By nasi in forum File I/O & Other I/O Streams
    Replies: 12
    Last Post: May 27th, 2010, 11:22 PM
  4. Error in printing method as always it prints the last value
    By TommyFiz in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 1st, 2009, 10:37 AM
  5. Replies: 1
    Last Post: March 28th, 2009, 07:21 AM