Results 1 to 5 of 5
Thread: array question
- 09-15-2009, 04:50 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 5
- Rep Power
- 0
-
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?
- 09-15-2009, 05:00 AM #3
Member
- Join Date
- Sep 2009
- Posts
- 5
- Rep Power
- 0
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=)
- 09-15-2009, 05:03 AM #4
Member
- Join Date
- Sep 2009
- Posts
- 5
- Rep Power
- 0
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!");
}
}
}
-
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
-
Learning - 2D array and method question.
By Russo in forum New To JavaReplies: 24Last Post: 09-15-2009, 08:16 AM -
Array question
By McChill in forum New To JavaReplies: 5Last Post: 02-20-2009, 02:18 AM -
file/ array question
By mayhewj7 in forum New To JavaReplies: 10Last Post: 02-18-2009, 03:54 PM -
Basic array question
By jigglywiggly in forum New To JavaReplies: 12Last Post: 01-09-2009, 04:44 PM -
Newbie search array question
By CirKuT in forum New To JavaReplies: 19Last Post: 09-14-2008, 06:26 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks