Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.33 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class Lab09_Q1_Yuksel {
  5.        
  6.  
  7.    
  8.     public static void main(String[] args) throws IOException {
  9.         Scanner scan= new Scanner(System.in);
  10.         ArrayList<String> users=new ArrayList<String>();//creating arraylist for users
  11.         ArrayList<Integer> userid=new ArrayList<Integer>();//creating arraylis for users s id
  12.         ReadFile(users, userid, "users.txt");//reading the file
  13.         int count=BubbleSort(users, userid);//calling the bubble sorting method and sorting in alphabetic order
  14.         PrintLists(users, userid);//printing the bubble sorted users and ids
  15.         System.out.println("---------\n"+"Sorting...\n" + "Input size (N) was: " + users.size()+"\n"+"Sorting ended after " + count+ " operations");//printing the sorting part
  16.         System.out.print("Enter userName to query: ");//take name from user to search
  17.         String name=scan.next();
  18.         int index=LinearSearch(users, name );//Searching the given name
  19.         System.out.println("-------\n"+"Searching...\n"+ "Input size (N) was: "+users.size()+"\n"+"Search ended after " + (index+1)+ " operations" );//printing the linear searching and results
  20.        
  21.         System.out.println("--------\n"+"Name:  "+ users.get(index)+"\n"+"ID: "+userid.get(index));
  22.        
  23.        
  24.    
  25.        
  26.     }
  27.    
  28.     public static void ReadFile( ArrayList<String> userNames, ArrayList<Integer> userIDs, String fileName) throws IOException{
  29.        
  30.         Scanner fscan=new Scanner(new File(fileName));
  31.         int count=0;
  32.         while(fscan.hasNextLine()){//scanning the file
  33.             userIDs.add(fscan.nextInt());//storing the id numbers in one arraylist
  34.             userNames.add(fscan.next());//storing the user names in one arraylist
  35.             count++;
  36.            
  37.         }
  38.         System.out.println("ArrayLists are populated. "+ count+" total users are inserted.");
  39.        
  40.    }
  41.     public static int BubbleSort( ArrayList<String> userNames, ArrayList<Integer> userIDs){ //sorting the names in alphabetic order
  42.             int count1=0;
  43.            
  44.             boolean sorted = false;
  45.         for (int j = 0; j < userNames.size() - 1 && !sorted; j++)//sorting every name
  46.         {
  47.             sorted = true;
  48.             for (int k = 0; k < userNames.size() - j - 1; k++)
  49.             {
  50.                 count1++;
  51.                 if (userNames.get(k).compareTo(userNames.get(k+1)) > 0)
  52.                 {
  53.                     sorted = false;
  54.                     // Swap the current and next elements.
  55.                     String temp = userNames.get(k);
  56.                     int num= userIDs.get(k);
  57.                     userNames.set(k, userNames.get(k+1));//changing the places of the user names to get alphabetic order
  58.                     userIDs.set(k,userIDs.get(k+1));//changing the users according to the user name
  59.                         userNames.set(k+1, temp);//settin the changes
  60.                         userIDs.set(k+1,num);
  61.                    
  62.            
  63.                 }
  64.        
  65.             }
  66.                
  67.         }
  68.    
  69.     return count1; 
  70.  
  71.          
  72.     }
  73.        
  74.     public static void PrintLists( ArrayList<String> userNames, ArrayList<Integer> userIDs){//printing the new sorted file
  75.         for(int i=0; i<userNames.size()-1;i++){
  76.             System.out.println(userNames.get(i)+" "+ userIDs.get(i));
  77.         }
  78.        
  79.        
  80.     }
  81.     public static int LinearSearch( ArrayList<String> userNames, String name){
  82.        
  83.  
  84.          for (int i=0; i < userNames.size(); i++) {//Searching for the given name
  85.  
  86.          if (name.equals(userNames.get(i))) {
  87.            return i;        
  88.        
  89.          }
  90.       }
  91.      
  92.                        
  93. return -1;  
  94.  }  
  95.    
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement