ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi everyone hope you all enjoying some quality home time at the holiday, my question relates to using java on my slackware installation. I have a class that opens a file and reads data from a text file and then inputs that data into an array. However, i have just migrated from windows to linux and i cannot seem to get the file to read due to the incorrect way in which i have named the file and its path. I have a file called data1.txt and its path is /home/progr/data/data1.txt and as you can see from the code below i have used the same path and filename in the file parameter, however, the filename is not recognised on the compile and i am trying to figure out if i have named this correctly or if there are some other issues here which i have not thought about. Any help would be greatly appreciated.
import java.util.*;
public class EasyReader{
public static void main(String[] args){
EasyReader console = new EasyReader();
final int MAX = 100;
int [] values = new int[MAX];
int [] numberValues = new int[10];
int logicalLength;
logicalLength = fillArray(numberValues);
System.out.print(logicalLength);
}// End Main
public static int fillArray(int []numberValues){
EasyReader File23 = new EasyReader("/home/progr/data/data.txt");
int LL = 0;
int xy= File23.readInt();
while (!File23.eof())
{
numberValues[LL] = xy;
LL++;
xy = File23.readInt();
}// End while
File23.Close();
return LL;
}// End
}// End Class
The second part of the question is if i have a comma separated text file that contains multiple ciolumns of data how do i read the data from the file and place the data in a multidemional array ?
In your code, I don't see a constructor that takes a String, but you're using one in your fillArray() method. Could you post the exact compiler error please? Also, put code in [code] tags.
The second part of the question is if i have a comma separated text file that contains multiple ciolumns of data how do i read the data from the file and place the data in a multidemional array ?
Look at the string tokenizer class, this class is used to break strings into tokens based on a delimiter, in your case, commas
Code:
import java.util.StringTokenzier;
String str = "foo,bar,gork";
StringTokenizer tok = new StringTokenizer(str,",");
while(tok.hasMoreTokens())
{
System.out.println(tok.nextToken());
}
would print:
foo
bar
gork
you would just need to add logic to place the string into an array instead of printing them, hope this get you on the right path.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.