Need a little help getting in the right direction

Reading from a text file into an array

Page 1 of 1

3 Replies - 749 Views - Last Post: 03 April 2009 - 07:28 PM Rate Topic: -----

#1 rustcat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-April 09

Need a little help getting in the right direction

Posted 02 April 2009 - 02:00 PM

-Specifics:
- At least four classes will need to be implemented, though other classes may be used depending on your style,
note they are listed here in the best bottom-up order for the design:
- ArrayUtils.java : Contains three static utility methods, the first two being "void selectionSort(String[] array)"
and "int[] binarySearch(String[] array, String target)". These first two methods can be adapted from Deaton's
ArrayTools.java in his Excercise and Examples section on the class web site. The third method should be
"String[] concatArrays(String[] array1, String[] array2)". The concatArrays method should simple copy the
contents of 2 arrays into a new array that's exactly the correct size to hold the contents of those arrays -
ie there should be no "null" contents.
- ArrayBuilder : This class is responsible for loading a multi-line file of words separated by spacess. To do this,
each line should be split into an array using .split(" ") and then use the concatArrays() method to add the
new words to everything already loaded. (ie words = ArrayUtils.concatArrays(words, scanny.nextLine().split(" ") )
- ScriptReader : This class loads and executes a small script file containing the commands listed below. Because
come commands take parameters(arguements), each line will need to be split using split(" "). For example:
String[] lineArray = scanny.nextLine().split(" ");
if(lineArray[0].equals("clear")) {
clearAll();
} else if ....
- Driver.java : Only responsible for looping asking for script files to load as described below.
- On startup, the program must ask "Please input the name of the command file or 'q' to quit." or similar.
This will be the only command line input for the entire program, all other commands
will be from the "command file." You can think of it as a bare-bones script.
This should loop for each command file until quit is explicitly chosen, similar to before.
- Commands: (all the following commands will be read in from the "command file."
- display
- Display the entire array using Arrays.toString().
- load <file>
- Load the specified file line by line into the data array. After each line, the array should
be resized as we talked about last week.
- clear
- Reset all data back to its original state, as though the program had just started, but maintain
the current position in the commands file. (this is used by the commands file to separate tests, etc)
- sort
- sort should be an implementation of selection-sort.(p. 443-447)
- find <target word>
- find should be an implementation of a binary-search. If successful, the index that
the word is found should be displayed, as well as the total number of comparisons it took
to find that word. If it fails to find the word, it will display a failure message, and the
number of comparisons attempted. (p. 447-451) (the comparissons made and the index found are the two
elements of the returned int array)
- For simplicity, all data input files will contain only single words containing lowercase characters.
In addition, no data in input files will be duplicated.
- No Lists should be used for the algorithms, only arrays. In addition, the Arrays class sort and search
functionality should not be used. (note that Arrays.toString([yourArray]) is fine)

-Validity checking:
- The program should keep an internal state in some form, such that when an invalid command is performed,
suchas when a sort or find is attempted when there is no data, or a load has occurred since the last sort. The
best way to do this is to use an enum variable in your CommandInterpreter class(private enum States {EMPTY,
UNSORTED, SORTED}; private States state = States.EMPTY;} But any method that works is fine, as enums haven't
been covered.

import java.util.Arrays;

public class MainExamp implements Runnable{

	public static void main(String[] args) {

		new MainExamp().run();

	}

	public void run() {
		System.out.println("...---Now doing String method demos---...");
		doStringMethods();
		System.out.println("...---Now doing some sorting---...");
		doSort();
	}

	public void doStringMethods() {
		String strApple = "apple";
		String strBanana = "banana";
		String strCarrot = "carrot";

		String strOriginal = "I am the original string.";

		System.out.println(strBanana.equals(strApple));
		System.out.println(strBanana.equals(strBanana));
		System.out.println(strBanana.equals(strCarrot));
		System.out.println(strBanana.compareTo(strApple));
		System.out.println(strBanana.compareTo(strBanana));
		System.out.println(strBanana.compareTo(strCarrot));
		System.out.println(strOriginal.split(" "));
		System.out.println(Arrays.toString(strOriginal.split(" ")));
	}

	public void doSort() {
		String strOriginal = "I am a random assortment of strings and what have yous. Good day sir.";

		String[] unsorted = strOriginal.split(" ");
		String[] sorted = null;

		System.out.println(Arrays.toString(unsorted));
		sorted = sort(unsorted);
		System.out.println(Arrays.toString(sorted));
	}

	public static String[] sort(String[] unsorted) {

		String[] tokens = new String[unsorted.length];


		tokens = new String[] { "Good", "I", "a", "am", "and", "assortment",
				"day", "have", "of", "random", "sir.", "strings", "what",
				"yous." };
		return tokens;
	}
}



have a couple more heaps of code but im not sure how to structure the classes. Also have yet to learn how to read from a file into an array.

Is This A Good Question/Topic? 0
  • +

Replies To: Need a little help getting in the right direction

#2 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Need a little help getting in the right direction

Posted 02 April 2009 - 05:08 PM

And what is your problem ?
Code does not compile ?
You have run time error ?
Program behaviour is not expected behaviour ?
Was This Post Helpful? 0
  • +
  • -

#3 rustcat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-April 09

Re: Need a little help getting in the right direction

Posted 02 April 2009 - 09:39 PM

not sure how to read in files into an array, or how i should structure my classes.

This post has been edited by rustcat: 02 April 2009 - 09:39 PM

Was This Post Helpful? 0
  • +
  • -

#4 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Need a little help getting in the right direction

Posted 03 April 2009 - 07:28 PM

use Scanner to read the file, your teacher made it easy fo ryou and put it all on a seperate line. So you a while loop of <scannerName>.hasNextLine() and assign it to a string, then add the string into the array by .add(stringName)
Hope this helps!

use Scanner to read the file, your teacher made it easy fo ryou and put it all on a seperate line. So you a while loop of <scannerName>.hasNextLine() and assign it to a string, then add the string into the array by .add(stringName)
Hope this helps!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1