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

Thread: Returning An Array

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Returning An Array

    Hello I am trying to sort a list of numbers on seperate files from a text document in C:/Scores.txt. First off in processFile I am trying to return the numbers located in the file as an array. I am having some issues understanding what i should change into an array for it to return all the numbers as an array. Also it would bea bonus to know how to read each line one by one.
    package readandsort;
     
    /**
     *
     * @author Logan
     */
    import java.io.*;
    public class ReadAndSort {
     
     
        public static void main(String[] args) {
     
        }
        public static int processFile (String scores) throws IOException, FileNotFoundException {
            FileInputStream fstream = new FileInputStream("c:/scores.txt");
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            double doubleValue;
            while ((strLine = br.readLine()) !=null) {
                doubleValue = Integer.parseInt(strLine); 
                return doubleValue;
            }
            br.close();
        }
    }
     
            }
        }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Returning An Array

    To return the contents of a variable, put the variable name after the return:
    return theVarToReturn;
    The method definition statement needs to include the type of the data being returned.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Returning An Array

    Hmm I think I understand, but now I am getting an error saying ';' expected and return scores; is not a statement.
    public static int[] processFile (int[] scores) throws IOException, FileNotFoundException {
            FileInputStream fstream = new FileInputStream("c:/scores.txt");
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            while ((strLine = br.readLine()) !=null) {
                scores[] = Integer.parseInt(strLine); 
     
            }
            return scores;
            br.close();


    --- Update ---

    to clarify, on the line
    scores[] = Integer.parseInt(strLine);
    I am getting the error ; expected and not a statement.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Returning An Array

    The array reference is missing the index. Put an index inside the []s

    BTW Any statement following a return statement will NOT be executed.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Returning An Array

    For example,
    scores[3] = Integer.parseInt(strLine); ? I am not receiving an error on that but i want to return all of the scores and not just one of them

    --- Update ---

    Hmm wait, i think i understand.
    public static int[] processFile (int[] scores, int a) throws IOException, FileNotFoundException {
    FileInputStream fstream = new FileInputStream("c:/scores.txt");
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;
    while ((strLine = br.readLine()) !=null) {
    scores[a] = Integer.parseInt(strLine);
    br.close();
    }
    return scores;

    }
    Is this the correct application?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Returning An Array

    If you don't change the value of the index to the array, all the values go into the same slot in the array.

    For debugging print out the contents of the array after its been filled.
    For example:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Having trouble returning an array
    By 93tomh in forum What's Wrong With My Code?
    Replies: 16
    Last Post: July 30th, 2012, 11:09 AM
  2. [SOLVED] Issue when returning an array
    By Broxxar in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 21st, 2012, 10:19 PM
  3. returning 2d array in java
    By dr.code.skm in forum Member Introductions
    Replies: 2
    Last Post: July 20th, 2011, 10:14 AM
  4. returning a 2D array
    By straw in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2010, 04:30 AM
  5. Returning Random Strings from an Array
    By cfmonster in forum Collections and Generics
    Replies: 3
    Last Post: September 8th, 2009, 11:13 PM