Reputation: 49
So I'm new to Java and am trying to pass a variable from the scanner to my getter/setter method so that depending on the number entered it'll sort the array differently.
I've got it where the list will sort; problem is my scanner repeats where you have to enter your selection in multiple times before the list shows up.
I know the problem has to do with the call "int c = assign_6.choice()". If I hard code in a number it's fine but it appears to be making multiple calls to the choice() function.
I've tried moving the function out of the main and removing the Comparable in my setter/getter file and also removing the quick sort and using Array and Collections. None of which worked.
I feel like it's probably a stupid mistake I'm making and missing it due to not knowing Java that well. Could use help in figuring this out.
Here's my output:
nter number 1,2 or 3: 1
Enter number 1,2 or 3: 1
Enter number 1,2 or 3: 1
Enter number 1,2 or 3: 1
Enter number 1,2 or 3: 1
Enter number 1,2 or 3: 1
Enter number 1,2 or 3: 1
Enter number 1,2 or 3: 1
Enter number 1,2 or 3: 1
Enter number 1,2 or 3: 1
contacts [age=20, state=Alabama, firstname=Pickles, lastname=Cattle]
contacts [age=35, state=New York, firstname=George, lastname=Constanza]
contacts [age=90, state=Florida, firstname=Adam, lastname=Tree]
contacts [age=32, state=Illinois, firstname=Mary, lastname=Upton]
contacts [age=58, state=Washington, firstname=Bob, lastname=Wiseman]
Code:
import java.util.Scanner;
public class test_6 {
public static void main(String[] args) {
Contacts[] a = {
new Contacts(32, "Illinois", "Mary", "Upton"),
new Contacts(58, "Washington", "Bob", "Wiseman"),
new Contacts(20, "Alabama", "Pickles", "Cattle"),
new Contacts(35, "New York", "George", "Constanza"),
new Contacts(90, "Florida", "Adam", "Tree"),
};
Quick.sort(a);
for (Contacts contacts:a){
System.out.println(contacts);
}
}
public static int choice() {
System.out.print("Enter number 1,2 or 3: ");
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
return i;
}
}
public class Contacts implements Comparable<Contacts>{
Integer age;
String state;
String firstname;
String lastname;
int c = test_6.choice();
public Contacts(Integer age, String state, String firstname, String lastname){
this.age = age;
this.state = state;
this.firstname = firstname;
this.lastname = lastname;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public int compareTo(Contacts contacts) {
if (c == 1){
return this.getLastname().compareTo(contacts.getLastname());
}
else if (c == 2){
return this.getState().compareTo(contacts.getState());
}
else if (c == 3){
return this.getAge().compareTo(contacts.getAge());
}
else return 0;
}
@Override
public String toString() {
return "contacts [age=" + age + ", state=" + state + ", firstname=" + firstname + ", lastname=" + lastname
+ "]";
}
}
Upvotes: 0
Views: 114
Reputation: 895
I don't have enough rep to comment but I feel like it could be your sc.nextInt() statement. try to call sc.close() before you return I and see if that solves your problem.
Upvotes: 1
Reputation: 2647
I'm not entirely sure, you could debug, but it may be something to do with your function being assigned in the global scope. You are better to move the static function choice()
to your Contacts class/object.
Then call the choice()
function from inside the initializer Contacts
. For example:
import java.util.Scanner;
public class test_6 {
public static void main(String[] args) {
Contacts[] a = {
new Contacts(32, "Illinois", "Mary", "Upton"),
new Contacts(58, "Washington", "Bob", "Wiseman"),
new Contacts(20, "Alabama", "Pickles", "Cattle"),
new Contacts(35, "New York", "George", "Constanza"),
new Contacts(90, "Florida", "Adam", "Tree"),
};
Quick.sort(a);
for (Contacts contacts:a){
System.out.println(contacts);
}
}
//Function Choice Moved to Contacts.choice()
}
public class Contacts implements Comparable<Contacts>{
Integer age;
String state;
String firstname;
String lastname;
int c;
public Contacts(Integer age, String state, String firstname, String lastname){
this.age = age;
this.state = state;
this.firstname = firstname;
this.lastname = lastname;
this.c = choice();
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public int compareTo(Contacts contacts) {
if (c == 1){
return this.getLastname().compareTo(contacts.getLastname());
}
else if (c == 2){
return this.getState().compareTo(contacts.getState());
}
else if (c == 3){
return this.getAge().compareTo(contacts.getAge());
}
else return 0;
}
public static int choice() {
System.out.print("Enter number 1,2 or 3: ");
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
return i;
}
@Override
public String toString() {
return "contacts [age=" + age + ", state=" + state + ", firstname=" + firstname + ", lastname=" + lastname
+ "]";
}
}
Upvotes: 1