Вы находитесь на странице: 1из 3

import java.io.

File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class phonebook {


static File file;
static FileReader read;
static Scanner scan;
static FileWriter write;

public static String[] Read(){


try {
file = new File("phonenumbers.txt");
read = new FileReader(file);
scan = new Scanner(read);
int row = 0;
String phoneNum[] = new String [100];

scan.nextLine();
while(scan.hasNext()){
phoneNum[row] = scan.nextLine();
// System.out.println(phoneNum[row]);
row++;
}
return phoneNum;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}

public static void BubbleSortAsc(String[] bubble){


for(int y=0; y<bubble.length; y++){
for(int x=0; x<bubble.length-1; x++){
int N1 = Integer.parseInt(bubble[x]);
int N2 = Integer.parseInt(bubble[x+1]);
if(N1 > N2){
String hold = bubble[x+1];
bubble[x+1] = bubble[x];
bubble[x] = hold;
}
}
}
try {
file = new File("asc.txt");
write = new FileWriter(file);
for(int x=0; x<bubble.length; x++){
write.write(bubble[x]+"\n");
}
write.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void BubbleSortDesc(String bubble[]){
for(int y=0; y<bubble.length; y++){
for(int x=0; x<bubble.length-1; x++){
int N1 = Integer.parseInt(bubble[x]);
int N2 = Integer.parseInt(bubble[x+1]);
if(N1 < N2){
String hold = bubble[x+1];
bubble[x+1] = bubble[x];
bubble[x] = hold;
}
}
}
try {
file = new File("desc.txt");
write = new FileWriter(file);
for(int x=0; x<bubble.length; x++){
write.write(bubble[x]+"\n");
}
write.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {


Scanner console = new Scanner(System.in);
String input;

String phoneNum[] = Read();


BubbleSortAsc(phoneNum);
BubbleSortDesc(phoneNum);
String Search[] = Read();
System.out.print("Search: ");
input = console.next();
Search(input, Search);

public static void Search(String num, String[] phone){


for(int x=0; x<phone.length; x++){
int len = phone[x].length();
for(int y=0; y<len; y++){
if(num.equals(String.valueOf(phone[x].ch
arAt(y)))){
System.out.println(phone[x]);
break;
}
}
}
}
}

Вам также может понравиться