Note that there are some explanatory texts on larger screens.

plurals
  1. POTranspose arrayLists with .split(",")
    primarykey
    data
    text
    <p>I have a program reading in arrayLists and transposing it. The input file is<BR> <code>C0 F0 D0 E0 G0 B0</code><BR> <code>C1 F1 D1 E1 G1 B1</code><BR> <code>C2 F2 D2 E2 G2 B2</code><BR> <code>C3 F3 D3 E3 G3 B3</code><BR> <code>C4 F4 D4 E4 G4 B4</code><BR> <code>C5 F5 D5 E5 G5 B5</code><BR> I want to know how to change the program so that the input is<BR> <code>C0,F0,D0,E0,G0,B0</code><BR> <code>C1,F1,D1,E1,G1,B1</code><BR> <code>C2,F2,D2,E2,G2,B2</code><BR> <code>C3,F3,D3,E3,G3,B3</code><BR> <code>C4,F4,D4,E4,G4,B4</code><BR> <code>C5,F5,D5,E5,G5,B5</code><BR> So somewhere the .split(",") has to be used. I'd also like to know how to make the program read in a general number of columns. So how 2-dimensional arrayLists have to be used? Here is the code:<BR></p> <pre><code>import java.util.*; import java.lang.*; import java.io.*; public class list { public static void main(String[] args) { FileOutputStream out; PrintStream p; try { File file = new File(args[0]); String fileName= file.getName(); int len = fileName.length(); StringBuffer sb = new StringBuffer(fileName); sb.replace(len-4,len,".hr"); out = new FileOutputStream(sb.toString()); p = new PrintStream( out ); try { List&lt;String&gt; column1 = new ArrayList&lt;String&gt;(); List&lt;String&gt; column2 = new ArrayList&lt;String&gt;(); List&lt;String&gt; column3 = new ArrayList&lt;String&gt;(); List&lt;String&gt; column4 = new ArrayList&lt;String&gt;(); List&lt;String&gt; column5 = new ArrayList&lt;String&gt;(); List&lt;String&gt; column6 = new ArrayList&lt;String&gt;(); Scanner myfile = new Scanner(new DataInputStream(new FileInputStream(args[0]))); while (myfile.hasNext()) { column1.add(myfile.next()); column2.add(myfile.next()); column3.add(myfile.next()); column4.add(myfile.next()); column5.add(myfile.next()); column6.add(myfile.next()); } myfile.close(); p.println(column1); p.println(column2); p.println(column3); p.println(column4); p.println(column5); p.println(column6); } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { System.err.println ("Error writing to file"); } } // End of MAIN } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload