• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to read the file

 
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So at this stage, I am creating the grid, so first, I have to input the size of the grid like 4 and then input the grid
0 0 0 1
1 2 3 0
2 2 0 2
3 3 1 0

However, I want to read the grid from the file. For example, java test 4 < file.txt

Where 4 is the size of the grid and the file is my grid. Each time the user will enter the size of the grid and the file name of the grid.

Any suggestion on how to do it?

 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is your code working? If not, tell us what's happening. Are you getting error messages? (Post complete message if you are.)
 
Tomy Alexsander
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No everything is fine it is working but it is working only when I am entering the data however I want that the grid was read from the file. So the user no needs to enter the grid.
At the movementmoment, i am running it like this
4 -- the size of the grid.
0 0 0 1
1 2 3 0
2 2 0 2
3 3 1 0
9 -- the answareanswer

But i want to have something like java test 4 > grid.txt.

I have my grid in s separate txt file but i do know-how run the file

I tried to do this in this way but it does not work.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it would be easiest to have something like this:
and not try to deal with redirecting input. Would this work?
 
Tomy Alexsander
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately it does not work i am getting this error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2
       at hello.main(hello.java:10).

If I am running the second code which I send. Unfortunately, I need to do it in this way:
 
Tomy Alexsander
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tomy Alexsander wrote:Unfortunately it does not work i am getting this error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2
       at hello.main(hello.java:10).

If I am running the second code which I send. Unfortunately, I need to do it in this way:



I am running grid 3x3 but i also have 4x4. 12x12 and 30x20
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Opening text files with a Scanner is quite easy. Start by looking at the Java™ Tutorials, which will tell you how to create a Path object.
Look at the Scanner documentation, which tells you how to create a Scanner, and another Java™ Tutorials section, which tells you how to close the Scanner.

Yes, you can write the size of the grid in the file as its first entry.

myTextFile.txt wrote: 4
0 0 0 1
1 2 3 0
2 2 0 2
3 3 1 0

Yes, you can pass the size and the name of the file as command line arguments. You would appear to be doing that already. No, you can't use | or > as command line operators as you would on a Unix/Linux terminal.

Don't mix a Scanner and Integer.parseInt(). Use the Scanner's methods to get the results directly. It is unnecessary to use String#split() when you can pass each line to a second Scanner. Look through the documentation.

Your code is very difficult to read, with strange variable names like myObj and dp, and lots of + 1 and − 1s. Also, you shouldn't write so much code in the main() method.
I would use the Streams functionality of a Scanner or a BufferedReader myself.

There are a few spelling errors in your post; let me correct them for you.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can always pass > as a command line argument and ignore it by going from args[0] to args[2].
The Streams technique has the advantage that it will count all the tokens for you and give you arrays of exactly the right size.
 
Tomy Alexsander
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The size of the grid must be input by the user not in the file. File hold only the grid.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is obviously a requirement for an assignment; if you had that stipulation in a real‑life application, you would regard that as very error‑prone.
How are you going to deal with matrices with two different sizes, as you hinted earlier (30×20)?
 
Tomy Alexsander
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They will be all the time the same 30x30 or 40x40. We are not getting 30x20
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Tomy Alexsander
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was trying to do something like this but it does not work too.

 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are mixing Lines and Ints. Use hasNextInt().
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Worse yet, you used "scn" instead of "fileReader".

Got to pay attention to details.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Move your code to its own method.
 
Tomy Alexsander
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to solve this problem but unfortunately, i can not I tried to create a method but I am just doing something wrong.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We cannot help without see what you've tried. Please re-post.
 
Tomy Alexsander
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for example where you told me to remove and replace but some of them I do not understand

 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you run the program as
java test 4 grid.txt
args[0]=4
args[1]=gird.txt

There is no args[2].
 
Tomy Alexsander
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about the last one? // Should use try-with-resources.

 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Careful abouh lengths of lines.

Yes, you should use try with resources for everything except System.in System.out and System.err.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try-with-resources looks like this: Your variable names may differ. After the closing brace (}) the resource (in) is automatically closed.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic