I've written a program here, the idea is to have an array store lines that the program reads from a text file. Here are my issues:
1) the add() method is not working. I don't know how to add the lines read from the textfile into the array.
2) I also would like for the array to only hold 51 elements at a time, that is, if a 52nd line (or a 53rd, 54th and so on) is read, the first line that was read in the textfile (which would be stored in s[0]) gets replaced by the line stored in s[1], the line stored in s[1] would be replaced by the line in s[2] and so on until s[51] is holding the newest line read.
public class Part6 {
* @param r
* @param w
* @throws IOException
*/
public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
String s[] = new String[51];
String line;
int n = 0;
while ((line = r.readLine()) != null) {
s.add(line);
n++;
}
}
/**
* The driver. Open a BufferedReader and a PrintWriter, either from System.in
* and System.out or from filenames specified on the command line, then call doIt.
* @param args
*/
public static void main(String[] args) {
try {
BufferedReader r;
PrintWriter w;
if (args.length == 0) {
r = new BufferedReader(new InputStreamReader(System.in));
w = new PrintWriter(System.out);
} else if (args.length == 1) {
r = new BufferedReader(new FileReader(args[0]));
w = new PrintWriter(System.out);
} else {
r = new BufferedReader(new FileReader(args[0]));
w = new PrintWriter(new FileWriter(args[1]));
}
long start = System.nanoTime();
doIt(r, w);
w.flush();
long stop = System.nanoTime();
System.out.println("Execution time: " + 10e-9 * (stop-start));
} catch (IOException e) {
System.err.println(e);
System.exit(-1);
}
}
}
Help with an array
Started by heat89, Sep 28 2010 05:55 AM
3 replies to this topic
#1
Posted 28 September 2010 - 05:55 AM
|
|
|
#2
Posted 28 September 2010 - 06:43 AM
1)
You add stuff to an array like
2)
do
3)
From nanotime to seconds is '*10e9', not '10e-9'
4)
an empty line is not null but will be an empty string "". Your while loop will continue forever.
You add stuff to an array like
s[0] = "string1"; s[1] = "string2";
2)
do
s[n] = line;
if (n == 50) {
n = 0;
} else {
n++;
}
3)
From nanotime to seconds is '*10e9', not '10e-9'
4)
an empty line is not null but will be an empty string "". Your while loop will continue forever.
#3
Posted 28 September 2010 - 06:47 AM
Thanks for the reply, a couple questions:
1) How would I add the string from my file being read though? I can't do s[0] = "string1" because the strings are being read from a file.
2) Wouldn't this replace the FIRST string read with the NEWEST string read? This shouldn't happen, the newest string read should be placed into s[51], not s[0].
1) How would I add the string from my file being read though? I can't do s[0] = "string1" because the strings are being read from a file.
2) Wouldn't this replace the FIRST string read with the NEWEST string read? This shouldn't happen, the newest string read should be placed into s[51], not s[0].
#4
Posted 28 September 2010 - 06:54 AM
heat89 said:
Thanks for the reply, a couple questions:
1) How would I add the string from my file being read though? I can't do s[0] = "string1" because the strings are being read from a file.
2) Wouldn't this replace the FIRST string read with the NEWEST string read? This shouldn't happen, the newest string read should be placed into s[51], not s[0].
1) How would I add the string from my file being read though? I can't do s[0] = "string1" because the strings are being read from a file.
2) Wouldn't this replace the FIRST string read with the NEWEST string read? This shouldn't happen, the newest string read should be placed into s[51], not s[0].
1) Isn't that what my number 2 is doing?
2) Okay i saw it wrong ^^
You'll need to manually loop trough the array and replace them all.
while (!(line = r.readLine()).equals("")) {
if(s[s.length-1] != null){
for(int i=0 ; i<s.length-1 ; i++){
s[i] = s[i+1];
}
}
s[s.length-1] = line;
}


Sign In
Create Account

Back to top









