Results 1 to 5 of 5

Thread: array question

  1. #1
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Cool array question

    How do I store the first number from a file and use it to make an array of that number?

    ex:
    5 //want to store this number
    1 2 3 4 5 // and loop to see if this is an array of 5.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,271
    Blog Entries
    1
    Rep Power
    25

    Default

    First things first... do you know how to read a text file? Do you know how you would you get that first line to begin with?

  3. #3
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default

    Yes i know how to read from a file and get it to print out the integers, but I am not very good with arrays. I don't know how to make it read the first integer and store it with out storing it into an array. I hope that makes since=)

  4. #4
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default

    This is what I ave so far, but it is spitting out random yes's and no's.

    import java.util.*;
    import java.io.*;

    import static java.lang.System.out;

    public class Squares {

    public static void main(String[]args)
    {
    int square;
    int matrix [][]= new int [64][64];
    int counter=1;
    int n;

    Scanner filename = new Scanner(System.in);

    out.print("Enter file name: ");
    String inputfilename = filename.nextLine();

    out.print("Enter output name: ");
    String outputfilename = filename.nextLine();

    out.println();


    try
    {
    File inputFile = new File(inputfilename);
    File outputFile = new File(outputfilename);

    filename = new Scanner(inputFile);

    out.println("Square #"+counter+":" );

    while(filename.hasNextInt())
    {
    square = filename.nextInt();


    for(int i=1; i<= square; i++)
    {
    for(int j=0; j <= square; j++)
    {
    matrix[i][j]=((i+1)+j)%square;

    if (matrix[i][j] == 0)
    {
    out.println("Yes!");
    }
    else
    {
    out.println("no");
    }

    }
    }


    counter++;

    }
    }
    catch(FileNotFoundException e)
    {
    out.print("File not found!");
    }
    }


    }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,271
    Blog Entries
    1
    Rep Power
    25

    Default

    Just read the first line of the file via fileName.nextLine() and parse that line. Then and only then do you go into your while loop.

Similar Threads

  1. Learning - 2D array and method question.
    By Russo in forum New To Java
    Replies: 24
    Last Post: 09-15-2009, 08:16 AM
  2. Array question
    By McChill in forum New To Java
    Replies: 5
    Last Post: 02-20-2009, 02:18 AM
  3. file/ array question
    By mayhewj7 in forum New To Java
    Replies: 10
    Last Post: 02-18-2009, 03:54 PM
  4. Basic array question
    By jigglywiggly in forum New To Java
    Replies: 12
    Last Post: 01-09-2009, 04:44 PM
  5. Newbie search array question
    By CirKuT in forum New To Java
    Replies: 19
    Last Post: 09-14-2008, 06:26 AM

Posting Permissions

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