hey guys so i am having a problem in my assinghmnet . i am making a text file and it has to import to java and make a table from the text file .
so there is what the txt file looks.
3 // this should be the rows 1,2,3,
3 // this should be the col 1,2,3,
5
1 2
3
2 2
3 2
0 0// this should exit it//
so , how do i make write a code that would read this text file and make it like a table?
thank you in advance

Dani AI

Generated

Format and assumptions
The input in the example can be interpreted as: first int = number of rows, second int = number of columns, third int = number of generations, then a sequence of live-cell coordinates as integer pairs terminated by the sentinel pair 0 0 (the sample comment shows this). The example has a few malformed lines (single integers on a line) — treat those as input errors or noise. Coordinates look 1‑based in the sample, so convert to 0‑based indices when filling an array.

Robust parsing recipe (token-oriented)
As suggested, prefer token-based parsing rather than relying on exact line structure. Read the three header ints, allocate the grid, then read coordinate pairs until the sentinel. Do bounds checks and ignore or report out-of-range pairs. Example approach:

Scanner in = new Scanner(new File(fileName));
int rows = in.nextInt();
int cols = in.nextInt();
int generations = in.nextInt();
boolean[][] grid = new boolean[rows][cols];

while (in.hasNextInt()) {
    int r = in.nextInt();
    int c = in.nextInt();
    if (r == 0 && c == 0) break;
    if (r >= 1 && r <= rows && c >= 1 && c <= cols) grid[r-1][c-1] = true;
    // else log or ignore malformed coordinate
}
in.close();

Handling comments and malformed input
If lines may include comments like // this should exit it, strip text after // before tokenizing, or read tokens and ignore non-integers. If an odd number of integers appears, detect the condition and fail gracefully rather than producing misaligned coordinates.

Game of Life processing notes
Use two boolean buffers (current and next) and swap each generation. For each cell count neighbors with offsets -1..1 (skip the center), apply standard rules (alive stays if 2 or 3 neighbors; dead becomes alive if exactly 3). Decide edge behavior (fixed bounds with checks, or wrap-around) and implement accordingly.

Context tie-ins
’s observation about single-number lines is valid — defensive parsing is needed. ’s CSV suggestion is fine for viewing the grid but not required for this compact input format.

Recommended Answers

All 6 Replies

Think about it and come up with something yourself. We need to have proof of your efforts in order to help you.

Show us what you've done so far and we'll help you from there...

This is what i have done till now.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;



public class gol {

public static void main(String [] args) throws FileNotFoundException {


    String fileName = args[0];

    Scanner txtfile= new Scanner(new File(fileName));
    System.out.println("Input file: "+fileName+"\n");





         }
     }

Okay, that's good... now to read from that file line-by-line, you can do something like:

String line = "";
while (txtFile.hasNextLine()) {
    line = txtFile.nextLine()
}

But, now think of how you'd read the data to facilitate your problem. Maybe after reading a particular line, you would like to read the different "tokens" in that line. Guess what, that Scanner class you're using has something for reading different tokens in the same line too. Scanner API

Also, just to be clear, the 3rd line of the text file is what? Is it the item in the (0,0) position of the table?

i am making a program game of life and 5 is number of generations. others are the coordinates and 00 is an exit.

If you want table like structure,You can create CSV(comma seperated value file) file and open it with Microsoft Excel,it will look like tabular form.

you can read line by splitting it with space and accordingly put it into new file.


I am unable to understand the game program.
Hence Please provide the complete information (clearly and more elaborately if you feel so).
Please dont give the information in pieces. Also dont wait until other forum members ask you the missing information in your problem.

How is generation in your problem related to line numbers & column numbers.

So you have given 4 types of inputs
1)line numbers , 2)column numbers, 3)generation 4)co-ordinates

You have not informed here how the game is played.

What are the game rules.

how are these inputs related?

The value at at line 5 is 3 , What does this mean ?(is it (3,0)?)
You said every thing after line 3 are co-ordinates ? then how come only one value at
line 5 instead of two.

Once again can you provide the complete and full information of your game.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.