0

I'm working with eclipse and when I want the program to print either all the array elements or just a single array element via indexing. It doesn't return an error or even an address when I run the program.

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();
    String [] TopXA = new String [ListSize + 1];
    int [] TopXB = new int[ListSize + 1];

    for (int x = 0; x < TopXA.length; ++ x)
    {
        System.out.println("Please enter an item to be organized on the list");
        String item = inputDevice.nextLine();
        TopXA[x] = item;
        System.out.println("You have " + (ListSize - x) + " 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.");

    for (int y = 0; y < TopXA.length; ++ y) 
        for (int z = 0; z < TopXA.length; ++ z)
        {
            String compareA = TopXA[y];
            String compareB = TopXA[z];

            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();

            switch(choice)
            {
            case 1:
                TopXB[y] =+ 1;
                break;
            case 2:
                TopXB[z] =+ 2;
                break;
            default:
                System.out.print("I'm sorry but that is not a valid input.");
            }
        }

When I run the code it will print out. "Do you prefer or ." instead of the array element.

dpolaristar
  • 69
  • 1
  • 8

2 Answers2

0

Disclaimer - I don't have eclipse, I dont program in Java

Please check two things 1) The for loop for y doesn't have curly brackets 2) Please check array 'TopXA' to see if 'item' is being populated in array once you input it. 3) Are CompareA and CompareB being assigned values?

EDIT - found problem

Add scanner.nextLine(); after the nextINt line.

Maertin
  • 384
  • 1
  • 8
  • As far as I can tell it should be populated but the only way I know how to check is too print the array which leads me back to square one. I've also tried populating the array directly with TopXA[x] = inputDevice.nextLine(); With the same results. – dpolaristar Nov 30 '15 at 23:14
  • Just do System.out.println(TopXA[0]); and see if it prints a value. – Maertin Nov 30 '15 at 23:16
  • I did that. In fact I did that first before I tried putting it in a variable same results. – dpolaristar Nov 30 '15 at 23:18
  • Correct. According to all the sites I've gone to and my Java book it should print something. Or at least complain with an error that array element is null. – dpolaristar Nov 30 '15 at 23:21
  • Okay - so 'Item' is not getting value properly - there is some problem in initialization, you need to check the scanner code – Maertin Nov 30 '15 at 23:22
  • Did you import Java.util? – Maertin Nov 30 '15 at 23:24
  • Yes I did import Java.util – dpolaristar Nov 30 '15 at 23:30
  • Your issue should be solved now. I edited my answer but the person above me gave the correct syntax at the same time- he referenced the object instance, I referenced the class instance. – Maertin Nov 30 '15 at 23:31
0

Your problem is that the nextInt() method is not consuming the newline character (see other question). This leads to the fact that your first element in the list is empty. In the first iteration (y=0, z=0) of the nested for-loops this empty element gets printed two times.

Try adding inputDevice.nextLine() after each nextInt() call and you will see that it is working ;)

And PLEASE: Don't name variables uppercase!

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");
        String item = inputDevice.nextLine();
        topXA[x] = item;
        System.out.println("You have " + (listSize - x) + " 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.");

    for (int y = 0; y < topXA.length; ++y) 
    {
        for (int z = 0; z < topXA.length; ++z)
        {
            String compareA = topXA[y];
            String compareB = topXA[z];

            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] =+ 2;
                break;
            default:
                System.out.print("I'm sorry but that is not a valid input.");
            }
        }
    }
}
}
Community
  • 1
  • 1
infotoni91
  • 682
  • 3
  • 12