Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

ArrayList of Character data  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im trying to create a code that uses an ArrayList of Character data where the user enters a string and then each char in the string is represented as a separate character element in the list. Each character should them be displayed back in a separate line each.  This is what I have so far but don't know how to get the string and then get each individual char in that string.


import java.util.ArrayList;
import java.util.Scanner;
public class StringSlicer {

   
   public static void main(String[] args) {
       Scanner scnr = new Scanner(System.in);
       ArrayList<Character> userString = new ArrayList<Character>();
       
       System.out.println("Enter a word: ");
       
       while (scnr.hasNextLine()){
           userString.add(scnr.nextLine());
       }
       
       
   }
   
}
 
Marshal
Posts: 63842
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by turning your computer off. You cannot work out that sort of thing in code. Work it out on paper, saying how you would do it without using any programming words. You may need a different kind of data structure from a List.

Don't use while (scnr.hasNextLine()) ... when reading from System.in. That test will implicitly return true, so you have written an infinite loop. Also, don't use while (scnr.hasNext()) ... for the same reason. That problem only applies to inputs without a defined endpoint, so that sort of test is all right when reading from a file.
 
Irvin Florencia
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried writing it out and the only thing I could get was that after asking for input from the command line, I would put String userString = scnr.nextLine() as this would get the string from the input. I just don't know how to take that string and put it into the ArrayList as they are two different types.
 
Irvin Florencia
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was able to get it done after trying several things.
 
Campbell Ritchie
Marshal
Posts: 63842
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what was the best solution?
 
Morning came much too soon and it brought along a friend named Margarita Hangover, and a tiny ad.
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!