Converting Scanner to ArrayList

Discussion in 'Plugin Development' started by CrazyGuy3000, Dec 2, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    CrazyGuy3000

    Hey Bukkit,
    Just wondering how I convert my scanner into a ArrayList, my method looks like the following.
    Code:java
    1. URL url = new URL("[url]http://www.puzzlers.org/pub/wordlists/pocket.txt[/url]");
    2. Scanner s = new Scanner(url.openStream());
     
  2. Offline

    felixfritz

    You'd have to create a seperate ArrayList for that and read out the contents with the scanner. What are you planning to save in the list?
    Code:java
    1. List<String> list = new ArrayList<String>();
    2.  
    3. while(s.hasNextLine())
    4. list.add(s.nextLine());
    5.  
     
  3. Offline

    Not2EXceL

    I'm still hung on why you're using a scanner to read from a url.

    Use a BufferedReader with an InputStreamReader
     
    Wingzzz likes this.
  4. Offline

    CrazyGuy3000

    Not2EXceL felixfritz
    Sorry guys for that late reply,
    You didn't tahg me so I was unaware there was replys.

    I am trying to store a list of ops.txt that can be used globally in a network,
    Not2EXceL can you show me a example of a "BufferedReader" ive never
    used one.
     
  5. Offline

    Not2EXceL

    CrazyGuy3000
    Here's an example. Its from an old lib I was working on for myself. The example is old, and needs a rework. However it does show a basic usage of a BufferedReader and BufferedWriter
    Show Spoiler

    Code:java
    1. package com.not2excel.lib.io.manager;
    2.  
    3. import java.io.*;
    4. import java.util.LinkedList;
    5. import java.util.List;
    6.  
    7. /**
    8. * @author Richmond Steele
    9. * @since 9/12/13
    10. * All rights Reserved
    11. * Please read included LICENSE file
    12. */
    13. public class TextManager implements IOManager
    14. {
    15. /**
    16.   * File to manage
    17.   */
    18. private File file;
    19.  
    20. /**
    21.   * Write stream object
    22.   */
    23. protected BufferedWriter bufferedWriter;
    24.  
    25. /**
    26.   * Read stream object
    27.   */
    28. protected BufferedReader bufferedReader;
    29.  
    30. /**
    31.   * Constructor
    32.   * @param file file to manage
    33.   */
    34. public TextManager(final File file)
    35. {
    36. this.file = file;
    37. }
    38.  
    39. /**
    40.   * Returns a new instance of this class
    41.   * @param file file to manage
    42.   * @return instance of this class
    43.   */
    44. public static TextManager newInstance(final File file)
    45. {
    46. return new TextManager(file);
    47. }
    48.  
    49. /**
    50.   * Creates a new Buffered Writer
    51.   */
    52. @Override
    53. public void setupWrite()
    54. {
    55. try
    56. { bufferedWriter = new BufferedWriter(new FileWriter(file)); }
    57. catch (final IOException e)
    58. { e.printStackTrace(); }
    59. }
    60.  
    61. /**
    62.   * Creates a new Buffered Reader
    63.   */
    64. @Override
    65. public void setupRead()
    66. {
    67. try
    68. { bufferedReader = new BufferedReader(new FileReader(file)); }
    69. catch (final FileNotFoundException e)
    70. { e.printStackTrace(); }
    71. }
    72.  
    73. /**
    74.   * Closes all streams
    75.   */
    76. @Override
    77. public void close()
    78. {
    79. try
    80. {
    81. if (bufferedReader != null)
    82. { bufferedReader.close(); }
    83.  
    84. if (bufferedWriter != null)
    85. { bufferedWriter.close(); }
    86. }
    87. catch (final IOException e)
    88. { e.printStackTrace(); }
    89. }
    90.  
    91. /**
    92.   * Reads one line from the file
    93.   * @return line
    94.   */
    95. protected String readLine()
    96. {
    97. try
    98. { return bufferedReader.readLine(); }
    99. catch (IOException e)
    100. { e.printStackTrace(); }
    101. return null;
    102. }
    103.  
    104. /**
    105.   * Writes a given line to the file
    106.   * @param line line
    107.   */
    108. protected void writeLine(final String line)
    109. {
    110. try
    111. {
    112. bufferedWriter.write(line);
    113. bufferedWriter.newLine();
    114. }
    115. catch (IOException e)
    116. { e.printStackTrace(); }
    117. }
    118.  
    119. /**
    120.   * Reads all strings from a file
    121.   * @return list of strings
    122.   */
    123. protected List<String> readFile()
    124. {
    125. final List<String> stringList = new LinkedList<String>();
    126. setupRead();
    127. String s;
    128. while((s = readLine()) != null)
    129. {
    130. if (!s.startsWith("##"))
    131. { stringList.add(s.trim()); }
    132. }
    133. close();
    134. return stringList;
    135. }
    136.  
    137. /**
    138.   * Writes a list of strings to file
    139.   * @param stringList list of strings
    140.   */
    141. protected void writeFile(final List<String> stringList)
    142. {
    143. setupWrite();
    144. for (final String s : stringList)
    145. { writeLine(s); }
    146. close();
    147. }
    148. }
    149.  

     
  6. Offline

    CrazyGuy3000

    Can you show another example that is easier to read? Im struggling to understand that one o_o
     
  7. Offline

    Cirno

    BufferedReader br = new BufferedReader(InputStream thingy);
    ArrayList<String> str = new ArrayList<String>();
    String l;
    while((l = br.readLine()) != null){
    str.add(l);
    }

    br.close();
     
Thread Status:
Not open for further replies.

Share This Page