Jump to content
Seriously No Politics ×

I Need Help Please With Java Program


Recommended Posts

Posted

Any Computer Programmers? 

 

I am trying to read a file txt into a 2d Array 

 

The name of the file is f1

 

6 4
5 14 3 7
0 -3 -8 -3
1 15 6 8
-1 -1 5 0
 
What I have so far 
 
import java.util.Scanner;
import java.io.*;
 
public class HelpPlease
{
  public static void main(String [] args)throws IOException
      {
          Scanner filereader = new Scanner(new File("f1.txt"));
           while(filereader.hasNext())
           {
              // an now I am clueless, please help 
           }
      }
}

 

Posted

I personally would use BufferedReader constructed from a FileReader, count the number of lines in the file via readLine and the maximum number of entries in each line (unless you know this already), then create the array. Looks like you can use Scanner constructed from the result of readLine() to easily get the numbers.

Posted

I personally would use BufferedReader constructed from a FileReader, count the number of lines in the file via readLine and the maximum number of entries in each line (unless you know this already), then create the array. Looks like you can use Scanner constructed from the result of readLine() to easily get the numbers.

 

I am a beginner in Java, I am a student that needs help. 

Posted

I would assume there are message boards out there for computer programmers where you might be more successful in getting help.

 

I'd ask my son (I don't know if he does Java or not), but he is extremely busy right now and if you are a student, it makes more sense to me to connect with a group that can give you ongoing help.  If I remember next time I talk to him, I will ask him if he knows of any such things…where he goes when he has a sticky problem for suggestions.

Posted

I am a beginner in Java, I am a student that needs help.

Is the program itself your assignment? If so, I can answer specific questions or give you some more hints, but I'm not going to write it for you.

Posted (edited)

Is the program itself your assignment? If so, I can answer specific questions or give you some more hints, but I'm not going to write it for you.

 

import java.util.Scanner;
import java.io.*;
 
public class Demo {
  public static void main(String [] args)throws IOException{
  
    Scanner filereader = new Scanner(new File("m2.txt"));
      while(filereader.hasNext()){
      String line = filereader.nextLine();
      System.out.println(line);
   
    String[] tokens = line.split(" ");
     
      int b = 4; // Number of Columns 
      int [][] ab = new int [tokens.length];
      for (int i = 0; i < tokens.length; i++)
      {
        ab = Integer.parseInt (tokens);
      }
  }
  }
  }
  
It compiles, but I know I am doing it wrong 
Edited by MormonFreeThinker
Posted

    while(filereader.hasNext()){

      String line = filereader.nextLine();

      System.out.println(line);

   

    String[] tokens = line.split(" ");

     

      int b = 4; // Number of Columns 

      int [][] ab = new int [tokens.length];

So after reading the first line of the file, you have an array of size [2][4] -- because tokens.length is the number of "words" in the first line. However, since you are allocating the array in the while loop, you are going to be creating a new 2D array for every line in the file, and at the end of the while loop you will be left with an array that would, in theory, only hold values for the last line read.

What you want to do instead is either a) read the file twice, once to count the number of lines and a second time to read the values or b) read the file once, store the values for each line in an ArrayList or other linked list and store those lists in an ArrayList (so you would have an ArrayList where each item is an ArrayList of Strings or Integers), and then create an array from the ArrayList.

    for (int i = 0; i < tokens.length; i++)

      {

        ab = Integer.parseInt (tokens);

      }

 

This will barf. Remember you set b = 4, and then allocated the array to be of size [tokens.length]. So ab does not exist for any i. The valid values are ab[0][0] to ab[tokens.length-1][3].

Posted (edited)

MormonFreeThinker!  RUN! DO NOT WALK! TO StackOverflow.com

 

That site is totally awesome for asking programming questions -- it has saved my programming bacon on many occasions!  Get a user id there, it's free.  Ask your questions there -- we have programmers here, but on SO you will be putting your question before hundreds and thousands of qualified coders!

 

I am Cyberherbalist on StackOverflow.

 

And the same invitation to SO goes out to all you programmers! :D

Edited by Stargazer
Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...