Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

linear search/string sorting

Status
Not open for further replies.

beauafric

Programmer
Sep 22, 1999
1
US
am supposed to Write a program that creates an<br>
Employee database (held in memory) by prompting the<br>
user for the information.<br>
Each entry in the database is composed of an<br>
employee<br>
name (a string) and an employee identification<br>
number(a number between 100 and 200). The database holds<br>
five<br>
entries. After the database has been created the<br>
program prints it out in name order. <br>
<br>
The problem i can't figure out is the ordering of the employee name. I currently get a bunch of errors. Am just one week old in java so am pretty much clueless...<br>
if someone could look at it and give me some guidance<br>
or some pointer it will be greatly appreciated.<br>
<br>
cheers<br>
<br>
<br>
import java.io.*;<br>
import java.util.Locale;<br>
import java.text.Collator;<br>
import java.text.CollationKey;<br>
<br>
<br>
class EmployeeException extends Exception<br>
{<br>
public EmployeeException(String msg)<br>
{<br>
super(msg);<br>
}<br>
}<br>
<br>
class Worker<br>
{<br>
public String name;<br>
public int number;<br>
<br>
public Worker()<br>
{<br>
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));<br>
try {<br>
System.out.print("Enter an Employee name:");<br>
name = keyboard.readLine();<br>
}<br>
catch (IOException iox) {<br>
System.out.println("I/O error");<br>
}<br>
<br>
try {<br>
System.out.print("Enter an Employee identification number:");<br>
number =Integer.parseInt(keyboard.readLine());<br>
}<br>
catch (IOException iox) {<br>
System.out.println("I/O error");<br>
}<br>
catch (NumberFormatException numx) {<br>
System.out.println("Num error");<br>
}<br>
<br>
if (number &lt; 100 ¦¦ number &gt; 200) { <br>
System.out.println("Identification number must be a number between 100 and 200");<br>
}<br>
}<br>
<br>
public void print()<br>
{<br>
System.out.print(name);<br>
System.out.print(" ");<br>
System.out.print(number);<br>
}<br>
}<br>
<br>
class Employees<br>
{<br>
private final static int MaxSize=5;<br>
private Worker[] staff;<br>
<br>
public Employees()<br>
{<br>
staff=new Worker[MaxSize];<br>
<br>
for (int i=0; i&lt;MaxSize; i++) {<br>
Worker newStaff=new Worker();<br>
staff=newStaff;<br>
} <br>
}<br>
<br>
public void sort(String[] input) {<br>
Collator c=Collator.getInstance(Locale.CANADA_FRENCH);<br>
CollationKey[] ckeys=new CollationKey[input.length];<br>
for (int i=0;i&lt;input.length;i++) {<br>
ckeys=c.getCollationKey(input);<br>
}<br>
<br>
CollationKey tempKey;<br>
for (int i=1;i&lt;ckeys.length;i++) {<br>
for (int j=0;j&lt;ckeys.length-i;j++) {<br>
if (ckeys[j].compareTo(ckeys[j+1]) &gt; 0) {<br>
tempKey=ckeys[j];<br>
ckeys[j]=ckeys[j+1];<br>
ckeys[j+1]=tempKey;<br>
}<br>
}<br>
}<br>
for (int i=0;i&lt;input.length;i++) {<br>
input=ckeys.getSourceString();<br>
}<br>
}<br>
<br>
public void print()<br>
{<br>
for (int i=0; i&lt; MaxSize; i++) {<br>
staff.print();<br>
System.out.println();<br>
}<br>
}<br>
}<br>
<br>
public class Lab2<br>
{<br>
<br>
public static void main(String args[])<br>
{<br>
Employees myStaff=new Employees();<br>
System.out.println();<br>
System.out.println("The Employee Database");<br>
myStaff.print(); <br>
}<br>
} <br>
<br>

 
Hi!<br>
<br>
I do not understand your program, only correct it (syntactically,<br>
not functionality). Here is the corrected Lab2.java:<br>
<br>
<br>
import java.io.*;<br>
import java.util.Locale;<br>
import java.text.Collator;<br>
import java.text.CollationKey;<br>
<br>
<br>
class EmployeeException extends Exception<br>
{<br>
public EmployeeException(String msg)<br>
{<br>
super(msg);<br>
}<br>
}<br>
<br>
class Worker<br>
{<br>
public String name;<br>
public int number;<br>
<br>
public Worker()<br>
{<br>
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));<br>
try {<br>
System.out.print("Enter an Employee name:");<br>
name = keyboard.readLine();<br>
}<br>
catch (IOException iox) {<br>
System.out.println("I/O error");<br>
}<br>
<br>
try {<br>
System.out.print("Enter an Employee identification number:");<br>
number =Integer.parseInt(keyboard.readLine());<br>
}<br>
catch (IOException iox) {<br>
System.out.println("I/O error");<br>
}<br>
catch (NumberFormatException numx) {<br>
System.out.println("Num error");<br>
}<br>
<br>
if ((number &lt; 100) ¦¦(number &gt; 200)) { <br>
System.out.println("Identification number must be a number between 100 and 200");<br>
}<br>
}<br>
<br>
public void print()<br>
{<br>
System.out.print(name);<br>
System.out.print(" ");<br>
System.out.print(number);<br>
}<br>
}<br>
<br>
class Employees<br>
{<br>
private final static int MaxSize=5;<br>
private Worker[] staff;<br>
<br>
public Employees()<br>
{<br>
staff=new Worker[MaxSize];<br>
<br>
for (int i=0; i&lt;MaxSize; i++) {<br>
staff=new Worker();<br>
} <br>
}<br>
<br>
public void sort(String[] input) {<br>
Collator c=Collator.getInstance(Locale.CANADA_FRENCH);<br>
CollationKey[] ckeys=new CollationKey[input.length];<br>
for (int i=0; i&lt;input.length; i++) {<br>
ckeys=c.getCollationKey(input);<br>
}<br>
<br>
CollationKey tempKey;<br>
for (int i=1; i&lt;ckeys.length; i++) {<br>
for (int j=0; j&lt;ckeys.length; j++) {<br>
if (ckeys[j].compareTo(ckeys[j+1]) &gt; 0) {<br>
tempKey=ckeys[j];<br>
ckeys[j]=ckeys[j+1];<br>
ckeys[j+1]=tempKey;<br>
}<br>
}<br>
}<br>
for (int i=0; i&lt;input.length; i++) {<br>
input=ckeys.getSourceString();<br>
}<br>
}<br>
<br>
public void print()<br>
{<br>
for (int i=0; i&lt; MaxSize; i++) {<br>
staff.print();<br>
System.out.println();<br>
}<br>
}<br>
}<br>
<br>
public class Lab2<br>
{<br>
<br>
public static void main(String args[])<br>
{<br>
Employees myStaff=new Employees();<br>
System.out.println();<br>
System.out.println("The Employee Database");<br>
myStaff.print(); <br>
}<br>
} <br>
<br>
<br>
<br>
Good luck. Bye, Otto.<br>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top