Results 1 to 4 of 4

Thread: Creating Array

  1. #1
    gnawk is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Creating Array

    hi for my program i need to make two methods
    one method will open the file passed in, create an array, and store the integers read from the file into the array. And the other method will display the contents of the array on scree and print 10 integers per line which will align numbers into columns of size 5.

    for example if the file is called
    numbers.txt
    and contains
    6 <- first digit makes the size of the array
    7
    5
    9
    4
    5
    2

    outcome should be
    Enter input filename: numbers.txt

    Original array:
    7 5 9 4 5 2

    i don't know how to do the first part where i have to create an array and since i cant do the first part i don't know if my second part works or not.

    Java Code:
    public class Array
    {
        public static void main(String[] args) throws FileNotFoundException
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.print("Enter input filename: ");
            String fileName = keyboard.nextLine();
            File file = new File(fileName);
                if (!file.exists())
                {
                    System.out.println("There is no input file called " + fileName);
                    System.exit(0);
                }
        }
    
        public static int[] inputData(File file)
        {
            Scanner inputFile = new Scanner(file);   // i don't know what to do on this part....
            while (inputFile.hasNext())
            {
                int number = inputFile.nextInt();
            }
        }
        public static void printArray(int[] array)
        {
            int count = 0;
            for (int ndx = 0; ndx < array.length; ndx++)
            {
                System.out.print(array[ndx] + " ");
                count ++;
    
                if (count == 10)
                    System.out.println();
            }
         }
    }

  2. #2
    Jeff_H is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: Creating Array

    I have a few tips and suggestions for you. First off, I would advise changing while(inputFile.hasNext()) to: while(inputFile.hasNextInt()). Otherwise if the user enters a String, the loop will iterate; by using hasNextInt() instead, the loop will only iterate when there is an integer. Second, printArray(int[] array) seems to be in perfect working order, so i'll use it as an example.
    printArray(int[] array) accesses every element of the array one at a time, you'll need to use a similar tactic to fill the array. Use a counter variable (similar to ndx in printArray()) to assign values to the individual elements of the array. inputData() is also missing a return statement. If you need me to elaborate further, feel free to ask. I left it fairly vague because I don't want to do your coursework for you :P

    On a brighter note, I would like to complement you on your use of white space; it made reading your program very easy :)

    I hope this helps.

  3. #3
    gnawk is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Re: Creating Array

    can you explain to me in more detail??
    And as for the while (inputFile.hasNextInt()) is it necessary in my coding?? Because i left it in there randomly as i was working on my coding.
    I am really confused on how to get the integers from the numbers.txt and turn them into array

  4. #4
    Jeff_H is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: Creating Array

    Certainly, I'll start with inputFile.hasNextInt(). No, it isn't necessary, but it does prevent certain run-time errors from occurring.

    Alright, here's the slightly more complex part, and i'll do my best to explain. You created a File object (file), and you assigned that object to a Scanner object. You'll use the inputFile object just like you used 'keyboard'. inputFile.nextInt() will return the next integer value found in the file.

    for example if the file is called
    numbers.txt
    and contains
    6 <- first digit makes the size of the array
    7
    5
    9
    4
    5
    2
    Java Code:
    while (inputFile.hasNext())
            {
                int number = inputFile.nextInt();
            }
    The first time the loop iterates, the variable number will be assigned a value of 6. The end of the loop is then reached, and number reaches the end of it's lifetime. The loop will continue onto its' second iteration and set number equal to 7.

    In other words, when the Scanner object, inputFile, begins reading the file, it will begin at the top. When inputFile encounters white space it stops reading. The next time nextInt() is used, the Scanner object picks up where it left off.
    I suppose the easiest way to think about it is by pretending that numbers.txt is a user entering data from behind the scenes.

Similar Threads

  1. Creating an Array of Objects
    By int80 in forum New To Java
    Replies: 4
    Last Post: 08-09-2011, 12:40 PM
  2. Creating array of JButtons
    By tabchas in forum New To Java
    Replies: 20
    Last Post: 04-16-2011, 05:36 AM
  3. Creating an array of objects
    By geowizard in forum New To Java
    Replies: 5
    Last Post: 11-16-2009, 01:25 AM
  4. Creating Array of LinkedList
    By sasikumardr in forum New To Java
    Replies: 1
    Last Post: 12-11-2007, 10:25 AM
  5. creating array at runtime
    By javaplus in forum New To Java
    Replies: 4
    Last Post: 11-08-2007, 10:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •