I have to create a program that displays the 9 rows of a sudoku as 9 9-digit numbers and then prompt the user to do one of 6 operations on the sudoku. Then we have to output the sudoku each time the user performs an operation. This is sort of a sample run of how it should go:
Welcome to Sudoku Permuter.
C C C C C C C C C
1 2 3 4 5 6 7 8 9
R1 0 8 0 4 0 2 0 6 0
R2 0 3 4 0 0 0 9 1 0
R3 9 6 0 0 0 0 0 8 4
R4 0 0 0 2 1 6 0 0 0
R5 2 0 0 0 0 9 6 0 0
R6 0 1 0 3 5 7 0 0 8
R7 8 4 0 0 0 0 0 7 5
R8 0 2 6 0 0 0 1 3 0
R9 0 9 0 7 0 1 0 4 0
(0 denotes a blank)
Enter 1 to swap two rows in a panel
Enter 2 to swap two columns in a panel
Enter 3 to swap two row panels
Enter 4 to swap two column panels
Enter 5 to swap two numbers
Enter 0 to end:
Let's say the user enters 3 (to swap two row panels). This would come up:
Enter row panels (1-3) to swap: 3 1
It would swap row panels 1 and 3, and this would be the output:
C C C C C C C C C
1 2 3 4 5 6 7 8 9
R1 8 4 0 0 0 0 0 7 5
R2 0 2 6 0 0 0 1 3 0
R3 0 9 0 7 0 1 0 4 0
R4 0 0 0 2 1 6 0 0 0
R5 2 0 0 0 0 9 6 0 0
R6 0 1 0 3 5 7 0 0 8
R7 0 8 0 4 0 2 0 6 0
R8 0 3 4 0 0 0 9 1 0
R9 9 6 0 0 0 0 0 8 4
Rows 1-3 have been switched with rows 7-9.
Let's say the user inputs 5. This comes up:
Enter two numbers: 2 8
The original sudoku is outputted again, except 2's and 8's are switched throughout.
C C C C C C C C C
1 2 3 4 5 6 7 8 9
R1 0 2 0 4 0 8 0 6 0
R2 0 3 4 0 0 0 9 1 0
R3 9 6 0 0 0 0 0 2 4
R4 0 0 0 8 1 6 0 0 0
R5 8 0 0 0 0 9 6 0 0
R6 0 1 0 3 5 7 0 0 2
R7 2 4 0 0 0 0 0 7 5
R8 0 8 6 0 0 0 1 3 0
R9 0 9 0 7 0 1 0 4 0
If the user entered 1, something would come up saying
Enter two rows (1-9) to switch:
And whichever rows the user enters, those two individual rows would be swapped and the sudoku would once again be outputted. It'd be similar if the user entered 2, except 2 columns would be switched. Similarly, if the user entered 4, two column panels would be switched.
We're supposed to use a two dimensional array like this:
int [] [] sudoku = new int[10] [10]
I have no idea how to do this. I've been struggling all semester, this is my first programming class. I just don't understand arrays at all, and I don't understand how we're even supposed to display the sudoku in the first place. This problem isn't in our book, so I have nothing to look back on, either. I really need to pass this class. If anyone could help me, I really appreciate it. Try to make it easy to understand, there's a lot of stuff I haven't learned how to do yet (ex: for the record, idk what parseInt is). I've tried reading the book (several times). It helps some, but this program is going to be impossible. Thank you SO much for the help.
Do you know how to read input?
Do you know how to work with 1-dimensional arrays (lists), like {1, 2, 3, 4, 5}?
Do you understand for loops?
What part of the concept of arrays seems difficult to you?
For instance, here's a block of code that just prints out the raw contents of the array. Does this code make sense?
int [] [] sudoku = new int[10] [10];
// loop through all of the rows
for (int row = 0; row < 10; row++) {
// loop through all columns for each row
for (int column = 0; column < 10; column++) {
// print out the sudoku value at that row and column
System.out.print(sudoku[row] [column] + " ");
}
// at the end of the row, print a blank line to start the next row
System.out.println();
}
Here's how you can hard-code your sample board to work with it:
int [] [] sudoku = new int [] [] {
{ 0, 8, 0, 4, 0, 2, 0, 6, 0 },
{ 0, 3, 4, 0, 0, 0, 9, 1, 0 },
etc.
}
Here's some pseudo-code for swapping two rows.
Get first row # from user
Get second row # from user
Loop through each column in the board
Swap(cell at first row #, current column, cell at second row #, current column)
End Loop
Swapping basically requires a temporary variable to hold one of the values while swapping:
Swap(a, b)
Store a into Temp
Store b into a
Store Temp into b
As mellamokb's comment says, breaking things down into parts is the trick. At first glance I would probably do something like this:
Hardcode in a sudoku board, and make a routine to print the board out
Once that works right, then you'd need a menu. So extend your program to make a little menu that prints back the user's selection, and does nothing more
Once that's going, you can start filling in the menu choices. So when the user selects option one, make the routine for swapping two rows. The other menu items can still just print out their number.
Once you've got that working, you're actually nearly done. You can take your routine from #3 and make copies of it with small changes to make other 4 modifications possible.
You don't mention where the sudoku board comes from. If it's hardcoded, you're done. If not, then all you have to is make that method. At this point, you already know that you can print the board right, show the menu, and change the board.
You mention problems with arrays, and they can be a bit of a leap. Is there a specific question you have about arrays that we might be able to help with? They are like anything else in programming. You start not knowing a ton about them, and just sort of following what guidance and code you find elsewhere. As you gain more experience (as you will on this project and future projects), they'll be less mysterious and make more sense until one day they're as easy as 3 + 7.
Related
I have a vector that looks like this:
y =
Columns 1 through 19:
1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2
Columns 20 through 38:
2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4
Columns 39 through 57:
4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6
Columns 58 through 67:
6 6 6 6 6 6 6 6 6 6
The vector y is always start at 1 and be counted up. You see that there are lots of same numbers. It's the classes for the samples.
Here we have 1 1 1 1 1 1 1 1 1 1 1 1 = 12 samples for class number 1.
We have 2 2 2 2 2 2 2 2 2 2 2 = 11 samples for class number 2.
My problem here is that I want to find start and stop for every class. For example: Class 1 begins always at index 0 and ends, in this case, at index 11.
Class 2 begins directly after class 1 ends.
Question:
I'm using EJML (Effient Java Matrix Library) and I'm planning to use this function:
C = A.extractMatrix(1,4,2,8)
Which is equal to this MATLAB code:
C = A(2:4,3:8)
But I need to find the start and stop indexes from this y vector. In what index does e.g class 3 stops and starts? Do you have any smart ideas how to do that?
Sure, I could use a for-loop, to do this, but for-loops in Java is quite slow because I'm going to have a very very large y vector.
Suggestions?
Edit:
Here is an suggestion. Is that good, or could it be done better?
private void startStopIndex(SimpleMatrix y, int c, Integer[] startStop) {
int column = y.numCols();
startStop[0] = startStop[1] + 1; // Begin at the next class
for(int i = startStop[0]; i < column; i++) {
if(y.get(i) != c) {
break;
}else {
startStop[1] = i;
}
}
}
Assuming that we are calling the method from:
Integer[] startStop = new Integer[2];
for(int i = 0; i < c; i++) {
startStopIndex(y, c, startStop);
}
If you want to do it faster then binary search is your friend. Threw this together really quick and it does things in O(log n) time, where as a linear search does it in O(n). It's pretty basic and assumes your data looks pretty much like you describe it. Feed it weird data and it will break.:
int[] breakPoints(int[] arr, int low, int high){
int[] rtrn = new int[high];
for(int i=low;i<high;i++){
rtrn[i]=binarySearch(arr, i, 0, arr.length-1);
}
return rtrn;
}
int binarySearch(int[] arr, int k, int start, int end){
int mid = (start+end)/2;
if(mid==arr.length){
return -1;
}
if(arr[mid]==k && arr[mid+1]==k+1){
return mid+1; //or just mid if you want before breakpoint
}
if(arr[mid]<=k){
return binarySearch(arr, k, mid+1, end);
}
return binarySearch(arr, k, start, mid-1);
}
You'd call it like this:
int[] data = {1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,5,5,6,6,6,6};
int[] bp = breakPoints(data,1,6);
//return 0, 3, 8, 13, 16, 18
I think there is a name for this, but I can't remember what it might be, but you start looking for the next boundary with an accelerating search, and use a binary search after that.
You know the numbers are in ascending order, and there are potentially a lot of the same number, so you start by checking the next element. But instead of keep going 1 step at a time, you accelerate and step 2, 4, 8, 16, ... until you find a higher number.
Once you've found a higher number, you've gone too far, but the last step had the initial number, so you know the boundary is somewhere between the last two steps, and you then apply a binary search for the boundary.
Once you've fund the boundary, you start over stepping 1, 2, 4, ... for the next boundary.
If you expect most numbers to have about the same number of occurrences, you could keep a running average count, and make the first step with that average, to get a running start.
I'll leave it to you to actually code this.
The below is in MATLAB. the for loop will go through each unique value stored in x1 and then find the first and last occurrence of that value.
x = [ 1 1 1 2 2 3 3 3 3 3 4 4 4 4 5 5 5 ]
x1 = unique(x)'
for k1 = 1:length(x1)
x1(k1,2:3) = [find(x == x1(k1,1),1,"first"), find(x == x1(k1,1),1,"last")];
end
the above code yields x1 to be a 3 column matrix
1 1 3
2 4 5
3 6 10
4 11 14
5 15 17
I am trying to use nested for loops (java) to print out the following:
331
330
322
311
300
222
111
and I am having some trouble. So far I have:
for(int a = 3; a >=0; a--)
{
for(int b = 3; b>=0; b--)
{
for(int c = 2; c>=0; c--)
{
System.out.println(a + " "+ b +" "+ c);
}
}
}
but that prints out something more like this:
3 3 2
3 3 1
3 3 0
3 2 2
3 2 1
3 2 0
3 1 2
3 1 1
3 1 0
3 0 2
3 0 1
3 0 0
2 3 2
2 3 1
2 3 0
2 2 2
2 2 1
2 2 0
2 1 2
What is wrong with my code? How can I get it to print out the first sequence, not the second? I'm pretty sure it has something to do with the middle loop, but I'm really not sure.
Thanks!
If you have nested for loops, unless you set the limits on the inner loops based on the values of the variables in the outer loops, the pattern generated by the inner loops (second and third numbers, in this case) is gonna be the same in each iteration of the outer loop.
I can't code right now, but it seems to me that if you set b to run from a to zero, instead of from 3 to zero you might get a little closer to what you want.
But still, since it seems there isn't a pattern in what you want to get, it's hard to think of an algorithm to print them. What do these numbers mean?
I have a sparse 2D data set with about 6 million records each containing up to about 20 features.
I want to convert this into a non-sparse 2D data set with only zero's and ones, but i keep getting the OutOfMemoryError.
Here's an example of my data set:
1: 1723762 2737 2 283
2: 239 220 12 19237 2795
3: 3910 2931 86043 26354 349874 73736 2611 93921 4123830
4: 5
5: 4728 2
...(up to ~6mil)
I want to convert it into:
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...(up to ~6mil)
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
...(up to ~6mil)
notice that on the first row, there is a 1 at the 2nd column, because it has that feature in the original data set, same at the 5th column on the 4rd row and 2nd column on the 5th row.
I have created a code for this, but it already runs out of memory when I try the following:
File data = new File("dataSet.txt");
Scanner in = new Scanner(data);
//check how many rows
int nRows = 0;
while (in.hasNextLine()) {
in.nextLine();
nRows++;
}
//initializing the matrix
in.close();
in = new Scanner(data);
int[][] matrix = new int[nRows][nRows];
It seems it cannot even handle that large an array? It is crucial that I get a non-sparse matrix. Is there any way around this memory error?
Kind regards,
Allocate the array in smaller, more manageable parts, make those parts persistent after you do work on them, and then look them up again as needed. The problem here is you can't sort and process the whole thing into memory at once. You are handling an immense amount of data, and no amount of additional heap space will help you if you want to build a 6Mx6M matrix.
OK here is what I have to do
As an employee of MCI (Mammoth Cakes Incorporated), it is your job to create extremely large
layered birthday cakes. A layered birthday cake is made by taking small circular cakes layers and
stacking them on top of each other.
To perform your job, you stand in front of a big conveyor belt
while layers of varying sizes pass in front of you. When you see one you like, you may take it off the
conveyor belt and add it to your cake.
You may add as many layers to your cake as you would like,
as long as you follow these rules:
Once a layer is added to your cake it cannot be moved. (It messes up the icing.) Thus, layers
can only be added to the top of your cake.
Each layer passes in front of you only once. You may take it or leave it. If you take it, you
must add it to the top of your cake. If you leave it, it will move on down the conveyor belt,
never to return.
Each layer in your cake must be at least as small as the layer below. You cannot place a
larger layer on top of a smaller one.
You will be told in advance the diameters (in inches) of the layers coming down the conveyor belt.
Your job is to create the tallest cake possible using those layers.
For example, suppose the following list represents the diameters of the layers coming down the
conveyor belt: 8 16 12 6 6 10 5
Suppose you take the first layer (with a diameter of 8”) for your cake. That means you may not take the second layer (since you already have a layer of size 8”, and 16” > 8”). Similarly, you could not
take the third layer, but you could take the fourth layer (since 6” < 8”).
Following that, you could
also take the fifth layer (the rule is simply that the layer on top cannot be larger; it can be the same
size). Proceeding in this fashion we can create a cake with a height of 4 layers: 8 6 6 5
However, if we had let the first layer go on by and started with the second layer, we could create a
cake with a height of 5: 16 12 6 6 5
Your program will process multiple input sets, one per line. Each line will begin with an integer N,
followed by N positive integers representing the sizes of the cake layers in the order that they will be
arriving on the conveyor belt. N will always be a non-negative integer, 0 N 100,000. Each layer
will have a diameter between 1 and 100,000, inclusive. A line where N = 0 marks the end of the
input
Sample Input
7 8 16 12 6 6 10 5
10 45 25 40 38 20 10 32 25 18 30
10 10 9 8 7 6 5 4 3 2 1
0
Sample Output
5
6
10
Question: Find the tallest layer of Cakes
Here is what I have written so far:
import java.io.*;
import java.util.*;
public class cake
{
private static String line;
private static ArrayList storage = new ArrayList();
private static Integer highestStack = 0;
public static void main(String [] args)throws IOException
{
FileReader fin = new FileReader("cake.in");
BufferedReader infile = new BufferedReader(fin);
FileWriter fout = new FileWriter("cake.out");
BufferedWriter outfile = new BufferedWriter(fout);
line = infile.readLine();
do
{
String[] temp = line.split(" ");
String number;
for(int j = temp.length-1; j!=0; j--)
{
if(Integer.parseInt(temp[j]) <= Integer.parseInt(temp[j-1]))
{
highestStack++;
}
}
storage.add(highestStack);
// Collections.sort(storage);
line = infile.readLine();
}while(!line.equals("0"));
infile.close();
outfile.close();
}
}
As I commented on several answers totally missing the point, this is a Dynamic Programming problem.
Now that you added the constraints, it is clear that a Dynamic Programming solution running in O(n^2) is the way to go, and the fact that N won't go above 100 000 makes it easy to solve using DP (and probably very hard to solve using non-DP algos).
At every moment, you have to ask yourself "What is the best I can do up to 'x'".
Here's how it looks like for you first example :
0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 (Best we can do using pieces: 5 )
0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 2 2 (Best we can do using pieces: 5 10 )
0 0 0 0 0 1 2 2 2 2 2 2 2 2 2 2 2 (Best we can do using pieces: 5 10 6 )
0 0 0 0 0 1 3 3 3 3 3 3 3 3 3 3 3 (Best we can do using pieces: 5 10 6 6 )
0 0 0 0 0 1 3 3 3 3 3 3 4 4 4 4 4 (Best we can do using pieces: 5 10 6 6 12 )
0 0 0 0 0 1 3 3 3 3 3 3 4 4 4 4 5 (Best we can do using pieces: 5 10 6 6 12 16 )
0 0 0 0 0 1 3 3 4 4 4 4 4 4 4 4[5] (Best we can do using pieces: 5 10 6 6 12 16 8 )
Tallest cake as a height of: 5
The way to read a line above is easy. Let's take the first line for example:
0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1
It means that the tallest cake we can make that has a base of anywhere from 5 to 16 is made of one element (our first piece, the '5').
Then we get the piece '10', and we get the line:
0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 2 2
It means that the tallest cake we can make from 5 to 9 will have one element (our '5') and that from 10 to 16 we can stuff two pieces (5 and 10).
And you repeat like that, with up to 100 000 elements if you want.
On my computer a full 100 000 solution takes less than 20 seconds to be solved using Dynamic Programming.
Here's the code solving your problems and outputting the above. I added output statements on purpose so that you can see what is going on (it will only look pretty with relatively small numbers that said, it is really just to get what is going on with the algorithm).
public static void main( String[] args ) {
doIt( new int[] {8,16,12,6,6,10,5} );
doIt( new int[] {0, 45, 25, 40, 38, 20, 10, 32, 25, 18, 30} );
doIt( new int[] {10, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1} );
}
public static void doIt( int[] r ) {
final int[] a= new int[r.length];
int max = Integer.MIN_VALUE;
for (int i = 0; i < a.length; i++) {
max = Math.max( max, a[i] );
a[(a.length-1)-i] = r[i];
}
final int[] s = new int[max+1];
for (int i = 0; i < a.length; i++) {
final int size = a[i];
s[size]++;
for (int j = size+1; j < s.length; j++) {
s[j] = Math.max( s[j-1], s[j] );
}
for (int j = 0; j < s.length; j++) {
System.out.print( " " + ((s[j]) > 9 ? "" : " ") + s[j] );
}
System.out.print( " (Best we can do using pieces: " );
for (int k = 0; k <= i; k++) {
System.out.print( a[k] + " " );
}
System.out.println( ")" );
}
System.out.println( "Tallest cake as a height of: " + s[s.length-1] );
}
I'm not certain what you're asking, exactly.. So, I'll give you some general hints.
Look into the Stack data structure, instead of an ArrayList. Push a layer onto the stack, and then use peek to check the diameter of the topmost layer of your cakestack against the current item in the conveyor.
If the goal is to find the tallest possible cake, a naive approach would be to simply apply the above algorithm by starting with the first layer in the conveyor, continuing to the end, and recording the ultimate height (stack.size()). Then repeat with the second item in the conveyor as your base, and then the third, and so on, comparing the resulting height against the recorded max at the conclusion of each loop.
Let's walk through the process. Each time we encounter a layer on the assembly line, we make a decision: use this layer or not? The best result overall is the better of the following two outcomes:
We use this layer, and build on top of it the tallest cake using the remaining layers not larger than this layer.
We don't use this layer, and build the tallest cake using any of the remaining layers.
We can model this simply with recursion - pseudocode:
tallest(remaining_layers, base_size) = # set base_size = infinity the first time
max(
first_layer + tallest(other_layers, size(first_layer)),
tallest(other_layers, base_size)
)
where first_layer = first(remaining_layers),
other_layers = rest(remaining_layers)
However, that won't cut it by itself, since we're supposed to use dynamic programming.
The idea is that we're recursively calling tallest with other_layers both times. Wouldn't it be nice if we could call it once, and have all the information we need?
What information do we need? Well, if we had the tallest cake using the remaining layers for any base size, we'd be set: we just pick the tallest cake that can fit on the current layer, and see if that makes an improvement versus the tallest cake overall. But here's the trick: even if it doesn't make an improvement, we may still gain information. The idea is to have a list of the most "efficient" (smallest base) cakes for each size.
Our process will therefore be as follows:
Set up a list of cakes, with one cake in it that has zero layers.
# There will be, at all times, one cake in the list of any given height.
# Starting at zero instead of one makes the iteration neater.
For each layer on the conveyor belt, working **backwards** from the last:
Find the tallest cake in the list that fits on this layer.
Construct the cake 'c' consisting of that cake on top of this layer.
If there is already a cake in the list of the same height as 'c':
If the other cake has a smaller base, throw 'c' away. # It didn't help.
Otherwise, remove the other cake from the list. # 'c' is better.
If we still have 'c', add it to the list.
The tallest possible cake for the input is now the tallest one in the list.
This input sequence is tricky:
10 45 25 40 38 20 10 32 25 18 30
A simplistic approach that only skips lead-in layers would find these [cakes]:
[10] 45 25 40 38 20 10 32 25 18 30
10 [45 25] 40 38 20 10 32 25 18 30
10 45 [25] 40 38 20 10 32 25 18 30
10 45 25 [40 38 20 10] 32 25 18 30 <-- naive tallest, 4
10 45 25 40 [38 20 10] 32 25 18 30
10 45 25 40 38 [20 10] 32 25 18 30
10 45 25 40 38 20 [10] 32 25 18 30
10 45 25 40 38 20 10 [32 25 18] 30
10 45 25 40 38 20 10 32 [25 18] 30
10 45 25 40 38 20 10 32 25 [18] 30
10 45 25 40 38 20 10 32 25 18 [30]
The rules of the game allow you to skip any layer, though, not just the lead ones, and so the correct tallest cake in this case would be:
10 [45] 25 [40] [38] 20 10 [32] [25] [18] 30
Or written out with only the selected layers:
45 40 38 32 25 18
The problem you're trying to solve is a dynamic programming question (albeit a simple one).
Algorithm
public static int findMaxHeight(int[] layers) {
int[] max = new int[layers.length];
for(int i=layers.length - 1; i >= 0; i--) {
int localMax = 0;
for(int j=0; j < layers.length; j++) {
if(layers[j] < layers[i]) {
if(max[j] > localMax) {
localMax = max[j];
}
}
}
max[i] = localMax + 1;
}
int height = 0;
for(int i=0; i < max.length; i++) {
if(max[i] > height) {
height = max[i];
}
}
return height;
}
Step By Step
As a step through of how this works, consider:
8 16 12 6 6 10 5
Since we are going in reverse order,
5 10 6 6 12 16 8
Starting with 5, there are smaller than 5 values from []:
5 10 6 6 12 16 8
1
From [5], max[5] = 1 so 1+1
5 10 6 6 12 16 8
1 2
And so on...
5 10 6 6 12 16 8
1 2 2 3 4 5 4
Then we find out max of the list [1, 2, 2, 3, 4, 5, 4], which is 5.
And, as explained above, this is the correct answer to the provided example that he stepped through in the problem description.
How It Works
The algorithm works by taking saving the maximum value for each layer. The question explains that, for any given layer, it can stack only cakes less than or equal to its diameter. Therefore, the maximum of any given layer will always be the maximum of a layer of equal or less size which follows it on the belt plus 1 (counting the layer itself). If there are no layers that can be stacked onto it, we know that the max for this layer is 1.
Actually is very simple, it goes like this:
int[] layers = new int[] {x1,x2,x3...xn};
int[] count = new int[layers.length];
for(int i = 1; i < layers.length; i++)
{
for(int j = i+1 ; j < layers.length; j++)
{
if ( layers[j] >= layers[i]) count[i]++;
}
}
answer = Collections.max(Arrays.asList(count));
I have similar problem with little changes, which is:
I have text file contain large number of lines with different sizes "i.e. not all lines has same length"
each line contain integers only.
as an example A.txt =
4 6 4 1 2 2 5 7 7
0 9 5 5 3 2 43 3 32 9 0 1 3 1
3 4 5 6 7 4
34 5 8 9 0 7 6 2 4 5 6 6 7 5 4 3 2 21 4 9 8 4 2 1 5
I want to put these integers into an array so each integer will be an element in the array and saving lines from "overlapping" i.e. I need to keep each line as it is.
Could anybodyy help me with this?
a = dlmread('a.txt')
a =
Columns 1 through 21
4 6 4 1 2 2 5 7 7 0 0 0 0 0 0 0 0 0 0 0 0
0 9 5 5 3 2 43 3 32 9 0 1 3 1 0 0 0 0 0 0 0
3 4 5 6 7 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
34 5 8 9 0 7 6 2 4 5 6 6 7 5 4 3 2 21 4 9 8
Columns 22 through 25
0 0 0 0
0 0 0 0
0 0 0 0
4 2 1 5
I would do the following:
1) Create a new Array for each line
2) Read lines, one at a time from file
3) Split each line by the "space" char
4) Iterate through the String[] that you get from the split operation, passing each value to Integer.parseInt(value);
5) Store value in array;
6) When reading next line, create new array to store the new lines values.
You can read the data using Scanner, one line at a time, and store the numbers in a List, e.g. an ArrayList:
import java.util.*;
import java.io.*;
public class Numbers
{
public static void main(String[] args) throws FileNotFoundException
{
Scanner data = new Scanner(new File("A.txt"));
List<List<Integer>> ints = new ArrayList<List<Integer>>();
while (data.hasNextLine()) {
List<Integer> lineInts = new ArrayList<Integer>();
Scanner lineData = new Scanner(data.nextLine());
while (lineData.hasNextInt()) {
lineInts.add(lineData.nextInt());
}
ints.add(lineInts);
}
System.out.println(ints);
}
}
This code opens the file for reading, and creates a two-dimensional ArrayList. The outer list contains a list for each line in the file. The inner lists contain the integers on the respective lines. Note that empty lines result in empty lists. Also, you will have to properly handle any IO exception, unlike the code shown above.
If you really want the integers in a two dimensional array instead of an ArrayList, then you'll either have to call toArray, or alter the code above. That is left as an exercise for the reader.