4 Replies - 2648 Views - Last Post: 09 February 2010 - 09:00 PM Rate Topic: -----

#1 porsche_gt_1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-February 10

Reading in From a text file to different data types

Posted 09 February 2010 - 04:14 PM

For an assignment I have for my software engineering class our instructor decided to have us read in information from a text file and store them as a LinkedList, a HashSet, a TreeSet, a LinkedHashSet and a
PriorityQueue. This teacher also happens to NOT TEACH ANYTHING. I have been looking at the assignment for the last few hours and cannot seem to find a straight forward way to input a text file to these data types. Can anyone help me?
Is This A Good Question/Topic? 0
  • +

Replies To: Reading in From a text file to different data types

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,613
  • Joined: 27-December 08

Re: Reading in From a text file to different data types

Posted 09 February 2010 - 04:20 PM

You might consider using Scanner to read in the data. To add the data into these collections, use their add() methods. If you want more information on each structure, google "ClassName Java 6" and one of the first links should be a java.sun.com url, which is the API. It will contain the information you need to use the structures you are talking about.

For more information on Scanner, check out the following link:
http://java.sun.com/...il/Scanner.html
Was This Post Helpful? 0
  • +
  • -

#3 porsche_gt_1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-February 10

Re: Reading in From a text file to different data types

Posted 09 February 2010 - 05:24 PM

The teacher did give me the code in order to read in from the file. But I just don't know how to set up reading in from scanner to save in these data types. I have the data types being read into an Array already but that was taken from an in class example.
Was This Post Helpful? 0
  • +
  • -

#4 porsche_gt_1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-February 10

Re: Reading in From a text file to different data types

Posted 09 February 2010 - 06:58 PM

My teacher seems to go around the idea of teaching at times and I am not sure how to do the following for my assignment that is due tomorrow at midnight. I have looked at the following code for at least 6 hours and I am just stuck. I am not that great of a programmer but this is the last class that I will need to do programming in for my entire college career.

Given the following instructions I must make a program that does the following:

1. when the program in lab #2 runs and the user clicks on the “Cancel” button, the program
crashes. This is undesirable behavior.
2. if the user clicks on the “OK” button without entering any text to search for, the program
(incorrectly) lists all input lines as “matches” to what the user searched. This is also
undesirable.
Modify the program to specifically detect these two conditions using exceptions.
_____________________________________________________________________________________________
Store all of the information read from the file into a total of 6 lists: along with the ArrayList used in
the in-class exercise #2, use a LinkedList, a HashSet, a TreeSet, a LinkedHashSet and a
PriorityQueue. Print the contents of each list – how are the contents different between the lists?
When searching, search all 6 lists for the same key and print the result of the search from each list
separately.
In your finished program,
1. if the user clicks on the “Cancel” button, the program should throw an exception – use an
exception class (“MyException”) initialized with a message, stop taking any more search terms
and print an appropriate exception message.
2. If the user clicks on the “OK” button without entering any text to search for, the program
should stop taking any more search terms and print an appropriate message.
______________________________________________________________________________________________
package lab2;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.util.LinkedList;


/**
 * <p>Title: Main program for Lab #2</p>
 * <p>Description: CSIS 343 Lab on 
 *      Basic file I/O
 *      Java Collections
 *      Generics
 *      Exceptions
 *      Assertions
 * </p>
 * 
 * 
 *
 */
public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		/*
		 * Name of file that will be read in
		 */
		final String INPUT_FILE = "src/lab2/Presidents";

		ArrayList<String> list = new ArrayList<String>();
		
		
		/*
		 * Open a file and read each line of the file into a list
		 */

		try {
			Scanner input = new Scanner(new FileReader(INPUT_FILE));

			while (input.hasNext()) {
				String line = input.nextLine();
				list.add(line);
			}

			input.close();

		} catch (FileNotFoundException f) {
			System.err.println("Could not open the file: " + INPUT_FILE);
		}

		assert (!list.isEmpty());

		/*
		 * Print the contents of the file that was just read in.
		 */
		for (String President: list){
			System.out.println( President );
		}

		while (true) {
			/*
			 * Get user's search term -- this is what the user wants to search
			 * for in the file.
			 */
			String key = javax.swing.JOptionPane
					.showInputDialog("Enter a search term:");
			System.out.println("Searching for " + key + "...");
				
			if ( key == null){
			try {
  
            }
            catch(NullPointerException e) {
                System.out.println(
                    "Must Enter A Search Term");
           
			/*
			 * If the user clicks on the "Cancel" button, key will be null.
			 */

			/*
			 * Check if the user clicked on the "OK" button without entering any
			 * text for the key
			 */

			/*
			 * Search for key using a simple linear search
			 */
			boolean found = false;
			for (String i: list) {
				String x = (String) i;

				if (x.indexOf(key) >= 0) {
					found = true;

					if ( x.charAt(0) == 'R' )
						System.out.println( "Republican: " + x.substring(2, x.length()) );
					if ( x.charAt(0) == 'D' )
						System.out.println( "Democrat: " + x.substring(2, x.length()) );
				}
			} // end for
			if (!found) {
				System.out.println("  Search term was not found in the list");
			} else {
				System.out.println("... done!");
		 try
			{
			}
			catch(NullPointerException s)
			{
			throw new NullPointerException("You Entered No Search Term");
			}
			}
		}

	}
}}}



and
 package lab2;

/**
 * <p>Title: Example exception class for Lab #2</p>
 * <p>Description: 
 * This class is used in a CSIS 343 Lab on Exceptions
 *      and other topics
 * </p>
 * 

 *
 */
public class MyException extends Exception {
    /**
	 * Data members
	 */
	private static final long serialVersionUID = 1L;
	private String key;
    
	/**
	 * MyException default constructor
	 * 
	 */
	public MyException() {
	}

	public MyException(String arg0) {
		super(arg0);
	}

	public MyException(Throwable arg0) {
		super(arg0);
	}

	public MyException(String arg0, Throwable arg1) {
		super(arg0, arg1);
	}
	
	public MyException(String message, String k){
		super(message);
		key = new String( k );
	}

	public String getKey(){
		return key;
	}
	
		
}



Any Help Will Be Greatly Appreciated!

This post has been edited by porsche_gt_1: 09 February 2010 - 06:58 PM

Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,613
  • Joined: 27-December 08

Re: Reading in From a text file to different data types

Posted 09 February 2010 - 09:00 PM

You don't put any code in your try block, so how can anything be caught? I would actually put the input in the try block, then in the catch block, throw your MyException.
String key = javax.swing.JOptionPane .showInputDialog("Enter a search term:"); 
System.out.println("Searching for " + key + "..."); 
if ( key == null){ 
    try { } 
    catch(NullPointerException e) { 



Then for the condition when the user hits OK, but doesn't enter anything, use the String trim() method then test the length. That way, if they only enter whitespace and the length is 0 (stringName.trim().length() == 0), you know the user wants to terminate.

In terms of the contents of your Collections, some will be different b/c Sets only store unique elements, LinkedLists do not.

Also, please work on the indentations of your code. The further down I go, the harder it is for me to follow your logic. Thanks for helping us help you! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1