6 Replies - 3071 Views - Last Post: 18 October 2007 - 03:56 PM Rate Topic: -----

#1 siscion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-October 07

File I/O & Scanner Class

Posted 18 October 2007 - 01:47 PM

Hello everyone =D,

Before I indefinitely have to post my code for your help, I would like to ask a few questions. My assignment is as follows:

If they select to search for current employee information, the system should run the employee directory system from Homework #2 using the already determined input file. If the user chooses to add a new employee, the system should allow the user to enter information similarly to Homework #1’s input and save this new employee information into the input file. Finally, if the user selects to run the payroll for the employees, the system should calculate the deductions and net pay for every employee depending on their type (Net Weekly for Casual and Net Monthly for Full-time) in the input file and output the results to the output file chosen by the user.


So far, I have welcome the user to the system, then open the JFileChooser and you can choose the txt file. Then it prompts the user with the 4 options.


So my question(s) are -

How exactly do I go about calling the information from the determined input file?

Detailed - According to my homework, I have a txt file already made up by them ( which I do ). Now, I have to somehow get the information in the txt file into the program. If you select option 1 ( Search for Employee ), for example, after typing in their name / phone number / employee id, it is supposed to bring up their information from the txt file. Java confuses me greatly, and I really don't know how to do that. Our lab TA said a scanner class would be the best way to do it over the tokenizer method, but I dont know how to use a scanner at all >.>

We are also supposed to be able to add new employee's, so how would I get that to save to the file?

Detailed - According to my homework, if the user chooses to add a new employee, the system should allow the user to enter information in the same manner we did it in our very first project. So after the enter the information for the so called new employee, how do I get it to save to the same txt file I'm using to search the directory with and call up the payroll with?

If you want to see my code, I can post snippets of it but in reality I do not have a lot in the main program yet. Thanks for the help. I've spent awhile looking online for tutorials and the majority of them imho have been complete crap. I've yet to find anything related in anyway or similar in anyway that can possibly help me.

Thanks again ^^

Is This A Good Question/Topic? 0
  • +

Replies To: File I/O & Scanner Class

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3911
  • View blog
  • Posts: 11,448
  • Joined: 18-April 07

Re: File I/O & Scanner Class

Posted 18 October 2007 - 02:33 PM

First off I want to say that your TA is a very smart person and the advice they gave you is really a nice way of doing it. The scanner class is basic and great for the newbie to learn all about. You can read about it at the following URL:

Java SE documentation - Scanner Class

The basic idea is that you first import the package to get access to this object. Then you load it with a filename to open. You can find it in the package java.util and you include it by putting this at the top of your file along with the java.io package for file manipulation like so...

import java.util.Scanner;
import java.io.*;



Not too bad right? So far so good. So now we have to load the scanner with the path to your file. We first create a file object with it and then pass that to the scanner like so...

Scanner myscanner = new Scanner(new File("c:\\directory\\filename.txt"));



Got it so far? We are now connected to the file and it is open, so we then can start reading from the file using Scanner's methods like nextLine() to read a line from the file. I suggest you play with this a little and use the documentation I showed you above to see if you can first get it to read the files. The trick here is to use read until you hit the end of the file. An example of reading the file is below...

import java.util.Scanner;
import java.io.*;

public class readtestfile {

	public static void main(String args[]) {
		// We have to put this in a try catch because the Scanner object may throw a file not found error.
		try {
			// Setup our scanner to read the file at the path c:\test.txt
			Scanner myscanner = new Scanner(new File("c:\\test.txt"));

			// Keep looping through the file while there is data to be read.
			while (myscanner.hasNextLine()) {
				// Read the line and show it on screen.
				System.out.println(myscanner.nextLine());
			}

			// Close the scanner
			myscanner.close();
		}
		catch (FileNotFoundException ex) { System.out.println("Error: " + ex.toString()); }
	}
}



The comments show you the different parts of reading a file. Notice that we setup the scanner, check to make sure there is a line to read and then read the line and show it on screen. After we hit the end of the file, we close it.

Now the second part is going to be reading in each employee's information and parsing the string. Your TA was pretty smart to recommend a tokenizer. I think it might be a bit too much for a beginner, but look it up to see how it works. It essentially splits a string into pieces (like a sentence into individual words).

But before you do that, I recommend building an employee class (it would be very simplistic) and then store the employees each in an array. You create a new one and add it to the array. Then you can save this array of employees to file when you are all done. I am not sure that is what the instructor had in mind or if they wanted you to write directly to the file each time.

But anyways, that is the next step of the process. Play around with the scanner a little and see if you can get it to read your input file. Then come back when you got things working.

Enjoy!

"At DIC we be super-human coding monsters disguised as geeks!" :alien:
Was This Post Helpful? 0
  • +
  • -

#3 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1818
  • View blog
  • Posts: 20,237
  • Joined: 17-March 01

Re: File I/O & Scanner Class

Posted 18 October 2007 - 03:02 PM

Quote

"At DIC we be super-human coding monsters disguised as geeks!"


LOL... nice touch Martyr2 :)
Was This Post Helpful? 0
  • +
  • -

#4 siscion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-October 07

Re: File I/O & Scanner Class

Posted 18 October 2007 - 03:25 PM

I appreciate the fast and thorough response, but I am definitely retarded when it comes to Java >.> - Here is what I have in my main project atm ( Employee System ).

// import statements
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.util.Scanner;

public class EmployeeSystem  {
	
	 
	//Variables								
	private int userChoice;  //Stores the user's choice of 1 - 4 when prompted   
	 
	/** Constructor
	 *  initialize all private data members to appropriate values
	 *  If you add more data members, you will have to initialize them here
	 *  too.
	 */
	   
	 public EmployeeSystem()  {
		
	this.userChoice = 0;
		
	}  // end constructor
	 
	 
	/**
	 * This method displays a welcome message to the user.
	 */
	 public void runSystem(){
	 
	   if (userChoice == 1) {
		   
			System.out.println("Please enter all or part of the employee name, employee ID, or employee phone number to view the employee information: ");
		}
		
		else{
		   
			System.out.println("test");
		}
		} 
	  
	public void displayWelcome()  {
		
		/* Display a brief welcome message about the system */
		
		System.out.println("Welcome to CSE Employee System");
		System.out.println("You can view employee information, add employees, and run the payroll using this system. ");
		
	}  // end displayWelcome
	
	/**
	 * This method displays a goodbye message to the user.
	 */
	public void displayGoodbye()  {
		
		/* Display a brief goodbye message from the system */
		  
		  System.out.println("Thank you, goodbye!");
				  
	}  // end displayGoodbye
	
	/**
	 * This method obtains the employee choice from the user.  It calls
	 * readInteger() to obtain the choice.  "1" means "Search Database" 
	 * employee, "2" means "Add" employee, "3" means "Run Payroll", and "4" is "Quit.
	 * Then it stores the choice in the userChoice variable
	 * Note that this method does not return anything.
	 */
	  
		public void obtainUserChoice()  {
		System.out.println("Please select one of the following options: ");
		  System.out.println("1. Search Current Employee Information");
		  System.out.println("2. Add New Employee Information");
		  System.out.println("3. Run Payroll for Employees");
		  System.out.println("4. Exit");
		  System.out.print("Choice: ");
		   		  
		// 2.  Call "readInteger()" to get the choice.
		  
		  userChoice = readInteger();
		  }

	
	/**
	 * This method reads an integer and
	 * returns that integer to the caller of this method.
	 */
	private int readInteger()  {
		
		int temp = 0;
		
		Scanner scanner = new Scanner(System.in);
		
		try {
			temp = scanner.nextInt();  // read integer
		}  catch (InputMismatchException ex) {
			System.out.println(ex);
		}
		
		return temp;
		
	}  // end readInteger
	
	/**
	 * This method reads in a string and returns that string to the caller of
	 * this method.
	 */
	private String readString()  {
		
		String userInput = "";
		
		Scanner scanner = new Scanner(System.in);
		
		userInput = scanner.nextLine();
		
		return userInput;
		
	}  // end readString

	/**
	 * This main method creates an object of the PayrollSystem class and runs
	 * the Payroll System. 
	 *
	 * First, it asks the PayrollSystem object to display a welcome message.  
	 * Then it asks the object to obtain the employee choice from the user.  
	 * Next, it asks the PayrollSystem object to run the payroll accordingly.  
	 * Finally, it asks the PayrollSystem object to display a goodbye message.
	 */
	  public static void main(String[] args)  {
		
	  EmployeeSystem mySystem = new EmployeeSystem();
		mySystem.displayWelcome();
		
		JFileChooser chooser = new JFileChooser("C:/ComputerScience/CS Project 3");
		chooser.showOpenDialog(null);
		
		mySystem.obtainUserChoice();
		mySystem.displayGoodbye();
									 
	}  // end main
}  // end Class PayrollSystem 


- I guess I don't quite understand how to implement that scanner code into my program and then test it. Also, when I load the scanner, what does that do exactly? Is that like what the JFileChooser does ( Maybe what I think the JFileChooser does is wrong, which it probably is =x ) and some of the commenting may be wrong because I had to copy this from my Homework 2 and haven't looked at the commenting thoroughly yet. It may be a hassle to you guys and I apologize, but me and java just aren't clicking yet =P
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3911
  • View blog
  • Posts: 11,448
  • Joined: 18-April 07

Re: File I/O & Scanner Class

Posted 18 October 2007 - 03:25 PM

I speak only the truth. Honest! No no really I do! Ok maybe I fibbed a little... I hate myself now. Please excuse me while I go design and code my own death.... something with C# should do nicely.
Was This Post Helpful? 0
  • +
  • -

#6 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3911
  • View blog
  • Posts: 11,448
  • Joined: 18-April 07

Re: File I/O & Scanner Class

Posted 18 October 2007 - 03:49 PM

View Postsiscion, on 18 Oct, 2007 - 03:25 PM, said:

I appreciate the fast and thorough response, but I am definitely retarded when it comes to Java >.> - Here is what I have in my main project atm ( Employee System ).

// import statements
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.util.Scanner;

public class EmployeeSystem  {
	
	 
	//Variables								
	private int userChoice;  //Stores the user's choice of 1 - 4 when prompted   
	 
	/** Constructor
	 *  initialize all private data members to appropriate values
	 *  If you add more data members, you will have to initialize them here
	 *  too.
	 */
	   
	 public EmployeeSystem()  {
		
	this.userChoice = 0;
		
	}  // end constructor
	 
	 
	/**
	 * This method displays a welcome message to the user.
	 */
	 public void runSystem(){
	 
	   if (userChoice == 1) {
		   
			System.out.println("Please enter all or part of the employee name, employee ID, or employee phone number to view the employee information: ");
		}
		
		else{
		   
			System.out.println("test");
		}
		} 
	  
	public void displayWelcome()  {
		
		/* Display a brief welcome message about the system */
		
		System.out.println("Welcome to CSE Employee System");
		System.out.println("You can view employee information, add employees, and run the payroll using this system. ");
		
	}  // end displayWelcome
	
	/**
	 * This method displays a goodbye message to the user.
	 */
	public void displayGoodbye()  {
		
		/* Display a brief goodbye message from the system */
		  
		  System.out.println("Thank you, goodbye!");
				  
	}  // end displayGoodbye
	
	/**
	 * This method obtains the employee choice from the user.  It calls
	 * readInteger() to obtain the choice.  "1" means "Search Database" 
	 * employee, "2" means "Add" employee, "3" means "Run Payroll", and "4" is "Quit.
	 * Then it stores the choice in the userChoice variable
	 * Note that this method does not return anything.
	 */
	  
		public void obtainUserChoice()  {
		System.out.println("Please select one of the following options: ");
		  System.out.println("1. Search Current Employee Information");
		  System.out.println("2. Add New Employee Information");
		  System.out.println("3. Run Payroll for Employees");
		  System.out.println("4. Exit");
		  System.out.print("Choice: ");
		   		  
		// 2.  Call "readInteger()" to get the choice.
		  
		  userChoice = readInteger();
		  }

	
	/**
	 * This method reads an integer and
	 * returns that integer to the caller of this method.
	 */
	private int readInteger()  {
		
		int temp = 0;
		
		Scanner scanner = new Scanner(System.in);
		
		try {
			temp = scanner.nextInt();  // read integer
		}  catch (InputMismatchException ex) {
			System.out.println(ex);
		}
		
		return temp;
		
	}  // end readInteger
	
	/**
	 * This method reads in a string and returns that string to the caller of
	 * this method.
	 */
	private String readString()  {
		
		String userInput = "";
		
		Scanner scanner = new Scanner(System.in);
		
		userInput = scanner.nextLine();
		
		return userInput;
		
	}  // end readString

	/**
	 * This main method creates an object of the PayrollSystem class and runs
	 * the Payroll System. 
	 *
	 * First, it asks the PayrollSystem object to display a welcome message.  
	 * Then it asks the object to obtain the employee choice from the user.  
	 * Next, it asks the PayrollSystem object to run the payroll accordingly.  
	 * Finally, it asks the PayrollSystem object to display a goodbye message.
	 */
	  public static void main(String[] args)  {
		
	  EmployeeSystem mySystem = new EmployeeSystem();
		mySystem.displayWelcome();
		
		JFileChooser chooser = new JFileChooser("C:/ComputerScience/CS Project 3");
		chooser.showOpenDialog(null);
		
		mySystem.obtainUserChoice();
		mySystem.displayGoodbye();
									 
	}  // end main
}  // end Class PayrollSystem 


- I guess I don't quite understand how to implement that scanner code into my program and then test it. Also, when I load the scanner, what does that do exactly? Is that like what the JFileChooser does ( Maybe what I think the JFileChooser does is wrong, which it probably is =x ) and some of the commenting may be wrong because I had to copy this from my Homework 2 and haven't looked at the commenting thoroughly yet. It may be a hassle to you guys and I apologize, but me and java just aren't clicking yet =P



I think I might need to know a little more info about what you can and can't do on this assignment. Are you allowed to design a class for employee? Have you done custom classes yet? Are you allowed to use an array? Have you done things like an array of employee classes for instance?

The idea is that you usually read in the file, create some of the employees using an employee class, assign them their information and store them in an array. Your search function would then go through the array looking for the name etc.

Then you would write it back to file when they chose save. :)
Was This Post Helpful? 0
  • +
  • -

#7 siscion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-October 07

Re: File I/O & Scanner Class

Posted 18 October 2007 - 03:56 PM

We have other classes - 2 Employee Classes ( 2 Different types of Employees ), a Payroll Class which goes along with adding the 2 different types of employees and then a Employee Directory class ( Searching for employees via part of their name, phone # or emp ID ).

As far as arrays go, no idea how to use them and we haven't gone over them in class.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1