Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Dette programmet skal overføre listen med ord til en matrise og boblesortere innholdet
- import java.io.*;
- import javax.swing.JOptionPane;
- public class Boble
- {
- public static void main(String[] l) throws IOException
- {
- //vardeklarasjon
- String ord, fil, inn, hmm;
- char b;
- int t, i;
- FileReader fraFil;
- String[] list;
- boolean sjekk;
- fil = "liste.txt";
- fraFil = new FileReader(fil);
- ord = "";
- list = new String[100];
- hmm="";
- do
- {
- t = fraFil.read();
- sjekk = t==13 || t==10;
- if(sjekk)
- {
- if(ord != "")
- {
- list = nyArray.inn(list, ord);
- ord = "";
- }
- }
- if(!sjekk)
- {
- b = (char)t;
- ord = ord + b;
- }
- }while (t > 0);
- for(i = 0; i<list.length; i++)
- {
- for(t = i +1; t<list.length; t++)
- {
- if(list[t]!=null)
- {
- if(list[i].compareTo(list[t])>0)
- {
- String temp = list [i];
- list[i] = list[t];
- list[t] = temp;
- }
- }
- }
- }
- inn = JOptionPane.showInputDialog("Skriv inn ord");
- do
- {
- sjekk = Bin.sok(list, inn);
- if(sjekk)
- {
- inn = JOptionPane.showInputDialog("Ordet " + inn + " var i listen. Skriv inn nytt ord \n (Enter avslutter)");
- }
- if(!sjekk)
- {
- inn = JOptionPane.showInputDialog("Ordet " + inn + " var ikke i listen. Skriv inn nytt ord \n (Enter avslutter)");
- }
- }while (inn.compareTo(hmm)!=0);
- // boblesortering'
- fraFil.close();
- System.exit(0);
- }
- }
- class nyArray
- {
- public static String[] inn(String[] arr, String word)
- {
- int i;
- String word2;
- do
- {
- for(i = 0; i < arr.length; i++)
- {
- word2 = arr[i];
- if(word.equals(word2))
- {
- i = arr.length;
- }
- if(!word.equals(word2))
- {
- if(arr[i]==null || arr[i] =="" && i < arr.length)
- {
- arr[i] = word;
- i = arr.length;
- }
- }
- }
- }while (i<arr.length);
- return arr;
- }
- }
- class Bin
- {
- public static boolean sok(String [] org, String ord)
- {
- int b, t, m, i;
- b=0;
- t=0;
- for(i=0; i<org.length; i++) //finner siste element i matrisen som har innhold
- {
- if(org[i] == null || org[i]=="")
- {
- t = i;
- i = org.length;
- }
- }
- boolean sjekk2 = false;
- while(b<=t)
- {
- m = (t+b)/2;
- if(ord.compareTo(org[m])>0)
- {
- b = m + 1;
- }
- else if(ord.compareTo(org[m])<0)
- {
- t = m - 1;
- }
- if(ord.compareTo(org[m])==0)
- {
- sjekk2 = true;
- b = t + 1;
- }
- }
- return sjekk2;
- }
- }
Add Comment
Please, Sign In to add comment