Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class Lab09_Q1_Yuksel {
- public static void main(String[] args) throws IOException {
- Scanner scan= new Scanner(System.in);
- ArrayList<String> users=new ArrayList<String>();//creating arraylist for users
- ArrayList<Integer> userid=new ArrayList<Integer>();//creating arraylis for users s id
- ReadFile(users, userid, "users.txt");//reading the file
- int count=BubbleSort(users, userid);//calling the bubble sorting method and sorting in alphabetic order
- PrintLists(users, userid);//printing the bubble sorted users and ids
- System.out.println("---------\n"+"Sorting...\n" + "Input size (N) was: " + users.size()+"\n"+"Sorting ended after " + count+ " operations");//printing the sorting part
- System.out.print("Enter userName to query: ");//take name from user to search
- String name=scan.next();
- int index=LinearSearch(users, name );//Searching the given name
- 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
- System.out.println("--------\n"+"Name: "+ users.get(index)+"\n"+"ID: "+userid.get(index));
- }
- public static void ReadFile( ArrayList<String> userNames, ArrayList<Integer> userIDs, String fileName) throws IOException{
- Scanner fscan=new Scanner(new File(fileName));
- int count=0;
- while(fscan.hasNextLine()){//scanning the file
- userIDs.add(fscan.nextInt());//storing the id numbers in one arraylist
- userNames.add(fscan.next());//storing the user names in one arraylist
- count++;
- }
- System.out.println("ArrayLists are populated. "+ count+" total users are inserted.");
- }
- public static int BubbleSort( ArrayList<String> userNames, ArrayList<Integer> userIDs){ //sorting the names in alphabetic order
- int count1=0;
- boolean sorted = false;
- for (int j = 0; j < userNames.size() - 1 && !sorted; j++)//sorting every name
- {
- sorted = true;
- for (int k = 0; k < userNames.size() - j - 1; k++)
- {
- count1++;
- if (userNames.get(k).compareTo(userNames.get(k+1)) > 0)
- {
- sorted = false;
- // Swap the current and next elements.
- String temp = userNames.get(k);
- int num= userIDs.get(k);
- userNames.set(k, userNames.get(k+1));//changing the places of the user names to get alphabetic order
- userIDs.set(k,userIDs.get(k+1));//changing the users according to the user name
- userNames.set(k+1, temp);//settin the changes
- userIDs.set(k+1,num);
- }
- }
- }
- return count1;
- }
- public static void PrintLists( ArrayList<String> userNames, ArrayList<Integer> userIDs){//printing the new sorted file
- for(int i=0; i<userNames.size()-1;i++){
- System.out.println(userNames.get(i)+" "+ userIDs.get(i));
- }
- }
- public static int LinearSearch( ArrayList<String> userNames, String name){
- for (int i=0; i < userNames.size(); i++) {//Searching for the given name
- if (name.equals(userNames.get(i))) {
- return i;
- }
- }
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement