Login Register






Java Programming Problem filter_list
Author
Message
Java Programming Problem #1
Hi,

I am helping a friend with her Java Programming project. The program is simple, it will:

1. Display Employee Information
2. Can do Searching by EmployeeID

Here's the code I created and I am experiencing:
Code:
Exception in thread "main" java.util.NoSuchElementException at java.util.StringTokenizer.nextToken(StringTokenizer.java:349) at MyCoc.showAll(MyCoc.java:62) at MyCoc.main(MyCoc.java:22)

I also want to implement a search() method but I don't know how but I have something in mind.

1. Maybe I will make the items in the txt file to become an array and return the whole line with employee information if it finds a match in employee ID.

2. Will simply go through each line and return the whole line when a match is found.

Note: It's a separated value with _ or *

Here's my code:

Code:
import java.io.*; import java.util.*; import java.util.ArrayList; public class MyCoc { static String fileName = "Employee.txt"; public static void main(String[] args) { // TODO, add your application code //System.out.println("Hello World!"); int menuItem = -1; while (menuItem != 0){ menuItem = menu(); switch(menuItem){ case 1: showAll(); break; case 2: searchID(); break; case 3: break; default: System.out.println("Invalid Input. Please Try Again"); } } } static int menu(){ int choice; Scanner userChoice = new Scanner(System.in); System.out.println(""); System.out.println(" EMPLOYEE PROFILE "); System.out.println(" --------------------------------------------- "); System.out.println(" 1. Display Employees "); System.out.println(" 2. Search by ID "); System.out.println(" 3. ######## "); System.out.println(" 0. Exit "); System.out.println(" ********************************************* "); System.out.println(""); System.out.print("Please select a transaction to process: "); choice = userChoice.nextInt(); return choice; } static void showAll(){ //choice #1 String line; StringTokenizer y=new StringTokenizer(line,"_*"); String id=y.nextToken(); String lastname=y.nextToken(); String firstname=y.nextToken(); String position=y.nextToken(); String rank=y.nextToken(); String s=y.nextToken(); System.out.println(" Employee ID :"+id ); System.out.println(" Last Name :"+lastname ); System.out.println(" First Name :"+firstname ); System.out.println(" Position :"+position ); System.out.println(" Rank :"+rank ); System.out.println (""); System.out.println(" ********************************************* "); } static void searchID(){ } }

In the text file:
Code:
Jamie_Sullivan*2006-0031*Worker*2*350.00 John_Smith*2001-0001*Manager*3*450.00 Tim_Jones*2010-0008*Worker*1*260.00 Kimmy_Dora*2009-0021*Worker*2*350.00

with the format:

firstName_lastName*employeeID*rank*sallary

Every idea is welcome and appreciated. I hope someone will care to help. Thank you so much.

Reply

RE: Java Programming Problem #2
Why don't you use a database for real. Maybe Mysql, its very easy with that and efficient as well. Using files for these kind of operations is very bad. It has a lot of flaws.
[Image: OilyCostlyEwe.gif]

Reply

RE: Java Programming Problem #3
Hi XTRMS4NITY.

StringTokenizer is deprecated. You can read that in the API here: http://docs.oracle.com/javase/7/docs/api...nizer.html

Quote:StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

As you can see they also give you the solution.

Now about your problem.
I think the second solution you describe is a bit easier. Both are valid.

You can search for the ID while reading.
Read the file line by line. The Oracle tutorials cover this pretty well: http://docs.oracle.com/javase/tutorial/e.../file.html

With each line you read, check if it contains the ID.
e.g.

Code:
if(line.contains(id))

You can use the split method to extract the three parts of the line.

Code:
String employeeID = line.split("\\*")[1];

This splits the line into two parts, using * as delimiter, and returns an array with the two parts. The second part is the ID. The first part is firstname_lastname. With [1] we access the second value of the array (array indices start with 0).

Regards
Deque
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: Java Programming Problem #4
@Deque I still believe that using JDBC with a Database would be better than using a file for maintaining records. DB would provide security and many other features whereas File would cause problems and the make it complex. Using JDBC in Java is very easy too Smile
[Image: OilyCostlyEwe.gif]

Reply

RE: Java Programming Problem #5
(03-27-2014, 06:03 PM)Psycho_Coder Wrote: Why don't you use a database for real. Maybe Mysql, its very easy with that and efficient as well. Using files for these kind of operations is very bad. It has a lot of flaws.


Hi. Yes, I think so too, that it has a lot of flaws. However, it's just an exercise program that a friend of mine, has. They were told to use only txt file. Biggrin

Thank you so much for the idea, I really appreciate it. Smile

If only it's my project then I would take time to learn real database.

Thanks,
XTRMS4NITY

Reply

RE: Java Programming Problem #6
(03-27-2014, 09:06 PM)Deque Wrote: Hi XTRMS4NITY.

StringTokenizer is deprecated. You can read that in the API here: http://docs.oracle.com/javase/7/docs/api...nizer.html

Quote:StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

As you can see they also give you the solution.

Now about your problem.
I think the second solution you describe is a bit easier. Both are valid.

You can search for the ID while reading.
Read the file line by line. The Oracle tutorials cover this pretty well: http://docs.oracle.com/javase/tutorial/e.../file.html

With each line you read, check if it contains the ID.
e.g.

Code:
if(line.contains(id))

You can use the split method to extract the three parts of the line.

Code:
String employeeID = line.split("\\*")[1];

This splits the line into two parts, using * as delimiter, and returns an array with the two parts. The second part is the ID. The first part is firstname_lastname. With [1] we access the second value of the array (array indices start with 0).

Regards
Deque


Thank you so much Ma'am Deque, it's very precise. Smile

Reply

RE: Java Programming Problem #7
Ma'am @Deque: The searchID(); is working now, thanks to your help. Biggrin

Reply

RE: Java Programming Problem #8
(03-27-2014, 09:36 PM)Psycho_Coder Wrote: @Deque I still believe that using JDBC with a Database would be better than using a file for maintaining records. DB would provide security and many other features whereas File would cause problems and the make it complex. Using JDBC in Java is very easy too Smile

Depends on the context.
There are situations when textfiles are just fine.
In this case it would be better performancewise, but I knew that this is just an exercise for a beginner meant to teach file input and output. A real database would be overkill for a beginner.
(03-28-2014, 08:25 AM)XTRMS4NITY Wrote: Ma'am @Deque: The searchID(); is working now, thanks to your help. Biggrin

:Thumbs-Up:
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply