Hey everyone, first time one here and i thought i would show my most recent university java assignment. I'm just looking for feedback... wanna see if i am developing good habits or bad.
the assignment is completed and marked by my uni so i am not looking for help.. they just don't really let me know my strong and weak points... ya know?
//Written and programmed by
//David Stevenson
//Feb 12, 2010
import java.util.Scanner;
import java.io.*;
public class StudentID
{
public static void main()throws IOException {
Scanner scan = new Scanner(System.in);
boolean answer = true;
int stuID;
int[] sID = new int[100];
System.out.println("Please enter a File Name: (eg. StudentID.txt)");
String file = scan.nextLine();
System.out.println("Please enter a File Header: ");
String header = scan.nextLine();
PrintWriter outputFile = new PrintWriter(file);
System.out.println("Please enter the number of student IDs to be entered.\n(For the assignment enter \'7\')");
int numStu = scan.nextInt();
for(int i = 1; i<=numStu;i++){
System.out.println("Enter Student ID: ");
sID[i] = scan.nextInt();
outputFile.println(sID[i]);
}//end of for
outputFile.close();
System.out.println("\nWriting Complete!");
File filed = new File(file);
Scanner inputFile = new Scanner(filed);
int count = 0;
while(inputFile.hasNext()){
count++;
stuID = inputFile.nextInt();
System.out.println("ID "+count+" from file is "+stuID);
}//end of while
inputFile.close();
System.out.println("\nAfter sort");
BubbleSort(sID,numStu,file,header);//Invokes BubbleSort()
for(int i = 1; i<=numStu;i++){
System.out.println(sID[i]);
}//end of for
System.out.println("\nWould you like to search for an ID?\n1=yes 2=no");
int ans = scan.nextInt();
if(ans == 1)
{
answer = true;
}
else {
answer = false;
}//end of if-else
while(answer){
System.out.println("Enter a number to search for: (-1 to cancel)");
int search = scan.nextInt();
if(search == -1){
answer = false;
}//end of if
int value = binarySearch(sID,numStu,search);//Invokes binarySearch()
if(value !=-1){
System.out.println("\nThe search found "+search+" in position "+value);
}else{
System.out.println("\nThe value was not found");
}//end of if-else
}//end of while
Insert(sID,numStu,file,header);//Invokes Insert()
System.out.println("\nAfter the Insert");
for(int i = 0; i<=numStu;i++){
System.out.println(sID[i]);
}//end of for
Bribed(sID,numStu,file,header);//Invokes Bribed()
System.out.println("\nAfter the bribe");
for(int i = 0; i<=numStu+1;i++){
System.out.println(sID[i]);
}//end of for
System.out.println("********************************************");
System.out.println("\tProgrammed by David Stevenson");
System.out.println("********************************************");
}//end of main()
//**********************************************************************************
// Declaration of methods
//**********************************************************************************
//*------------------*
// Bubble Sort
//*------------------* @param----------------------------------------|
public static void BubbleSort(int[] sID,int length,String file,String header) throws IOException{
int startScan,index,minIndex,minValue;
for(startScan = 1; startScan <= length;startScan++)
{
minIndex = startScan;
minValue = sID[startScan];
for(index = startScan+1; index<= length; index++)
{
if(sID[index] <minValue)
{
minValue = sID[index];
minIndex = index;
}//end of if
}//end of nested for
sID[minIndex] = sID[startScan];
sID[startScan] = minValue;
}//enf of for
PrintWriter outPut = new PrintWriter(file);
outPut.println(header);
for(int i = 1; i<=length;i++)
{
outPut.println(sID[i]);
}//end of for
outPut.close();
}//end of BubbleSort()
//*--------------------------*
// Binary Search (if-else)
//*--------------------------* @param-------------------------|
public static int binarySearch(int[] x, int length, int search){
int first = 1;
int last = length;
int middle =0;
int position = -1;
boolean found = false;
while(first <= last &&!found)
{
middle = (first + last) /2;
if(x[middle]== search)
{
found = true;
position = middle;
}
else if(x[middle] > search){
last = middle -1;
}
else {
first = middle + 1;
}//end of if-else-if-else
}//end of while
return position;
}//end of binarySearch()
//*-----------------------*
// Insert value to array
//*-----------------------* @param--------------------------------------|
public static void Insert(int[] sID,int length,String file,String header) throws IOException{
Scanner scan = new Scanner(System.in);
int value;
System.out.println("Please enter an ID to insert into Club: ");
value = scan.nextInt();
sID[0] = value;
for(int i = 0; i <length;i++)
{
int temp = sID[i];
sID[i] = sID[i+1];
sID[i+1] = temp;
}//end of for
int startScan,index,minIndex,minValue;
for(startScan = 1; startScan <= length;startScan++)
{
minIndex = startScan;
minValue = sID[startScan];
for(index = startScan+1; index<= length; index++)
{
if(sID[index] <minValue)
{
minValue = sID[index];
minIndex = index;
}//end of if
}// end of nested for
sID[minIndex] = sID[startScan];
sID[startScan] = minValue;
}//end of for
PrintWriter outPut = new PrintWriter(file);
outPut.println(header);
for(int i = 0; i<=length;i++)
{
outPut.println(sID[i]);
}//end of for
outPut.close();
}//end of Insert()
//*---------------------*
// Appending Array
//*---------------------* @param----------------------------------------|
public static void Bribed(int[] x,int length, String file, String header) throws IOException{
Scanner scan = new Scanner(System.in);
int bribed;
System.out.println("Have you been bribed lately?");
System.out.println("Yeah? ok... Enter the ID then!");
bribed = scan.nextInt();
x[length+1] = bribed;
PrintWriter outPut = new PrintWriter(file);
outPut.println(header);
for(int i = 0; i<=length+1;i++)
{
outPut.println(x[i]);
}//end of for
outPut.close();
}//end of Bribed()
}