Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: I Cant seem to get my sort routine to function

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation I Cant seem to get my sort routine to function

    Well for a lab I have to calculate and output a receipt for a college tuition bill. The problem is the receipt has to be organized by name and when I run the program it only prints the first name in the correct position. Please help.
    import java.util.Scanner;
     
    public class Lab6_3
    {
    							//the constant of this program.
    	static final double RATE=173.36;
    							//method for getting the tuition bill.
    	static double bill(int hours)
    	{
    		return RATE*hours;
    	}
    							//method for printing out all of the information.
    	static void receipt(String name, int hours, int stud)
    	{
    		double tuition=bill(hours);
    		System.out.println();
    		System.out.printf("%-15s",name);
    		System.out.printf("%10s",hours);
    		System.out.printf("%10s%-15.2f","$",tuition);
    		System.out.println();
    	}
    							//start of main program.
    	public static void main(String[]args)
    {
    	Scanner s=new Scanner(System.in);
    							//declarations of the variables of the program.
    	int stud, x, index, scan, min, temp2, length;
    	String temp;
    							//asking how many students at the college.
    	System.out.print("How many students do you want to attend your college : ");
    	stud=s.nextInt();
    							//declaring the arrays.
    	int[]hours=new int[stud];
    	String[]name=new String[stud];
    							//for loop to ask for the student's name and hours.
    	for(x=0;x<stud;x++)
    	{
    		s.nextLine();
    		System.out.print("Enter student's name here : ");
    		name[x]=s.nextLine();
    		System.out.print("Enter semester hours here : ");
    		hours[x]=s.nextInt();
    		System.out.println();
    	}
    							//sort routine.
    	for(index=0;index<name.length-1;index++)
    	{
    		min=index;
     
    		for(scan=index+1;scan<name.length;scan++)
    		{
    			if(name[scan].compareTo(name[min])<0);
    			min=scan;
    			temp=name[min];
    			name[min]=name[index];
    			name[index]=temp;
    			temp2=hours[min];
    			hours[min]=hours[index];
    			hours[index]=temp2;
    	}
    		scan++;
    }
    								//header for the output.
    	System.out.println();
    	System.out.print("     University of Derp");
    	System.out.println();
    	System.out.println();
    	System.out.print("Student               Hours     Tuition");
    								//for loop for outputting the receipt.
    	for(x=0;x<name.length;x++)
    		receipt(name[x],hours[x],stud);
    	System.out.println();
    	}
    	}
    Last edited by helloworld922; May 22nd, 2011 at 03:22 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,557
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: I Cant seem to get my sort routine to function

    First: Please put your code in code tags to preserve formatting. Unformatted code is hard to read.

    it only prints the first name in the correct position
    Please copy and paste here the output from the program and add some comments showing what is wrong. Also show what the output should be.

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I Cant seem to get my sort routine to function

    University of Derp

    Student Hours Tuition
    Ted Jones 52 $9014.72

    Matt Smith 85 $14735.60

    john dow 85 $14735.60

    This is what is being outputted. The list are suppose to be sorted by name in alphabetical order.Evey thing else seems to be right except that. The sort routine is suppose to be a binary sort

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,557
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: I Cant seem to get my sort routine to function

    Try debugging the code by adding println statements to show the execution flow and how the values of variables change.

    A comment on inputting data for easier testing. Put all the data in the program:

    String Data = "2\nFirst\n20\nSecond\n22\n"; //<<<<<<<<<<
    Scanner s=new Scanner(Data) ;//System.in);

  5. #5
    Junior Member
    Join Date
    May 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I Cant seem to get my sort routine to function

    The program only works for the first variable.
    EXAMPLE
    I enter the data D,A,B
    The program will pick up that A is first but the rest remain in the same position and it will look like this A,D,B

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,557
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: I Cant seem to get my sort routine to function

    Can you copy the full contents of the console window showing the input and output and paste it here?

    Did you: Try debugging the code by adding println statements to show the execution flow and how the values of variables change.

  7. #7
    Junior Member
    Join Date
    May 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I Cant seem to get my sort routine to function

    --------------------Configuration: <Default>--------------------
    How many students do you want to attend your college : 3
    Enter student's name here : Jim Bob
    Enter semester hours here : 555

    Enter student's name here : Tony Smith
    Enter semester hours here : 6

    Enter student's name here : Ricky James
    Enter semester hours here : 6


    University of Derp

    Student Hours Tuition
    Ricky James 6 $1040.16

    Tony Smith 6 $1040.16

    Jim Bob 555 $96214.80


    Process completed.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,557
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: I Cant seem to get my sort routine to function

    Not sure the reason for your last post.

    Earlier I asked:
    Please copy and paste here the output from the program and add some comments showing what is wrong.

    Did you: Try debugging the code by adding println statements to show the execution flow and how the values of variables change.

  9. #9
    Junior Member
    Join Date
    May 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I Cant seem to get my sort routine to function

    Sorry about that.

    --------------------Configuration: <Default>--------------------
    How many students do you want to attend your college : 3
    Enter student's name here : Jim Bob
    Enter semester hours here : 555

    Enter student's name here : Tony Smith
    Enter semester hours here : 6

    Enter student's name here : Ricky James
    Enter semester hours here : 6


    University of Derp

    Student Hours Tuition //This part of the output needs to be in order by name.I tried debugging the code and it seems to be
    running the data once before it exits out
    Ricky James 6 $1040.16

    Tony Smith 6 $1040.16

    Jim Bob 555 $96214.80


    Process completed.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,557
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: I Cant seem to get my sort routine to function

    running the data once before it exits out
    Continue debugging and determine why it exits after only once.

  11. #11
    Junior Member
    Join Date
    May 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I Cant seem to get my sort routine to function

    Still Nothing. I just cant seem to figure it out.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,557
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: I Cant seem to get my sort routine to function

    Why does it exit? What test does it do that is not working correctly?
    Look at the code where it exits. Add printlns to show what is happening.

    Post your code showing how you are using printlns to debug it.

  13. #13
    Junior Member
    Join Date
    May 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I Cant seem to get my sort routine to function

    Still nothing. I just cant seem to figure it out.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,557
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: I Cant seem to get my sort routine to function

    Post your code showing how you are using printlns to debug it.
    Maybe I can make some suggestions to help you use printlns to show the problem.

  15. #15
    Junior Member
    Join Date
    May 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I Cant seem to get my sort routine to function

    I inserted a println after the sort to discover that the program isnt even sorting at all. Now the problem is how to get it to notice the sort

    import java.util.Scanner;

    public class Lab6_3
    {
    //the constant of this program.
    static final double RATE=173.36;
    //method for getting the tuition bill.
    static double bill(int hours)
    {
    return RATE*hours;
    }
    //method for printing out all of the information.
    static void receipt(String name, int hours, int stud)
    {
    double tuition=bill(hours);
    System.out.println();
    System.out.printf("%-15s",name);
    System.out.printf("%10s",hours);
    System.out.printf("%10s%-15.2f","$",tuition);
    System.out.println();
    }
    //start of main program.
    public static void main(String[]args)
    {
    Scanner s=new Scanner(System.in);
    //declarations of the variables of the program.
    int stud, x, min, temp2, length;
    String temp;
    //asking how many students at the college.
    System.out.print("How many students do you want to attend your college : ");
    stud=s.nextInt();
    //declaring the arrays.
    int[]hours=new int[stud];
    String[]name=new String[stud];
    //for loop to ask for the student's name and hours.
    for(x=0;x<stud;x++)
    {
    s.nextLine();
    System.out.print("Enter student's name here : ");
    name[x]=s.nextLine();
    System.out.print("Enter semester hours here : ");
    hours[x]=s.nextInt();
    System.out.println();
    }
    //sort routine.
    for(int index=0;index<name.length-1;index++)
    {
    {

    min=index;

    for(int scan=index+1;scan>name.length;scan++)
    {
    if(name[scan].compareTo(name[min])<0)
    {
    min=scan;
    }
    temp=name[min];
    name[min]=name[index];
    name[index]=temp;
    temp2=hours[min];
    hours[min]=hours[index];
    hours[index]=temp2;
    System.out.println(name[scan]);
    }
    }
    }

    //header for the output.
    System.out.println();
    System.out.print(" University of Derp");
    System.out.println();
    System.out.println();
    System.out.print("Student Hours Tuition");
    //for loop for outputting the receipt.
    for(x=0;x<name.length;x++)
    receipt(name[x],hours[x],stud);
    System.out.println();
    }
    }

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,557
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: I Cant seem to get my sort routine to function

    Where are all the printlns I asked you to put in the code to show how variable are changing value and to show control flow?

    Also please surround your code with code tags to preserve formatting. Use The icon above the input box with the #

    Here are some sample printlns:
    System.out.println("index=" + index + ", n.l=" + name.length);
     
    System.out.println("comp: " + name[scan] + " to " + name[min]); 
     
    System.out.println("name=" + name[scan] + ", scan=" + scan);      //<<<<<<<< NEVER EXECUTED???

    If you add these to your code at the right places the output should give you a clue to where to look to find the problem.
    Last edited by Norm; May 22nd, 2011 at 07:59 PM.

Similar Threads

  1. About the ackermann function...
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2011, 09:24 AM
  2. Need Help on the Interface Function
    By yel_hiei in forum Object Oriented Programming
    Replies: 12
    Last Post: July 29th, 2010, 07:27 AM
  3. bubble sort and selection sort on strings
    By Sir Saula in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 3rd, 2010, 09:44 AM
  4. Substring function
    By bristol580 in forum Java SE APIs
    Replies: 2
    Last Post: November 12th, 2009, 10:29 AM
  5. Function of difference between two numbers
    By uplink600 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 13th, 2009, 05:57 AM