Results 1 to 2 of 2

Thread: Java methods

  1. #1
    fronter is offline Member
    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0

    Default 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
    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;
       }
    }
    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.

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: Java methods

    Hi fronter,

    Check your code on line 33 .

    Regards.

Similar Threads

  1. Replies: 1
    Last Post: 07-12-2012, 11:04 PM
  2. Replies: 2
    Last Post: 07-12-2012, 11:04 PM
  3. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  4. Java Noob, trying to call methods from another methods
    By gabrielpr12 in forum New To Java
    Replies: 8
    Last Post: 11-17-2011, 09:07 PM
  5. Replies: 1
    Last Post: 10-05-2011, 12:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •