|
-
Oct 11th, 2005, 10:53 AM
#1
Thread Starter
New Member
SIN program
I am making a SIN program but I have to include a merge sort and a binary search and it's not working for me. Can anyone help me with my program?
// This program is designed to verify if a SIN number is correct or not as well as a search to find a name and associate it
// with the S.I.N. number
// September 2005
import java.awt.*;
import hsa.*;
public class SIN3
{
//All variables
static Console consol; // The output console
static String[] socNumName = new String [30]; //Social insurance number and name from file
static String[] name = new String [10]; //Array for the names
static String[] sin = new String [10]; //Array for the SIN numbers
static TextInputFile f; //Input file
static int count = 0; //This is the count
static int countb = 0; //This is yet another count
static String sinSearch = " "; //Input for name to find
//This method is to load the file
public static void getFile ()
{
f = new TextInputFile ("SINwithnames.txt");
while (!f.eof ())
{
socNumName [count] = f.readString ();
count++;
}
f.close ();
} //getFile
//This method is to split the file into the names and the SIN numbers
public static void split ()
{
count = 0;
while (count < 10)
{
if (count == 0)
{
sin [count] = socNumName [0];
name [count] = socNumName [2] + ", " + socNumName [1];
}
else
{
sin [count] = socNumName [count * 3];
name [count] = socNumName [(count * 3) + 2] + ", " + socNumName [(count * 3) + 1];
}
count++;
}
} //split
//This method is to find the name in the array
public static int search ()
{
consol.println ("Please input the SIN number that you would like to verify and find the name for: ");
sinSearch = consol.readString ();
while (true)
{
if ((countb >= 10) || (sin [countb].equals (sinSearch)))
break;
else
countb++;
}
return countb;
} //search
//This method is to check to see if the digits are valid
public static void isCheckDigitValid ()
{
final int a = -48;
int n[] = new int [8], m = 1;
String first = sinSearch;
char[] s = first.toCharArray ();
for (int l = 0 ; l < 4 ; l++)
{
n [m] = (s [m] + a) * 2;
m += 2;
}
int odd = 0;
m = 1;
for (int l = 0 ; l < 4 ; l++)
{
if (n [m] < 10)
{
odd += n [m];
}
else
{
odd += (n [m] % 10) + 1;
}
if (l < 3)
{
m += 2;
}
}
int even = 0;
m = 0;
for (int l = 0 ; l < 4 ; l++)
{
even += s [m] + a;
if (l < 3)
{
m += 2;
}
}
int sum = odd + even;
int total;
total = 10 - (sum % 10);
if (total == a + s [8])
consol.println ("This is a vaid S.I.N!");
else
consol.println ("This is not a valid S.I.N!");
} //isCheckDigitValid
//This is to start the merge sort
public static void mergeSort (int numbers[], int temp[], int array_size)
{
m_sort (numbers, temp, 0, array_size - 1);
}
//mergeSort
//This is going to do the merge sort
private static void m_sort (int numbers[], int temp[], int left, int right)
{
int mid;
if (right > left)
{
mid = (right + left) / 2;
m_sort (numbers, temp, left, mid);
m_sort (numbers, temp, mid + 1, right);
merge (numbers, temp, left, mid + 1, right);
}
} //m_sort
//This is going to do the merge
private static void merge (int numbers[], int temp[], int left, int mid, int right)
{
int i, left_end, num_elements, tmp_pos;
left_end = mid - 1;
tmp_pos = left;
num_elements = right - left + 1;
while ((left <= left_end) && (mid <= right))
{
if (numbers [left] <= numbers [mid])
{
temp [tmp_pos] = numbers [left];
tmp_pos = tmp_pos + 1;
left = left + 1;
}
else
{
temp [tmp_pos] = numbers [mid];
tmp_pos = tmp_pos + 1;
mid = mid + 1;
}
}
while (left <= left_end)
{
temp [tmp_pos] = numbers [left];
left = left + 1;
tmp_pos = tmp_pos + 1;
}
while (mid <= right)
{
temp [tmp_pos] = numbers [mid];
mid = mid + 1;
tmp_pos = tmp_pos + 1;
}
for (i = 0 ; i <= num_elements ; i++)
{
numbers [right] = temp [right];
right = right - 1;
}
}
//merge
//binary search to find the sin number
static public int binarySearch (int[] array, int target)
{
int high = array.length, low = -1, probe;
while (high - low > 1)
{
probe = (high + low) / 2;
if (array [probe] > target)
high = probe;
else
low = probe;
}
if (low == -1 || array [low] != target)
return -1;
else
return low;
}//binarySearch
public static void output ()
{
if (countb >= 10)
{
consol.println ("Sorry, that number cannot be found");
}
else if ((count >= 0) || (count < 10))
{
consol.print ("The name is " + name [countb] + "\n");
}
}
public static void main (String[] args)
{
consol = new Console ();
getFile ();
split ();
mergeSort ();
binarySearch ();
output ();
isCheckDigitValid ();
} // main method
} // SIN class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|