Not sure how to use ArrayList

The program is a simple text editor

Page 1 of 1

3 Replies - 1103 Views - Last Post: 09 May 2009 - 06:58 PM Rate Topic: -----

#1 Clockwork_ED  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-May 09

Not sure how to use ArrayList

Posted 08 May 2009 - 06:18 PM

Hi, I'm trying to create a simple text editor, but I not sure how to use the method set_curr_line to change the current line int he buffer to a integer. Then it would print out the new current line. I need a push on how to do this.

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import static java.lang.System.*;

class editfile {
static final String PROGNAME = "editfile";
static echoscanner stdin = new echoscanner ();

//
// Class Buffer to hold the lines and remember current line.
//
static class edit_buffer {
int current_line = 0;
ArrayList <String> lines = new ArrayList <String> ();
}

//
// Print usage message and exit.
//
static void usage () {
err.printf ("Usage: java %s [-e] filename%n", PROGNAME);
exit (1);
}

//
// Print an error message if the command is invalild.
//
static void bad_command (String command) {
out.printf ("%s: %s: invalid command%n", PROGNAME, command);
}



//
// Read file into a buffer, given the filename. The current
// line is set to the last line read in. If the file does not
// exit, just return, since a new file is being created.
//
static void read_file (String filename, edit_buffer buffer) {
File infile = new File (filename);
if (! infile.exists ()) return;
try {
Scanner input = new Scanner (infile);
while (input.hasNextLine ()) {
buffer.lines.add (input.nextLine ());
}
buffer.current_line = buffer.lines.size ();
input.close ();
}catch (IOException error) {
err.printf ("%s: %s%n", PROGNAME, error.getMessage ());
exit (1);
}
}

//
// Write buffer back to the file.
//
static void write_file (String filename, edit_buffer buffer) {
try {
PrintWriter writer = new PrintWriter (new File (filename));
for (String line: buffer.lines) writer.println (line);
writer.close ();
}catch (IOException error) {
err.printf ("%s: %s%n", PROGNAME, error.getMessage ());
exit (1);
}
}




static void STUB (String function) {
out.printf ("STUB function %s%n", function);
}

//
// Loop, editing the file, reading commands from stdin.
//
static void edit_file (String filename, edit_buffer buffer) {
for (;;) {
out.printf ("%s> ", PROGNAME);
if (! stdin.hasNextLine ()) break;
String command = stdin.nextLine ();
command = command.trim ();
if (command.length () == 0 || command.charAt (0) == '#') {
continue;
}
if (command.matches ("^\\d+$")) {
int set_line = Integer.parseInt (command);
STUB ("set_curr_line " + set_line);
}else if (command.length () != 1) {
bad_command (command);
}else {
switch (command.charAt (0)) {
case '*': STUB ("print_all_lines"); break;
case '.': STUB ("print_current_line"); break;
case 'a': STUB ("append_after_line"); break;
case 'd': STUB ("delete_current_line"); break;
case 'i': STUB ("insert_before_line"); break;
case 'q': exit (0);
case 'w': write_file (filename, buffer); break;
default: bad_command (command); break;
}
}
}
out.printf ("%n");
}


//
// Main function. Load file into buffer, then read and
// execute commands. Write file back at end of file.
//
public static void main (String[] args) {
edit_buffer buffer = new edit_buffer ();
String filename = null;
switch (args.length) {
case 1: if (args[0].startsWith ("-")) usage ();
filename = args[0];
break;
case 2: if (! args[0].equals ("-e")) usage ();
filename = args[1];
stdin.want_echo = true;
break;
default: usage ();
}
read_file (filename, buffer);
edit_file (filename, buffer);
write_file (filename, buffer);
}
}


This post has been edited by Clockwork_ED: 08 May 2009 - 06:39 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Not sure how to use ArrayList

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Not sure how to use ArrayList

Posted 08 May 2009 - 07:03 PM

Oud a lot of static stuff here not a good idea

Your main should just validate the input... argv[0]
and then call

new editfile(arg[0]);

read_fiile, edit_file and write_file should be editfile methods and should not be static
stdin in C++ stuff

and there is no GUI... you are making a console mode editor ? like in the 70ies ?
Was This Post Helpful? 0
  • +
  • -

#3 Clockwork_ED  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-May 09

Re: Not sure how to use ArrayList

Posted 08 May 2009 - 08:05 PM

View Postpbl, on 8 May, 2009 - 06:03 PM, said:

Oud a lot of static stuff here not a good idea

Your main should just validate the input... argv[0]
and then call

new editfile(arg[0]);

read_fiile, edit_file and write_file should be editfile methods and should not be static
stdin in C++ stuff

and there is no GUI... you are making a console mode editor ? like in the 70ies ?



[quote]
This has to be a very simple text editor that runs well. It will not have a GUI.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Not sure how to use ArrayList

Posted 09 May 2009 - 06:58 PM

OK then an ArrayList is the most useful tool to store your lines
It has a add(index, line); method that will allow you to insert lines where you need it

But who would want to write a console mode editor in 2009 ? :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1