0

ISSUE:

When I run this everything works fine. Let's say that my chosen IDs are: 0001, 0002, 0003, 0004. And their infos: info1, info2, info3, info4. If I pass 0002 in the scanner I get info2. As you'd expect but if I pass in 0001 I'd expect to get info1 but instead I get nothing. I tried this with an array of fixed size 3 and it worked just fine. I changed to an ArrayList because I don't know how long the file would be. Why does this happen?

I'm a newbie in programming so please forgive my lack of knowledge about where to look for these things.

What my program does is, read in a file and add all the rows to an ArrayList and then asks for input from the user, the input should be in the format of xxxx or 4 characters.

The text file I made as an example looks as follows, no extra spaces on the lines:

0001
info1
4
0002
info2
5
0003
info3
9
0004
info4
10
0005
info5
3

The main class:

public class Main {

    public static ArrayList<Gameobject> games = new ArrayList<>();

    public static void main(String[] args) throws IOException {

        Scanner sc = new Scanner(new File("objects.txt"));

        String id, info;
        int amount;

        while(sc.hasNextLine()) {

            id = sc.nextLine();
            info = sc.nextLine();
            amount = Integer.parseInt(sc.nextLine());

            Gameobject s = new Gameobject(id, info, amount);

            games.add(s);
            System.out.println(s.getId());
         }

        sc = new Scanner(System.in);

        info(sc.next());

     }

The info method is as follows.

public static void info(String id){  
    for(Gameobject s : games){    
        if(s.getId().equals(id)){
            System.out.println(s.getInfo());
        }
    }
}

And the class Gameobject

public class Gameobject {

    String id, info;
    int amount;

    Gameobject(String id, String info, int amount) {
        this.id = id;
        this.info = info;
        this.amount = amount;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getInfo() {
        return info;
    }
    public void setInfo(String info) {
        this.info = info;
    }
    public int getAmount() {
        return amount;
    }
    public void setAmount(int amount) {
        this.amount = amount;
    }
}

I did some tests and passed in games.get(0).getId() to the info method and it worked. That got me the ID "0001" from the Gameobject. However just passing in "0001" did not.

alex Hexan
  • 115
  • 2
  • 6
  • 2
    Maybe your `objects.txt` file is malformatted or something. Please, include it in your post. – Lefteris008 Nov 11 '16 at 08:42
  • *"ISSUE is all the way at the bottom."* It would be much better to put it at the top. – T.J. Crowder Nov 11 '16 at 08:42
  • @DavidWallace: Presumably he means the enhanced `for` loop in the `info` method. – T.J. Crowder Nov 11 '16 at 08:43
  • @T.J.Crowder Well don't I feel foolish. Completely missed it. – Dawood ibn Kareem Nov 11 '16 at 08:44
  • You should also probably include the text file you're using, if it's not too long. Additional tip: never declare a variable of type `ArrayList`, make it a `List` and then when initializing it use the desired list implementation: `List myList = new ArrayList()`. – dabadaba Nov 11 '16 at 08:46
  • 1
    @alexHexan The very best thing you could do with this is learn to use the debugger. This is a very important skill if you are to become a professional programmer, so why not learn it now? – Dawood ibn Kareem Nov 11 '16 at 17:07
  • @dabadaba Very well, but what is the difference? – alex Hexan Nov 11 '16 at 17:15
  • 1
    @alexHexan a `List` is an interface, and an `ArrayList` is one of its implementations. See the full description of a [List](http://docs.oracle.com/javase/8/docs/api/java/util/List.html) and the [ArrayList](http://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html). And [http://stackoverflow.com/questions/9852831/polymorphism-why-use-list-list-new-arraylist-instead-of-arraylist-list-n] is a more in-depth explanation on why you should use a `List` and I totally recommend you read it, as you're a learner. – dabadaba Nov 11 '16 at 17:19

1 Answers1

2

I am with Lefteris008 on this one. Check your objects.txt.

Your IDs are Strings and inside your for-loop you compare Gameobject.getId() to a console input.

Having your objects.txt contain the id 0001<space> instead of 0001 is enough for .equals(id) to yield false.

If there is a chance that your input will be malformed, you could try to make your code act more defensive:

// This will remove leading and trailing whitespaces
id = sc.nextLine().trim();

This is just to help you get an idea of how to handle your input data. This is far from production ready.

Oli
  • 830
  • 7
  • 20
  • Checked for extra spaces and also added a .trim() method to the id just to be sure and nothing. – alex Hexan Nov 11 '16 at 09:16
  • Ok, then please add your objects.txt to your question. Next you should make sure that it is not the 'read from input' part giving you troubles by replacing `sc.next()` by `0001`. Have you tried to debug your code? – Oli Nov 11 '16 at 09:21
  • @alexHexan you say your input file has no extra spaces, yet I see an extra space in every single line – dabadaba Nov 11 '16 at 09:22
  • I did some tests before posting it here. The parameter I passed in the method was `games.get(0).getId();` That got me "0001" and it worked. However, it did not work by just passing in "0001" to the method. – alex Hexan Nov 11 '16 at 09:30
  • @alexHexan What method? You were trying to do `games.get("0001")`? – dabadaba Nov 11 '16 at 09:41
  • @dabadaba The _**info**_ method. – alex Hexan Nov 11 '16 at 09:47
  • @alexHexan just make sure it doesn't have spaces... check that `games.get(0).getId()` has none either with `.contains(' ')` or see if the length of the string is 4. – dabadaba Nov 11 '16 at 10:00
  • @alexHexan Add the following line inside the for loop in your info method (just before the if statement): `System.out.printf("DEBUG \"%s\" (game ID) %s= \"%s\" (input ID)%n", s.getId(), (s.getId().equals(id) ? '=' : '!'), id);` – Klitos Kyriacou Nov 11 '16 at 11:16
  • @KlitosKyriacou after adding the line and running the first line returned for "0001" is `DEBUG "0001" (game ID) != "0001" (input ID)` I really don't understand what the problem is. – alex Hexan Nov 11 '16 at 15:57
  • 1
    @alexHexan OK then use this instead: `System.out.printf("DEBUG \"%s\" (game ID) %s= \"%s\" (input ID)%n", Arrays.toString(s.getId().toCharArray()), (s.getId().equals(id) ? '=' : '!'), Arrays.toString(s.getId().toCharArray()));` – Klitos Kyriacou Nov 11 '16 at 16:25
  • @KlitosKyriacou Seems to add in the beginning of the first one. `DEBUG "[, 0, 0, 0, 1]" (game ID) != "[, 0, 0, 0, 1]" (input ID)` Tried creating a new file and the same happens. File format is UTF-8. – alex Hexan Nov 11 '16 at 16:35
  • 1
    @alexHexan Are use using Notepad on Windows to create the UTF-8 file? If so, then your problem is that Notepad is putting a Unicode Byte Order Mark at the beginning of each UTF-8 file it creates. This is an invisible character that is ignored by Notepad, but is not ignored by Java applications. Hence, that invisible character ends up as the first character of your ID string. Try saving as ASCII or ISO-Latin-1 ("ANSI") instead. – Klitos Kyriacou Nov 11 '16 at 17:19
  • @KlitosKyriacou That was it! Thank you very much! – alex Hexan Nov 11 '16 at 17:26