Results 1 to 2 of 2
  1. #1
    bchinfosieeuw is offline Member
    Join Date
    Feb 2012
    Posts
    1
    Rep Power
    0

    Default Transpose string[][] input file

    Hi
    I'd like to know why my program does not apply the transpose method.
    Input is like
    C0,F0,D0,E0,G0,B0
    C1,F1,D1,E1,G1,B1
    C2,F2,D2,E2,G2,B2
    C3,F3,D3,E3,G3,B3
    C4,F4,D4,E4,G4,B4
    C5,F5,D5,E5,G5,B5

    I want output like
    C0
    C1
    C2
    C3
    C4
    C5
    F0
    F1
    etc.

    The commas are handled by the .split(",")

    Here is the program:
    import java.io.*;
    import java.util.*;


    public class SwapIO {


    public static String[][] transpose(String [][] a) {
    int r = a.length;
    int c = a[r-1].length;
    String [][] t = new String[c][r];
    for(int i = 0; i < r; ++i) {
    for(int j = 0; j < c; ++j) {
    t[j][i] = a[i][j];
    }
    }
    return t;

    }



    public static void main(String[] args) {
    // TODO Auto-generated method stub
    FileOutputStream out; // declare a file output object
    PrintStream p; // declare a print stream object

    try
    {
    // Create a new file output stream
    // connected to "myfile.txt"
    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());

    // Connect print stream to the output stream
    p = new PrintStream( out );

    // p.println ("This is written to a file");
    Scanner sc = null;
    try {
    sc = new Scanner(new DataInputStream(new FileInputStream(args[0])));
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }

    try {
    // Scanner input = new Scanner(new File("array.txt"));
    Scanner input = new Scanner(new DataInputStream(new FileInputStream(args[0])));
    int m = count(args[0]);
    int n = count(args[0]);
    int place;
    int string;
    String[][] a = new String[m][n];
    // while (input.hasNextLine()) {

    for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
    try{// System.out.println("number is ");

    String txt = input.next();
    String[] tmp = txt.split(",");
    String[][] proxies = new String[m][n];
    String[][] chords = new String[count(args[0])][count(args[0])];


    for(int ii = 0; ii<tmp.length; ii=ii+1){
    String[] proxy = tmp[ii].split(",");
    for(int k = 0; k<proxy.length; k++){
    proxies[ii][k] = proxy[k];
    System.out.println(proxies[ii][k]);

    // for( place = 0 ; place < proxy.length ; place++ ){
    // for( string = 0 ; string < proxy.length ; string++ ){
    // proxies[k][ii] = proxies[ii][k];
    chords = transpose(proxies);//why does it not apply transpose?


    p.println(chords[k][ii]);
    // }
    // }

    }

    }




    // System.out.println("number is "+ str[i][j]);
    }
    catch (java.util.NoSuchElementException e) {
    // e.printStackTrace();
    }
    }
    } //print the input matrix
    System.out.println("The input sorted matrix is : ");
    for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
    String[][] chords = new String[count(args[0])][count(args[0])];

    for( place = 0 ; place < n ; place++ ){
    for( string = 0 ; string < n ; string++ ){
    chords[n - 1 - place][string] = a[string][place];
    }
    }
    System.out.println(chords[i][j]);
    // p.println(chords[i][j]);
    }
    }
    // }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    catch (Exception e)
    {
    System.err.println ("Error writing to file");
    }
    }

    public static int count(String filename) throws IOException {
    InputStream is = new BufferedInputStream(new FileInputStream(filename));
    try {
    byte[] c = new byte[1024];
    int count = 0;
    int readChars = 0;
    while ((readChars = is.read(c)) != -1) {
    for (int i = 0; i < readChars; ++i) {
    if (c[i] == '\n')
    ++count;
    }
    }
    return count;
    } finally {
    is.close();
    }
    }



    public static final boolean stringToBoolean(String r) {

    if (r.equals("0")) {return false;}

    r = r.toLowerCase();

    if (r.equals("false")) {return false;}

    if (r.equals("f")) {return false;}

    if (r.equals("none")) {return false;}

    if (r.equals("not")) {return false;}

    if (r.equals("no")) {return false;}

    if (r.equals("n")) {return false;}

    if (r.equals("zero")) {return false;}

    if (r.equals("z")) {return false;}

    else {return true;}

    }



    public static void shln(String s) {

    System.out.println(s);

    }



    public static void show(String s) {

    System.out.print(s);

    }



    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,772
    Blog Entries
    7
    Rep Power
    21

    Default Re: Transpose string[][] input file

    edit: sorry, I didn't read your code close enough.

    kind regards

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. User string input?
    By synister1337 in forum New To Java
    Replies: 10
    Last Post: 01-31-2012, 11:01 PM
  2. Input output without string class
    By surenq1 in forum New To Java
    Replies: 4
    Last Post: 06-13-2010, 04:18 PM
  3. need help in string to input
    By eyeore in forum New To Java
    Replies: 4
    Last Post: 01-11-2010, 12:07 PM
  4. How to Mask input string
    By nmc.091 in forum New To Java
    Replies: 1
    Last Post: 02-23-2009, 04:49 AM
  5. How to transpose a 2D array
    By kian_hong2000 in forum Java 2D
    Replies: 1
    Last Post: 08-27-2008, 07:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •