I'm writing a program that helps a user make a Top X or top favorites list.
The user will first enter the number of items that will be on the list which is then used to create an array of the same size. Then the user populates the array. Finally the user is asked a series of question where the program compares each item on the list to another item on the list one at a time and ask between the two which one do thy prefer more. Based on their choice their score is kept in another array that stores integer values. Which is then sorted in descending order.
When I run the program however it doesn't compare all the elements to each element.
For example say I have an array three elements long with the items being "Vanilla", "Chocolate", and "Strawberry." It will ask me to compare Vanilla to Chocolate, then Vanilla to Strawberry. However it will then not compare Chocolate to Strawberry and then print the results. I want to know what logical error I am getting. I have never used a bubble search for strings.
Here is my code for reference:
import java.util.Scanner;
public class FavoriteListMaker {
public static void main(String[] args)
{
Scanner inputDevice = new Scanner(System.in);
System.out.println("This program is to help you order a favorites list. Please enter the amount of items on the list.");
int ListSize = inputDevice.nextInt();
inputDevice.nextLine();
String [] TopXA = new String [ListSize];
int [] TopXB = new int[ListSize];
for (int x = 0; x < TopXA.length; ++ x)
{
System.out.println("Please enter an item to be organized on the list");
TopXA[x] = inputDevice.nextLine();
System.out.println("You have " + (ListSize - x - 1) + " items left to fill on the list.");
}
System.out.println("Now we will compare each item on the list with every item on the list one at a time.");
System.out.println("We will ask you a series a question on whether you like item A better then item B and tally the score.");
System.out.println("At the end the item with the most points wins.");
int comparisonsToMake = TopXA.length - 1;
for (int y = 0; y < TopXA.length - 1; ++ y)
{
for (int z = 0; z < comparisonsToMake; ++ z)
{
if(TopXA[y] != TopXA[z + 1])
{
String compareA = TopXA[y];
String compareB = TopXA[z + 1];
System.out.println("Do you prefer " + compareA + " or " + compareB + " .");
System.out.println("If you prefer " + compareA + " Please press 1. If you prefer " + compareB + " please press 2.");
int choice = inputDevice.nextInt();
inputDevice.nextLine();
switch(choice)
{
case 1:
TopXB[y] =+ 1;
break;
case 2:
TopXB[z + 1] =+ 1;
break;
default:
System.out.print("I'm sorry but that is not a valid input.");
}
}
}
--comparisonsToMake;
}
int comparisonsToMakeB = TopXB.length - 1;
for(int a = 0; a < TopXB.length - 1; ++ a)
{
for(int b = 0; b < comparisonsToMakeB; ++b)
{
if(TopXB[b] < TopXB[b + 1])
{
String temp = TopXA[b];
TopXA[b] = TopXA[b + 1];
TopXA[b + 1] = temp;
}
}
--comparisonsToMakeB;
}
for(int q = 0; q < TopXA.length; ++ q)
{
System.out.print("Your number " + (q + 1) + " pick is " + TopXA[q] + ".");
}
}
}