|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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. |
|
#2
|
||||
|
||||
|
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:
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. |
|
#3
|
|||
|
|||
|
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 ![]() |
|
#4
|
||||
|
||||
|
Quote:
sorry, my fault. the first loop should be: i<Names Quote:
working with algorithm has never been easy and with arrays, you just have keep in mind that max indexing is length-1 ![]() |
|
#5
|
||||
|
||||
|
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.
|
|
#6
|
|||
|
|||
|
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. |
|
#7
|
|||
|
|||
|
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 |
|
#8
|
|||
|
|||
|
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 |
|
#9
|
||||
|
||||
|
Quote:
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)));}} |
|
#10
|
||||
|
||||
|
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.” |
|
#11
|
|||
|
|||
|
Quote:
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. ![]() |
|
#12
|
|||
|
|||
|
Quote:
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. |
|
#13
|
|||
|
|||
|
I am so gonna fail...I am so lost.
*plays with the code some more* |