Home > other >  Wondering why my arraylist would store values in separated lines
Wondering why my arraylist would store values in separated lines

Time:06-19

As the topic has said. I'm currently working on a text adventure game and my ArrayList somehow doesn't work for me. I think my ArrayList didn't stored the ArrayList values properly. Code is provided here:

ArrayList<String> actionsList = new ArrayList<>();

public void setActions() {
    //counter variable;
    int i =0;
    try {
    Scanner sc = new Scanner(new File("Action.txt"));
    sc.useDelimiter("\n");      
    while (sc.hasNextLine()){
        String name = sc.next();
        if (sc.hasNext()) {
            actionName[i] = name;
            i   ;
        } else {
            break;
        }
    }
    
    //set name of actions to action objects created above.
    take.setName(actionName[0]);
    eat.setName(actionName[1]);
    drink.setName(actionName[2]);
    drop.setName(actionName[3]);
    wear.setName(actionName[4]);
    give.setName(actionName[5]);
    use.setName(actionName[6]);
    help.setName(actionName[7]);
    look.setName(actionName[8]);
    
    sc.close();
    } catch (IOException e) {
        System.out.println("The action file is missing.");
    }
}
    
public void addToGameList() {
    actionsList.add(take.name);
    actionsList.add(eat.name);
    actionsList.add(drink.name);
    actionsList.add(drop.name);
    actionsList.add(wear.name);
    actionsList.add(give.name);
    actionsList.add(use.name);
}

The program is reading the action names from action.txt file and set the values into the name of the action class's different instances. And the add method would add the name of these actions into the list. The file is just a List of actions:

take
eat
drink
drop
wear
give
use
help
look

formulated like this. I tried every part of the code and I found out that this code wasn't working as expected. For printing out arraylist's content. They should be formated like this

[xxx,xxxxx,xxxxx]

But for mine arraylist they are like

[xxx
,xxxx
,xxxxx
,xxxxxx
]
[take
, eat
, drink
, drop
, wear
, give
, use
]

This is what the out put was and my print code was like.

System.out.print(game.actionsList);

I really don't know what was going on, and it was exactly the same bug in my other ArrayList which is for storing the items' name.

CodePudding user response:

I suspect that the root of the problem is this:

    sc.useDelimiter("\n");      

The thing is that on Windows, the standard line termination sequence in a text tile is \r\n. So your custom delimiter is causing the \r characters at the end of each line on the "Action.txt" to be part of the action strings ... when you read them with Scanner.next

Possible solutions (depending on how the file is formatted ...):

  1. Don't change the delimiter.
  2. Use Scanner.nextLine rather than Scanner.next to read the action strings ... which renders the delimiter moot.
  3. Use name = name.trim(); to get rid of (all) pesky leading and trailing whitespace from the action strings.

CodePudding user response:

Try to see the output of name first

....
String name = sc.next();
System.out.println("#" name "#");
...

It should be formatted like

#take#

instead of

#take
#

For the answer, your scanner might accidentally take line endings in addition to the words. You can trim your input before inserting into the array list

...
String name = sc.next();
name = name.trim();

  • Related