Results 1 to 7 of 7
  1. #1
    yuri shibuya is offline Member
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Default problem with reading input from a textfile

    i want to read the input from text file with the format (state<space>input<space>next state)
    s0 a s1
    s1 a s2
    s2 a s2
    s1

    i'm only able to read the file line by line....but i want is to read the file by columns...
    please help me...

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: problem with reading input from a textfile

    You can still read the file line by line, but after reading a line you can split it into separate columns; read the details for the String.split( ... ) method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    yuri shibuya is offline Member
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Default Re: problem with reading input from a textfile

    thanx Jos...
    another problem...
    i'm trying to make a 2-d array from the text file as above...
    the array should be like this....
    s0 s1 s2
    s0 0 1 0
    s1 0 0 1
    s2 0 0 1

    this is what i've tried so far....

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.PrintWriter;
    import java.io.IOException;

    public class splitArray
    {
    public static void main(String[] args)
    {
    String st;
    String st2;
    String st3;
    String st4;

    try
    {
    BufferedReader br = new BufferedReader(new FileReader("fsm.txt"));

    st = br.readLine();
    st2 = br.readLine();
    st3 = br.readLine();
    st4 = br.readLine();

    String str[]=st.split(" ");
    String str2[]=st2.split(" ");
    String str3[]=st3.split(" ");
    String str4[]=st4.split(" ");

    PrintWriter out = new PrintWriter("output.txt");

    for(int i=0;i<str.length;i++)
    {
    System.out.print(str[i] + " ");
    }
    System.out.println();
    for(int i=0;i<str2.length;i++)
    {
    System.out.print(str2[i] + " ");
    }
    System.out.println();
    for(int i=0;i<str3.length;i++)
    {
    System.out.print(str3[i] + " ");
    }
    System.out.println();
    for(int i=0;i<str4.length;i++)
    {
    System.out.print(str4[i] + " ");
    }
    System.out.println("");
    System.out.println("");

    //create 2-D array
    String[][] a2=new String[4][4];

    a2[0][0]=" ";
    a2[0][1]="s0";
    a2[0][2]="s1";
    a2[0][3]="s2";
    a2[1][0]="s0";
    a2[2][0]="s1";
    a2[3][0]="s2";

    //to insert value into array
    for (int i=0; i<a2.length; i++)
    {
    for (int j=0; j<a2[i].length; j++)
    {
    if((a2[1][0] == "s0"))// && (str[0] == "s0"))// && (a2[0][j+1] == "s1"))
    {
    a2[1][1] = "1";
    System.out.print(" " + a2[i][j]);
    }
    //System.out.print(" " + a2[i][j]);
    }
    System.out.println("");
    }
    br.close();
    out.close();
    }

    catch(IOException e)
    {
    e.printStackTrace();
    }
    }
    }

    there is problem at the if statement.....please help me...T_T

  4. #4
    Ksharp is offline Banned
    Join Date
    Jun 2012
    Location
    Beijing,China
    Posts
    34
    Rep Power
    0

    Default Re: problem with reading input from a textfile

    Did you mean fsm.txt contains the following
    s0 a s1
    s1 a s2
    s2 a s2

    and want output.txt like :

    S0 S1 S2
    S0 0 1 0
    S1 0 0 1
    S2 0 0 1

    I code something to solve this. I know it is not best. Actually Java is not a good tool to process data ,specical complicated processing.

    Java Code:
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.BufferedReader;
    import java.io.PrintWriter;
    import java.io.IOException;
    
    
    public class splitArray{
     public static void main(String[] args) throws IOException {
      BufferedReader inputStream=null;
      PrintWriter outputStream=null;
      int row=-1;
      int _row,_column;
      String[] s={"S0","S1","S2"};
    
    
      try{
          inputStream= new BufferedReader(new FileReader("fsm.txt"));
          outputStream = new PrintWriter(new FileWriter("output.txt"));
          String l;
                while ((l = inputStream.readLine()) != null) {
                    int[] a={0,0,0};
                    row++;
                    _row=Integer.parseInt(l.substring(1,2)); 
                    _column=Integer.parseInt(l.substring(6));
                    a[_column]=1;
                    if (row == 0) outputStream.println("   S0 S1 S2");  
                    outputStream.println(s[row]+"  "+a[0]+"  "+a[1]+"  "+a[2]);
                }
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            }
    }
    }





    Ksharp

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: problem with reading input from a textfile

    Please don't spoonfeed the OP.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Ksharp is offline Banned
    Join Date
    Jun 2012
    Location
    Beijing,China
    Posts
    34
    Rep Power
    0

    Default Re: problem with reading input from a textfile

    Jos,
    Sorry, I just want to enhace my Java Skill. And try to solve it.
    Actually I am a beginner of Java too.

    Ksharp

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

    Default Re: problem with reading input from a textfile

    You can do that by not posting a solution.

    Java is also very good at processing data.
    Wouldn't be much use otherwise.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. reading particular line from textfile file
    By karrydhawan in forum New To Java
    Replies: 3
    Last Post: 01-05-2012, 12:22 PM
  2. Problem reading input from cmd.
    By peachyco in forum New To Java
    Replies: 9
    Last Post: 10-14-2010, 05:59 AM
  3. Reading a textfile
    By Bomber_Will in forum New To Java
    Replies: 8
    Last Post: 01-15-2010, 09:18 AM
  4. Problem in reading HTML input field while uploading file
    By sudipanand in forum Java Servlet
    Replies: 1
    Last Post: 11-27-2008, 09:26 AM
  5. reading textfile from java problem
    By saytri in forum New To Java
    Replies: 1
    Last Post: 01-17-2008, 02:13 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
  •