Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old April 17th, 2008, 03:06 PM
jenl2881 jenl2881 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 9 jenl2881 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 3 m 53 sec
Reputation Power: 0
Homework - Java application using loops and sort

I have never posted here before but I did read the guideline for posting. Hope all is alright. I just want to finish this class. Basically this is what we are required to make:

The program, using JOPtionPane, greets user and asks how many names user wants to enter into the system. User responds by enter a positive integer N (N<=100) and clicks ok. JOptionPane responds by asking the user to enter Name #1 and so on. The user enters the name and then the program prints a list of all the names in sorted order (alphabetical).

This is the code I have so far:

Code:
import javax.swing.JOptionPane;
public class Final 
{
	
	public static void main(String[] args) 
	{
		String N = JOptionPane.showInputDialog("How many names do you wish to enter into the system?");
		int Names = Integer.parseInt(N);
		String [] arrays = new String [Names];
		
		for(int i=0; i <= Names-1; i++)
		{
			String input = JOptionPane.showInputDialog("Please Enter Name " + (i+1));
			arrays [i] = input;
		}
		//SORT
		for (int a=1; a<=Names-1; a++)
		{
			for (int k=a+1; k<=Names; k++)
			{
                                                   //ERROR1
				if(arrays[a].compareTo(arrays[k])<0)
				{
					String b=arrays[a];
					arrays[a]=arrays[k];
					arrays[k]=b;	
				}
			}
		}
		
		for (int i=0; i <= 10; i++)
		{
                                //ERROR2
			System.out.println (arrays [i]);
		}
		
	
		}}

for //ERROR1 I get the error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at Final.main(Final.java:21)

for //ERROR2 I get the error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Final.main(Final.java:20)



I think something may be amiss with my If statement or something. If i take away the SORT portion it will print the names but not in order (and I get ERROR2), but if I have it nothing prints. The JOPtion windows work fine. Thanks! The teacher is horrid at explaining anything. We are basically beginners teaching ourselves Java.

Reply With Quote
  #2  
Old April 17th, 2008, 03:26 PM
MrFujin's Avatar
MrFujin MrFujin is offline
Lord of the Dance
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Oct 2003
Posts: 1,198 MrFujin User rank is Major (30000 - 40000 Reputation Level)MrFujin User rank is Major (30000 - 40000 Reputation Level)MrFujin User rank is Major (30000 - 40000 Reputation Level)MrFujin User rank is Major (30000 - 40000 Reputation Level)MrFujin User rank is Major (30000 - 40000 Reputation Level)MrFujin User rank is Major (30000 - 40000 Reputation Level)MrFujin User rank is Major (30000 - 40000 Reputation Level)MrFujin User rank is Major (30000 - 40000 Reputation Level)MrFujin User rank is Major (30000 - 40000 Reputation Level)MrFujin User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 4 Days 20 h 20 m 36 sec
Reputation Power: 309
remember that an array index go from 0 to length-1.
you should remove the '=' from the check in the for loops, so with with first for loop
Code:
i<=Names-1 

should be this
Code:
i<Names-1

Quote:
Code:
		for (int i=0; i <= 10; i++)
		{
                                //ERROR2
			System.out.println (arrays [i]);
		}



besides the error with '=', are you sure it always have a length at 10 or more? would guess that 10 should be replaced with Names.

Last edited by MrFujin : April 17th, 2008 at 03:28 PM.

Reply With Quote
  #3  
Old April 17th, 2008, 04:24 PM
jenl2881 jenl2881 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 9 jenl2881 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 3 m 53 sec
Reputation Power: 0
If I make it
Code:
i<Names-1


I get it only letting me enter 2 names if I say I want to enter 3. Then it gave me errors with my CompareTo.

Such an annoying assignment...

The last for says
Code:
 i<=10 


If I change it to Names it makes not difference. I have no idea where the 10 came from. I think he put it on the board but his codes NEVER work. I probably just copied and planned to change it. Guh

Reply With Quote
  #4  
Old April 17th, 2008, 07:01 PM
MrFujin's Avatar
MrFujin MrFujin is offline
Lord of the Dance
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Oct 2003
Posts: 1,198 MrFujin User rank is Major (30000 - 40000 Reputation Level)MrFujin User rank is Major (30000 - 40000 Reputation Level)MrFujin User rank is Major (30000 - 40000 Reputation Level)MrFujin User rank is Major (30000 - 40000 Reputation Level)MrFujin User rank is Major (30000 - 40000 Reputation Level)MrFujin User rank is Major (30000 - 40000 Reputation Level)MrFujin User rank is Major (30000 - 40000 Reputation Level)MrFujin User rank is Major (30000 - 40000 Reputation Level)MrFujin User rank is Major (30000 - 40000 Reputation Level)MrFujin User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 4 Days 20 h 20 m 36 sec
Reputation Power: 309
Quote:
I get it only letting me enter 2 names if I say I want to enter 3. Then it gave me errors with my CompareTo.

sorry, my fault.
the first loop should be: i<Names

Quote:
Such an annoying assignment...


working with algorithm has never been easy

and with arrays, you just have keep in mind that max indexing is length-1

Reply With Quote
  #5  
Old April 17th, 2008, 08:02 PM
tvc3mye's Avatar
tvc3mye tvc3mye is offline
Daniel Schildsky
Dev Shed Novice (500 - 999 posts)
 
Join Date: Mar 2004
Location: KL, Malaysia.
Posts: 530 tvc3mye User rank is First Lieutenant (10000 - 20000 Reputation Level)tvc3mye User rank is First Lieutenant (10000 - 20000 Reputation Level)tvc3mye User rank is First Lieutenant (10000 - 20000 Reputation Level)tvc3mye User rank is First Lieutenant (10000 - 20000 Reputation Level)tvc3mye User rank is First Lieutenant (10000 - 20000 Reputation Level)tvc3mye User rank is First Lieutenant (10000 - 20000 Reputation Level)tvc3mye User rank is First Lieutenant (10000 - 20000 Reputation Level)tvc3mye User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 5 Days 8 h 15 m 14 sec
Reputation Power: 129
Send a message via ICQ to tvc3mye Send a message via MSN to tvc3mye Send a message via Yahoo to tvc3mye
Error

The error does not lie in the Names-1 term. It is with the sorting section.


When a = Names -1, you will get k = Name. Since the array has only up to a total of elements equate to the value stored in Names variable, the subscript is up to Names -1. So, sure the execution throws ArrayIndexOutOfBoundsException because the element arrays[k] does not exist when k= Names.
Change k<=Names to k<Names should get rid of Error 1.
__________________
When the programming world turns decent, the real world will turn upside down.

Reply With Quote
  #6  
Old April 18th, 2008, 12:22 AM
jenl2881 jenl2881 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 9 jenl2881 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 3 m 53 sec
Reputation Power: 0
cool!

Ok your theory worked! I changed the sort part of the code to this:
Code:
		//SORT
		for (int a=1; a<=Names-1; a++)
		{
			for (int k=a+1; k<Names; k++)
			{

				if(arrays[a].compareTo(arrays[k])<0)
				{
					String b=arrays[a];
					arrays[a]=arrays[k];
					arrays[k]=b;	
				}
			}
		}
		
		for (int i=0; i<Names; i++)
		{

			System.out.println (arrays [i]);
		}
		
	
		}}

I also fixed the for loop with the println. The errors are gone but when I enter 3 names, then enter John, Adam, and Mark it sorts them as John, Mark, Adam.

Reply With Quote
  #7  
Old April 18th, 2008, 12:49 AM
jenl2881 jenl2881 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 9 jenl2881 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 3 m 53 sec
Reputation Power: 0
It works!

Thank you all! I was about to go to sleep when something occured to me. It was sorting the names AFTER the first name, but not the first name. I checked and sure enough I had a=1 and not a=0. Changed it and eureka, it works! Thanks! Here is the finished code:

Code:
import javax.swing.JOptionPane;
public class Final 
{
	
	public static void main(String[] args) 
	{
		String N = JOptionPane.showInputDialog("How many names do you wish to enter into the system?");
		int Names = Integer.parseInt(N);
		String [] arrays = new String [Names];
		
		for(int i=0; i<=Names-1; i++)
		{
			String input = JOptionPane.showInputDialog("Please Enter Name " + (i+1));
			arrays [i] = input;
		}
		//SORT
		for (int a=0; a<=Names-1; a++)
		{
			for (int k=a+1; k<Names; k++)
			{

				if(arrays[a].compareTo(arrays[k])>0)
				{
					String b = arrays[a];
					arrays[a]=arrays[k];
					arrays[k]=b;	
				}
			}
		}
		
		for (int i=0; i<Names; i++)
		{

			System.out.println (arrays [i]);
		}
		
	
		}}


Thanks both of you

Last edited by jenl2881 : April 18th, 2008 at 12:50 AM. Reason: typo

Reply With Quote
  #8  
Old April 23rd, 2008, 12:40 AM
jenl2881 jenl2881 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 9 jenl2881 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 3 m 53 sec
Reputation Power: 0
Exclamation Ugh

Ok now he suddenly wants this:

Revise the program that you wrote in the 2nd baby step as follows:

1. The fields to be entered by the user are FirstName, LastName, StreetAddress, City, Zipcode, State, DayOfBirth, MonthOfBirth, YearOfBirth, Title, and Salary.


2. After all records are entered, give a list of option to the user how the list should be sorted:
a. SortByFirstName
b. SortByLastName
c. SortByZipcode
d. SortByAge [Extra Credit A - 10%]
e. SortByTitle
f. SortBySalary

The user responds by typing the option letter (a, b, c, …etc, -- should be case-insensitive).
The program will then list the records accordingly.

ExtraCredit B [10%]:
Extend the list of option to include ascending and descending order.


This is what I have so far:
Code:
import javax.swing.JOptionPane;
public class Final 
{
	
	public static void main(String[] args) 
	{
		String N = JOptionPane.showInputDialog("How many names do you wish to enter into the system?");
		int Names = Integer.parseInt(N);
		String [][] arrays = new String [Names][11];
		
		for(int i=0; i<=Names-1; i++)
		{
			String input = JOptionPane.showInputDialog("Please Enter First Name");
			arrays [0] = input;

			String input1 = JOptionPane.showInputDialog("Please Enter Last Name");
			arrays [i][1] = input1;	

			String input2 = JOptionPane.showInputDialog("Please Enter Street Address");
			arrays [i][2] = input2;
			
			String input3 = JOptionPane.showInputDialog("Please Enter City");
			arrays [i][3] = input3;

			String input4 = JOptionPane.showInputDialog("Please Enter Zip Code");
			arrays [i][4] = input4;

			String input5 = JOptionPane.showInputDialog("Please Enter State");
			arrays [i][5] = input5;

			String input6 = JOptionPane.showInputDialog("Please Enter Day Of Birth");
			arrays [i][6] = input6;
	
			String input7 = JOptionPane.showInputDialog("Please Enter Month Of Birth");
			arrays [i][7] = input7;

			String input8 = JOptionPane.showInputDialog("Please Enter Year Of Birth");
			arrays [i][8] = input8;

			String input9 = JOptionPane.showInputDialog("Please Enter Title");
			arrays [i][9] = input9;

			String input10 = JOptionPane.showInputDialog("Please Enter Salary");
			arrays [i][10] = input10;
		}
		String sort = JOptionPane.showInputDialog("A. First Name, B. Last Name, C. Zip code, D. Age, E. Title, F. Salary")if sort = A;
		//ERROR1

		for (int a=0; a<=Names-1; a++)
		{
			for (int k=a+1; k<Names; k++)
			{

				if(arrays[a].compareTo(arrays[k])>0)    //ERROR2
				{
					String [] b = arrays[a];
					arrays[a]=arrays[k];
					arrays[k]=b;	
				}
			}
		}
		
		for (int i=0; i<Names; i++)
		{

			System.out.println (arrays [i]);
		}
		
	
		}} 


Error 1 is probably due to my not being finished yet. The 2nd error tells me [I]'Cannot invoke CompareTo(String[]) on the array type String[]'


I'd like to try the extra credit but I don't much care at the moment. I just want to know if anyone has any ideas how I should proceed? Should I start another post? Thanks!

Last edited by jenl2881 : April 23rd, 2008 at 12:35 PM. Reason: irrelavant information

Reply With Quote
  #9  
Old April 23rd, 2008, 07:08 AM
Yawmark's Avatar
Yawmark Yawmark is offline
Feelin' Groovy
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Aug 2001
Location: WDSMIA
Posts: 7,589 Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 1 Day 5 h 58 m 18 sec
Reputation Power: 1321
Send a message via ICQ to Yawmark Send a message via MSN to Yawmark
Quote:
I REALLY hate this teacher.

Your feelings about your teacher are not relevant to Java, nor is passing your frustration onto the forum particularly inspirational for others to help. Please focus on the Java problems at hand and save the "I Hate My Teacher"™ editorials for The Dev Shed Lounge.

Thanks!

~
__________________
Yawmark
class Sig{public static void main(String...args){\u0066or(int
\u0020$:"vÌÈÊ\"¤¾Àʲ¬Æ\"v¤Î¤\"²¤¨¸¬Æ".to\u0043h\u0061rArray()
)System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
+(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}

Reply With Quote
  #10  
Old April 23rd, 2008, 07:29 AM
gimp's Avatar
gimp gimp is offline
<?PHP user_title("gimp"); ?>
Click here for more information.
 
Join Date: Jan 2005
Location: Internet
Posts: 5,765 gimp User rank is General 7th Grade (Above 100000 Reputation Level)gimp User rank is General 7th Grade (Above 100000 Reputation Level)gimp User rank is General 7th Grade (Above 100000 Reputation Level)gimp User rank is General 7th Grade (Above 100000 Reputation Level)gimp User rank is General 7th Grade (Above 100000 Reputation Level)gimp User rank is General 7th Grade (Above 100000 Reputation Level)gimp User rank is General 7th Grade (Above 100000 Reputation Level)gimp User rank is General 7th Grade (Above 100000 Reputation Level)gimp User rank is General 7th Grade (Above 100000 Reputation Level)gimp User rank is General 7th Grade (Above 100000 Reputation Level)gimp User rank is General 7th Grade (Above 100000 Reputation Level)gimp User rank is General 7th Grade (Above 100000 Reputation Level)gimp User rank is General 7th Grade (Above 100000 Reputation Level)gimp User rank is General 7th Grade (Above 100000 Reputation Level)gimp User rank is General 7th Grade (Above 100000 Reputation Level)gimp User rank is General 7th Grade (Above 100000 Reputation Level)  Folding Points: 1555 Folding Title: Novice Folder
Time spent in forums: 2 Months 2 Weeks 3 Days 13 h 34 m 24 sec
Reputation Power: 1454
Send a message via AIM to gimp
You're doing OOP wrong.

What you want to do is make a Person (or User or Dude or WhateverNameYouWant) class with information about the person in it.

Then you make a variable that says what criteria to sort the people by.

Then you make a variable for ascending or descending.

Then you implement a compareTo(Person other) that sorts appropriately.

Then just Collections.sort(Person[]).

Oh, and use loops for entering people's names, don't do person1 person2 person3 etc.
__________________
A work in progress: Card Game Platform (Status: On Hold) | Joke Thread
“Rational thinkers deplore the excesses of democracy; it abuses the individual and elevates the mob. The death of Socrates was
its finest fruit.”

Reply With Quote
  #11  
Old April 23rd, 2008, 12:37 PM
jenl2881 jenl2881 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 9 jenl2881 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 3 m 53 sec
Reputation Power: 0
Quote:
Originally Posted by gimp
You're doing OOP wrong.

What you want to do is make a Person (or User or Dude or WhateverNameYouWant) class with information about the person in it.

Then you make a variable that says what criteria to sort the people by.

Then you make a variable for ascending or descending.

Then you implement a compareTo(Person other) that sorts appropriately.

Then just Collections.sort(Person[]).

Oh, and use loops for entering people's names, don't do person1 person2 person3 etc.


Well he wants us to use Bubble sort along with Arrays and the loops. In the beginning of the assignment he had us make a box that asked how many names would you like to enter then you would enter each name (if you said 3 names, it would ask you for each name). then it would sort them. So he wants the user to enter the information about each person, then have it sorted based on a chosen criteria.


Reply With Quote
  #12  
Old April 23rd, 2008, 12:40 PM
jenl2881 jenl2881 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 9 jenl2881 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 3 m 53 sec
Reputation Power: 0
Unhappy

Quote:
Originally Posted by Yawmark
Your feelings about your teacher are not relevant to Java, nor is passing your frustration onto the forum particularly inspirational for others to help. Please focus on the Java problems at hand and save the "I Hate My Teacher"™ editorials for The Dev Shed Lounge.

Thanks!

~


I merely said it to iterate that the teacher extended the assignment thought it was supposedly done. I did not intend to pass my frustration on to anyone. I don't usually ask for help on these forums since I feel like i'm taking advantage but I just really am lost on this final project.

Reply With Quote
  #13  
Old April 29th, 2008, 11:40 PM
jenl2881 jenl2881 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 9 jenl2881 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 3 m 53 sec
Reputation Power: 0
Unhappy lost

I am so gonna fail...I am so lost. *plays with the code some more*

Reply With Quote