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()    
    
}

The first bad habit I noticed is that you initialized your stID array as an array of 100 ints, but then you have this piece of code:

for(int i = 1; i<=numStu;i++){
System.out.println("Enter Student ID: ");
sID[i] = scan.nextInt();
outputFile.println(sID[i]);
}//end of for

If the user enters a number > 100 for numStu, your program will generate an ArrayOutOfBounds Exception and subsequently crash. A better habit would be to prompt the user for the number of students then initialize the array with that number.

i.e.

int[] students = null;
int numStud = scanner.nextInt();
...
students = new int[numStud];

I'm pretty tired right now, didn't sleep last night at all. But that is the general idea.

Thanks a lot for going over that.... i see where that would be a problem. I will keep my eyes open for real life applications and future assignments. (of course it was set to 100 because of the assignment needs).

lets say i did have the user set it for me.. would i then need to write the for loop with the "array".length and start my index at 0 to stop an out by one error?:

for(int i = 0; i<=sID.length;i++){   
      System.out.println("Enter Student ID: ");
      sID[i] = scan.nextInt();
      outputFile.println(sID[i]);
      }//end of for

Greatly appreciated
thanks
Zoxx98

Firstly, I would like to thank you for demarcating your loop and logic structures. Legacy and maintenance programmers will appreciate that effort.

The actual documentation that is provided within the code is very light. You will hear will hear as many viewpoints on this as there are people who program :).. There is something that java programmers use to help with this however. It's called Javadoc, and it will require some amount of work on your part, but it is one of the really good habits to get into. Take a look at this website :

http://java.sun.com/j2se/javadoc/writingdoccomments/

Besides making your program easier to understand by others, it helps when using some SCCS (Source Code Control Systems) setups. At the very least, it will help you. You may completely understand the code now, but in 3 weeks it could look completely foreign to you.

Secondly, the main function header typically includes a string parameter array. Your program does not use it and it is a contradiction to my next point, however; there are some IDE's that will not be able to handle it. Here is the example:

old

public static void main()throws IOException {

new

public static void main(String[] args)throws IOException {

lastly. I would like to point out a habit that is really good only if you use the code completely, but leaving out the implementation sucks big time. What I'm talking about is error handling.

Your function headers are all set up to throw out exceptions, which is good. However, nowhere in your code do you provide a mechanism for error handling, which sucks. It could be that you are in a part of the class that has not addressed this yet. It's hard to tell. The point that I'm trying to make is that don't add code like that if you have no intentions of using it. It adds an additional layer of confusion to your programs.

That said... this is another habit that if you develop and use it in all of your programs, you will definitely gain the favor of your fellow programmers :)

Take a look at this location:
http://java.sun.com/docs/books/tutorial/essential/exceptions/

One final note about good habits. Try and use meaningful identifiers whenever you can. This code:

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

is hard to deal with. While we all know that this is walking a value to the end of an array, it does not really describe the items involved. Not using meaningful identifiers in some shops is very bad form.

I have one recommendation for you that I have found to help when learning Java programming. Use and IDE called jGRASP. Once you get it up and running, enable the following option:
Settings->CSD Window Settings->Auto Generate CSD

It goes beyond syntax highlighting and helps to define your logic path in a program.

Looks like you are the one people go to in class for help :) Good job man.

Thanks a lot for going over that.... i see where that would be a problem. I will keep my eyes open for real life applications and future assignments. (of course it was set to 100 because of the assignment needs).

lets say i did have the user set it for me.. would i then need to write the for loop with the "array".length and start my index at 0 to stop an out by one error?:

for(int i = 0; i<=sID.length;i++){   
      System.out.println("Enter Student ID: ");
      sID[i] = scan.nextInt();
      outputFile.println(sID[i]);
      }//end of for

Greatly appreciated
thanks
Zoxx98

Using the length of an array for looping is a good habit to get into. There are situations that you will run into where you simply do not know, or can't control the length of an array.

Looks to me like you may be able to understand and use another looping mechanism in your programs. It's called the For-each loop in Java :) It's pretty cool as well!

Consider the following:

for (int studentID : sID) {   
      System.out.println(studentID);
      }//end of for

and the equivalent basic structure

for (int i = 0; i<=sID.length;i++) {   
      System.out.println(sID[i]);
      }//end of for

The first example is so much more easier to read don't you think? It's great to use with arrays that are already populated with values.

BUT...

There are some limitations for it's use. It's read only, works only with single structures, you can not see other elements of the array from inside the loop and it is only able to step through the array in one (forward) direction.

Even with those limitations, using things like this is a huge step in simplifying your coding.

Thanks a lot!! appreciate the comments. But on the note of error handling, it has not been addressed in class as of yet. I was simply following was i was told in that respect. I greatly appreciate everything you have noted.

as you said"the one to go to in class" --- A buddy of mine and I are the "prodigies" of our uni classes... SO FAR.... lols

I hope to become a great contributor to this site and other coders.

EDIT: jGRASP is remarkable compared to the BlueJ i have to use.

Why do you have to use BlueJ? You should be able to write your code in any IDE. If you're concerned that it might not run properly, you could always import the project into BlueJ and run it there to make sure. Also, if you're satisfied with the help you got in here, you can mark the thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.