Results 1 to 2 of 2
Thread: Java methods
- 02-09-2013, 09:48 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 1
- Rep Power
- 0
Java methods
Hello i am new here. I have a few questions about a program i am writing. The methods I am writing are not working properly when executed in the main. The first method i wannted to address that is not working properly is search.. Here is the code thus far. It is
The search method when called in main is supposed to decide if the user id has been used already. After running the main and going through again entering the same user id does not throw out an error and it acts as if it has never been used even though it is a duplicate. There are more classes in this program and i can send them if you need me to. Thanks in advance for any help.Java Code:import java.util.*; public class tester { private static String fname, lname, tempID; private static Scanner keyboard = new Scanner(System.in); private static boolean found; private static DataStructure s = new DataStructure(5); public static void main(String[] args) { int response; Scanner keyboard=new Scanner(System.in); do { System.out.println(" 1 Add a new student"); System.out.println(" 0 End"); response=keyboard.nextInt(); switch (response) { case 1: addIt(); break; } } while (response!=0); } public static void addIt() { do { System.out.println("Enter a unique ID number to add"); tempID=keyboard.nextLine(); found = s.search(tempID); if (found = true) { System.out.println("ID already in use"); System.out.println("Please re-enter a unique ID"); } } while (found); System.out.println("Enter first name"); fname=keyboard.nextLine(); System.out.println("Enter last name"); lname=keyboard.nextLine(); s.add(fname,lname,tempID); } } public class DataStructure { private DataRecord[] data = new DataRecord[100]; private static String key = null; private static int nElems = 0; private static IndexRecord [] a; private static boolean found = false; public IndexRecord fName; public IndexRecord lName; public IndexRecord Id; public int max; public DataStructure(int maxSize) { max = maxSize; this.fName = fName; this.lName = lName; this.Id = Id; } public void display() // displays array contents { for(int j=0; j<nElems; j++) // for each element, System.out.print(a[j] + " "); // display it System.out.println(""); } public int compareID(String Id1, String Id2) { return (Id1.compareTo(Id2)); } public boolean search(String key) { int j; for(j =0; j<nElems; j++) { if(a[j].key.equals(key)) { System.out.println("Found"); found = true; } else { System.out.println("Not found"); found = false; } } return found; } public void add(String f, String l, String id) { int j; for(j=100; j<data.length; j--) { if(a[j].compareTo(a[j+1])>=0) { a[j+1] = a[j]; } else { break; } a[j+1] = a[data.length-1]; } } public String listIt(int x, int y) { if(x == 1 && y==1) { for(int i =0; i<100;i++) display(); } else if (x == 2 && y==1) { for(int i =0; i<100;i++) display(); } else if (x == 3 && y==1) { for(int i =0; i<100;i++) display(); } else if (x == 1 && y==2) { for(int i=100; i>=0;i--) display(); } else if (x == 2 && y==2) for(int i=100; i>=0;i--) display(); else if (x == 2 && y==2) for(int i=100; i>=0;i--) display(); return null; } }
- 02-10-2013, 10:36 AM #2
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
Similar Threads
-
Why and where abstract methods & classes and static methods used?
By ajaysharma in forum New To JavaReplies: 1Last Post: 07-12-2012, 11:04 PM -
Why and where abstract methods & classes and static methods are used?
By ajaysharma in forum Advanced JavaReplies: 2Last Post: 07-12-2012, 11:04 PM -
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
Java Noob, trying to call methods from another methods
By gabrielpr12 in forum New To JavaReplies: 8Last Post: 11-17-2011, 09:07 PM -
Incorporating If-Else into Methods + Private Helper Methods?
By 5minutes in forum New To JavaReplies: 1Last Post: 10-05-2011, 12:15 AM


LinkBack URL
About LinkBacks
Reply With Quote
.
Bookmarks