0

I'm working on a program where I'm inputting values(String and int) into arrays, putting those values into an objects which go into an array list to be sorted by the the int value. When I run the program though, it prints out: Sorted List Entries: Item Name:null---Quant:0 Item Name:null---Quant:0 Item Name:null---Quant:0 //etc.. I'm trying to learn on my own here but I'm not sure what to do.

My main class:

import java.io.*;
import java.util.*;

public class InputItem
{
  public static void main(String args[])
  {
    String again;
    String names[] = new String[100];
    int quant[] = new int[100];
    int row=0;
    do{
    System.out.println("Please input assignment name:");
    Scanner newName = new Scanner(System.in);
    String name = newNamet.next();
    names[row] =name;

    System.out.println("Please input assignment quant:");
    Scanner quantI = new Scanner(System.in);
    int quantity = quantI.nextInt();
    quant[row] = quantity;

    System.out.println("Would you like to add another item? Enter 'Yes' or 'No'"); 
    Scanner input = new Scanner(System.in);
    again = input.next();
    row++;
    }
    while(again.equalsIgnoreCase("Yes"));
    List<Items> work = new ArrayList<Items>();
    for(int count = 0; count<row; count++)
    {
      work.add(new Items((names[row]),(quant[row])));
    }
    Collections.sort(work, new MyComp());
    System.out.println("Sorted List Entries: ");
    for(Items e:work)
    {
      System.out.println(e);
    } 
  }
}

Class with Comparator:

import java.util.*;
class MyComp implements Comparator<Items>
{
  @Override
  public int compare(Items e1, Items e2)
  {
    if((e1).getQuant()< (e2).getQuant())
    {
      return 1;
    }
    else
    {
      return -1;
    }

  }
}
public class Items
{
  private String name;
  private int quant;
  public Items(String n, int q)
  {
    this.name = n;
    this.quant = q;
  }
  public String getName()
  {
    return name;
  }
  public void setName(String name)
  {
    this.name = name;
  }
  public int getQuant()
  {
    return quant;
  }
  public void setQuant(int quant)
  {
    this.quant = quant;
  }
  public String toString()
  {
    return "Item Name:" + this.name+"---Quant:" +this.quant;
  }
  }
1
  • Are you sure that this code actaully compiles? - Scanner newName = new Scanner(System.in); String name = newNAmet.next(); Commented Feb 21, 2016 at 21:59

1 Answer 1

1

The problem is here...

for (int count = 0; count < row; count++) {
    work.add(new Items((names[row]), (quant[row])));
}

You're using row, which was defined in the previous section of code to keep track of which element you were updating, but is now pointing to the next element in the array (or an empty element). This basically means you are constantly adding the same (empty) values to your Items

Instead, you should be using count

for (int count = 0; count < row; count++) {
    work.add(new Items((names[count]), (quant[count])));
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I can't believe overlooked something that small!
It helps to place some debug statements in your code, this way you can see what you code is doing when it's running, it's basically how I caught it ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.