LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-23-2007, 08:47 AM   #1
MarkSlack
LQ Newbie
 
Registered: Nov 2007
Posts: 9

Rep: Reputation: 0
File i/o in java on linux


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 ?
 
Old 12-23-2007, 10:13 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
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.
 
Old 12-25-2007, 03:32 PM   #3
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 12-25-2007, 08:52 PM   #4
mmattax
LQ Newbie
 
Registered: Feb 2006
Posts: 14

Rep: Reputation: 0
Quote:
Originally Posted by MarkSlack View Post

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.

Last edited by mmattax; 12-25-2007 at 08:54 PM.
 
Old 01-07-2008, 04:57 AM   #5
MarkSlack
LQ Newbie
 
Registered: Nov 2007
Posts: 9

Original Poster
Rep: Reputation: 0
thank you for your help and pointing me in the right direction, i have rewritten the routine and it works ok
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
File Upload Exception in Java with WebSphere[Linux platform] vm_devadas Programming 1 01-25-2007 02:00 PM
Java File Processing using Scanner; Can't get it to read file in running directory xemous Programming 2 09-26-2006 06:13 PM
Java SDK won't install, binary file not executable, yoper linux 2.6 kernel lopoetve Linux - Software 8 10-30-2004 03:03 AM
I have Suse Linux 8.0 installed on my system & I don't know how to compile Java file? dukehazord Programming 8 08-07-2004 10:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration