0

So i'm taking an input file containing strings like:

birthday54
happy75
nifty43
bob1994

These strings are part of an ArrayList. I want to pass this ArrayList through a method that can take each individual string and print them out individually. So basically, how do i take an ArrayList of strings, separate each individual string, and then print those strings? In my code, my while loop condition is true so i have an infinite loop going here and it's only outputting the first string "birthday54" infinitely. I don't know what condition i should have for the while loop. Or if i should even have a while loop. Here is my code:

    public static void convArrListToString(ArrayList<String> strings){
            int i=0;
            while (true){
                 String[] convert = strings.toArray(new String[i]);  
                 System.out.println(convert[i]);
                }  
            }
    public static void main(String [] args) 
{
    Scanner in = new Scanner(new File("myinputcases.txt"));
    ArrayList<String> list = new ArrayList<String>();
    while (in.hasNext())
        list.add(in.next());

    convArrListToString(list);
3
  • What you're doing is just crazy... Commented Jan 18, 2017 at 2:14
  • lol i know. my main purpose of redundantly converting this arraylist into strings is so i can use the strings to convert them into their ascii equivalent, but i've already figured that part out. Commented Jan 18, 2017 at 2:16
  • how do i take an ArrayList of strings, separate each individual string... An list of strings is already separated. It's not clear what you're trying to. Commented Jan 18, 2017 at 2:17

3 Answers 3

1

I believe you only need to iterate the ArrayList and use the "Get" method to get each string like:

for(int i = 0 ; i < list.size(); i++){ 
   System.out.println(list.get(i)); 
}

or you can use the for each loop

for(String s : list){ 
 System.out.println(s);
}

cheers!

Sign up to request clarification or add additional context in comments.

Comments

0

Change this:

while (true) {
    String[] convert = strings.toArray(new String[i]);  
    System.out.println(convert[i]);
}  

To this:

for (String strTemp : strings) {
    System.out.println(strTemp);
}

It only ouputs "birthday54" because you didn't increment your i. You can increment it by putting i++ on the end of your while statement, but you will get an error if you do that after you iterate all of your values in your ArrayList. Look my answer, you can simply use for loop to iterate that ArrayList.

9 Comments

Surely you mean strings.forEach(System.out::println);.
@shmosel yeah, that's absolutely short than my answer. thanks :)
hey @shmosel, how can i take those strings that i just printed (using your code) and pass them through another different function?
@user7428413 If it's a static method, you can do strings.forEach(ClassName::methodName);
@shmosel, you are an absolute genius. one more question, if i implement that code you just wrote and pass it in a function that utilizes a for loop, is the run time O(n^2) or O(n)?
|
0

Man that's painful to look at, Try this instead of your while loop:

for (String s : strings) { 
     System.out.println(s); 
}

No while loop needed, the array list is a Collections object which is a container class in Java that can be iterated by object as well as by index, so this should pull each string out 1 by 1 until it approaches the end of the array list.

Resource on collections in java.

Comments

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.