#Can I get a String input without a Scanner?

14 messages · Page 1 of 1 (latest)

warm lava
#

(This is for homework but I've been going back through my online textbook which is impossible to check). My code is working correctly except for one issue - I need the String value to have two iterations but can't print the Array list twice.

I used the following code just to make sure it would work correctly:
​ if (i != myList.size()) { s = ", "; for (i = 0; i < myList.size() - 1; ++i) { System.out.print(myList.get(i) + s); } System.out.print(myList.get(i)); } ​
But I need it to print like this for one test:
1, 2, 3, 4, 5, 6
And like this for a second:
1 - 2 - 3 - 4 - 5 - 6

I tried using a Scanner but then I get no output so that seems incorrect. I can get one logic test to work but the other fails. Am I missing another way to set s to multiple choices (it's supposed to be a String argument for the method)?

It was recommended to me that I get the loop to run through twice but I can't figure how to and the line can't print twice.

west hollowBOT
#

This post has been reserved for your question.

Hey @warm lava! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

west hollowBOT
past spire
warm lava
past spire
#

The cod you showed is just printing all elements from a Collection called myList that are separated by a ,.

  1. You are mentioning tests. What format do theses tests have ?
  2. How are you getting the values of the list ? From a file ? From the command line ?
warm lava
# past spire The cod you showed is just printing all elements from a `Collection` called `myL...
  1. They're logic tests so if they enter printArrayList(myList, ", ") or printArrayList(myList, " - ") it should print.

  2. The ArrayList needed to hold {1, 22, 333, 400, 5005, 9} and I added each number by myList.add(index, number). They have been added through the method.
    ​`
    public static void printArrayList (ArrayList<Integer>myList, ArrayList<String>s) {
    myList = new ArrayList<Integer>();
    s = new ArrayList<String>();

    myList.size();
    myList.add(0, 1);
    myList.add(1, 22);
    myList.add(2, 333);
    myList.add(3, 400);
    myList.add(4, 5005);
    myList.add(5, 9);

    int i = 0;

    if (i != myList.size()) {
    s = ", ";
    for (i = 0; i < myList.size() - 1; ++i) {
    System.out.print(myList.get(i) + s);
    }
    System.out.print(myList.get(i));
    }

#

that's the full code

past spire
#

ok now I get you. First and formeost. You should not create myList inside the method printArrayList but it should be passed to it as an argument. Secondly, your second parameter is wrong. Your method header should be:
printArrayList(ArrayList<Integer> myList, String delimiter).
Honestly I am suprised that it compiled because this line s = ", "; should throw a compile error.

warm lava
#

Ok, I see what I did wrong. I have the String s but I tried to define it and I didn't need to. Basically, I did too much so I got it.

#

@past spire Thank you for your help!

west hollowBOT