Store boolean array as text file

Hi there
I'm having some trouble storinge a 25x25 2D array of booleans in a text file.
First I make the 2D array a 1D array, and then I change it to a number, but here something already goes wrong, not the full number is stored, presumably because the array is this big?
Any help will be much appreciated!
Solved!
Go to Solution.
Attachments:
Store 2D boolean array LV10.vi ‏17 KB

You can also convert the 2D boolean array into 1D array of numbers and save to text file and then read back to load the original 2D Boolean array
The best solution is the one you find it by yourself
Attachments:
2D Boolean to String.vi ‏16 KB

Similar Messages

  • Write Text Data Array to text file

    Greetings all. I hope someone can help me as I am really under the gun. The attached vi shows the basics of what I am trying to do. I have already written a vi that takes the Cal Data Array and prints it out in a nicely formatted report. My problem is that the powers that be also want the data saved to a generic text file that can be copied and printed out anywhere they like. As such, I need to save the data to a generic text file in column format such that it will all fit on one page in landscape mode. There are a total of 12 columns of data. I have been trying to create something that would format each column to a specific length instead of them all being the same. No luck so far. Basically, I need columns 1,2,3,8 and 12 to be length of 5. The rest a length of 9. I have tried to place the formatting part in a for loop with the formatting in a case, but it does not appear to work. I really need this quick so if anyone has any ideas, please help. As always, I really appreciate the assistance.
    Thanks,
    Frank
    Attachments:
    Write Cal Data to Text File.vi ‏21 KB

    pincpanter's is a good solution. Beat me to it while I was away building an example. Similiar approach using two for loops and case statement. Here is my suggestion anyway....
    cheers
    David
    Message Edited by David Crawford on 11-23-2005 09:37 AM
    Attachments:
    Write Text Data Array to text file.vi ‏31 KB

  • Using ArrayList to store Lines in a text File

    Hey Guys,
    I have the following program that I am using to read a text file and store each word in each line in an ArrayList. But I am getting the following error;
    Exception in thread "main" java.util.ConcurrentModificationException
         at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:449)
         at java.util.AbstractList$Itr.next(AbstractList.java:420)
         at bus.OrderReader.parseLine(OrderReader.java:50)
         at bus.OrderReader.main(OrderReader.java:32) The code is as follows;
    package bus;
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.Scanner;
    import java.util.Collection;
    import java.util.ArrayList;
    import java.util.Iterator;
    public class OrderReader {
        static Collection bread = new ArrayList();
        static Collection filling = new ArrayList();
        static Iterator iter1 = filling.iterator();
        static Iterator iter2 = bread.iterator();
         private static int counter;
    public static void main(String args[]){
        Scanner s = null;
        try {
            s = new Scanner(new BufferedReader(new FileReader("/Users/admin/Documents/workspace2/OBS/WebContent/WriteMe.txt")));
            s.useDelimiter("\r");
            while(s.hasNext()) {
                parseLine(s.next());
                counter++;
            s.close();
        catch(FileNotFoundException e) {
            System.out.println("cannot find the file");
            //ignore for now
        public static void parseLine(String line) {
            Scanner lineScanner = new Scanner(line);
            lineScanner.useDelimiter("#");
                bread.add(lineScanner.next());
                filling.add(lineScanner.next());
                System.out.println(iter1.next());
                System.out.println(iter2.next());
    }I do not understand why this does not work. Can someone please point me in the right direction?
    Thank You
    Jaz

    Instead of doing this:
    bread.add(lineScanner.next());
    filling.add(lineScanner.next());
    System.out.println(iter1.next());
    System.out.println(iter2.next());where presumably you're just printing what you just
    barely added, do this:
    Object breadObject = lineScanner.next();
    Object fillingObject = lineScanner.next();
    bread.add(breadObject);
    filling.add(fillingObject);
    System.out.println(breadObject);
    System.out.println(fillingObject);
    And get rid of the now unused iterators.Im not sure I understand quiet what is happening here. Are you creating a primitive data type of type Object and then storing all the values in that variable?
    why would I do this when this only allows me to view the very last element that was added to the list? I want to view the entire list.
    Thanks
    Jaz
    Message was edited by:
    DontKnowJack

  • How to Store something in a text file.

    Store the record of the session in a text file.
    please can any one tell me how can i create this thing.
    i cerate the button but does not work correctly and i can not save any calculation/
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.lang.Math.*;
    import java.math.BigInteger;
    public class Calculator2 extends JFrame implements ActionListener{
    JLabel lN1, lN2,lN3;//here are the Label.
    JTextField tN1, tN2;// here the place for the text field.
    JButton btnAdd,btnSub,btnMul,btnDiv,btnClear,btnSave; JTextArea outArea;//all our Button which are in the program.
    Calculator2(){//constructor.
    Box box=Box.createVerticalBox();//creation of the box.
    // input
    lN1=new JLabel("Write first number"); box.add(lN1);//creation of the input,the number which we want use.
    tN1=new JTextField(100); tN1.setEditable(true); box.add(tN1);//allows or prevents the user editing the text
    lN2=new JLabel("write second number"); box.add(lN2);
    tN2=new JTextField(100); tN2.setEditable(true); box.add(tN2);
    // buttons
    btnAdd=new JButton("+"); btnAdd.addActionListener(this); box.add(btnAdd);// button of addition operation
    // setPreferredSize();
    btnSub=new JButton("-"); btnSub.addActionListener(this); box.add(btnSub);// button of subtraction operation
    btnMul=new JButton("*"); btnMul.addActionListener(this); box.add(btnMul);// button of multiplication operation
    btnDiv=new JButton("/"); btnDiv.addActionListener(this); box.add(btnDiv);// button of division operation
    btnClear=new JButton("Clear"); btnClear.addActionListener(this); box.add(btnClear);// button of Clear operation
    btnSave=new JButton("Save"); btnSave.addActionListener(this); box.add(btnSave);// button of save operation
    // Output
    String desc="This is a simple integer calculator\n";//here the out put with text area.
    outArea=new JTextArea(desc,30,60);//out put area with the hight and length.
    ScrollPane scrollPane=new ScrollPane();//Creates an empty (no viewport view) JScrollPane where both horizontal and vertical scrollbars appear when needed
    scrollPane.add(outArea);
    box.add(scrollPane);
    Container c=getContentPane();
    c.add(box);
    c.setBackground(Color.green);//back ground color.
    setTitle("Calculator");//title
    setSize(600,375);//size
    setVisible(true);// visiblity of the program.
    private void showStatus(String status) {
    lN3.setText(status);
    public void actionPerformed(ActionEvent actionEvent){
    if(actionEvent.getSource()==btnClear){
    outArea.selectAll(); outArea.cut();
    } else {
    String sN1=tN1.getText(), sN2=tN2.getText(), sOp="";
    BigInteger bN1=new BigInteger(sN1);
    BigInteger bN2=new BigInteger(sN2);
    BigInteger bRes=BigInteger.ZERO, bResDiv[]={BigInteger.ZERO,BigInteger.ZERO};
    BigInteger bRemainder=BigInteger.ZERO;
    if(actionEvent.getSource()==btnAdd){bRes=bN1.add(bN2); sOp=" + ";}
    if(actionEvent.getSource()==btnSub){bRes=bN1.subtract(bN2); sOp=" - ";}
    if(actionEvent.getSource()==btnMul){bRes=bN1.multiply(bN2); sOp=" * ";}
    if(actionEvent.getSource()==btnDiv){sOp=" / ";
                if(bN2.signum()!=0)bResDiv=bN1.divideAndRemainder(bN2);//Math.signum() tells you what sign a number is....
                //returns zero if the argument is zero, 1.0 if the argument is greater than zero,
                //-1.0 if the argument is less than zero
                bRes=bResDiv[0]; bRemainder=bResDiv[1];
    if(sOp.equals(" / ") && bN2.signum()==0){
    // showStatus("Cannot divide by zero");
    } else {
    outArea.append(bN1.toString() + sOp+bN2.toString() + " = " + bRes.toString() + "\n");
    if(actionEvent.getSource()==btnDiv)outArea.append("Remainder = "+bRemainder.toString()+"\n");
    public static void main(String[] args) {//the main method of the program.
    new Calculator2().setVisible(true);
    }

    // This is a simple calculator that calculate 4 mathematical operations, addition,
    // subtraction, multiplication and division.
    // Author: Jean Chedid.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.lang.Math.*;
    import java.math.BigInteger;
    public class Calculator2 extends JFrame implements ActionListener{
        JLabel lN1, lN2,lN3;//here are the Label.
        JTextField tN1, tN2;// here the place for the text field.
        JButton btnAdd,btnSub,btnMul,btnDiv,btnClear,btnSave; JTextArea outArea;//all our Button which are in the program.
        Calculator2(){//constructor.
            Box box=Box.createVerticalBox();//creation of the box.
            // input
            lN1=new JLabel("Write first number"); box.add(lN1);//creation of the input,the number which we want use.
            tN1=new JTextField(100); tN1.setEditable(true); box.add(tN1);//allows or prevents the user editing the text
            lN2=new JLabel("write second number"); box.add(lN2);
            tN2=new JTextField(100); tN2.setEditable(true); box.add(tN2);
            // buttons
            btnAdd=new JButton("+"); btnAdd.addActionListener(this); box.add(btnAdd);// button of addition operation
            // setPreferredSize();
            btnSub=new JButton("-"); btnSub.addActionListener(this); box.add(btnSub);// button of subtraction operation
            btnMul=new JButton("*"); btnMul.addActionListener(this); box.add(btnMul);// button of multiplication operation
            btnDiv=new JButton("/"); btnDiv.addActionListener(this); box.add(btnDiv);// button of division operation
            btnClear=new JButton("Clear"); btnClear.addActionListener(this); box.add(btnClear);// button of Clear operation
            btnSave=new JButton("Save"); btnSave.addActionListener(this); box.add(btnSave);// button of save operation
            // Output
            String desc="This is a simple integer calculator\n";//here the out put with text area.
            outArea=new JTextArea(desc,30,60);//out put area with the hight and length.
            ScrollPane scrollPane=new ScrollPane();//Creates an empty (no viewport view) JScrollPane where both horizontal and vertical scrollbars appear when needed
            scrollPane.add(outArea);
            box.add(scrollPane);
            Container c=getContentPane();
            c.add(box);
            c.setBackground(Color.green);//back ground color.
            setTitle("Calculator");//title
            setSize(600,375);//size
            setVisible(true);// visiblity of the program.
         private void showStatus(String status) {
         lN3.setText(status);
        public void actionPerformed(ActionEvent actionEvent){
            if(actionEvent.getSource()==btnClear){
                outArea.selectAll(); outArea.cut();
            } else {
                String sN1=tN1.getText(), sN2=tN2.getText(), sOp="";
                BigInteger bN1=new BigInteger(sN1);
                BigInteger bN2=new BigInteger(sN2);
                BigInteger bRes=BigInteger.ZERO, bResDiv[]={BigInteger.ZERO,BigInteger.ZERO};
                BigInteger bRemainder=BigInteger.ZERO;
                if(actionEvent.getSource()==btnAdd){bRes=bN1.add(bN2); sOp=" + ";}
                if(actionEvent.getSource()==btnSub){bRes=bN1.subtract(bN2); sOp=" - ";}
                if(actionEvent.getSource()==btnMul){bRes=bN1.multiply(bN2); sOp=" * ";}
                if(actionEvent.getSource()==btnDiv){sOp=" / ";
                if(bN2.signum()!=0)bResDiv=bN1.divideAndRemainder(bN2);//Math.signum() tells you what sign a number is....
                //returns zero if the argument is zero, 1.0 if the argument is greater than zero,
                //-1.0 if the argument is less than zero
                bRes=bResDiv[0]; bRemainder=bResDiv[1];
                if(sOp.equals(" / ") && bN2.signum()==0){
                    // showStatus("Cannot divide by zero");
                } else {
                    outArea.append(bN1.toString() + sOp+bN2.toString() + " = " + bRes.toString() + "\n");
                    if(actionEvent.getSource()==btnDiv)outArea.append("Remainder = "+bRemainder.toString()+"\n");
        public static void main(String[] args) {//the main method of the program.
            new Calculator2().setVisible(true);
    }

  • Read in array from text file

    hello,
    i have managed to save the content of a 2d array to a text file, but i am a bit stuck reading it back and placing the data back into a 2d array.
    i have a Scanner object that can read the the integers from the file using nextInt(), but i am not sure how to place them back into the 2d array.
    i need something like:
            Scanner inputStream = null;
            try{
            inputStream = new Scanner(new FileInputStream("out.txt"));
            catch(FileNotFoundException e)
                System.out.println("out.txt not found");
                e.printStackTrace();
            for(int i = 0; i < 13; i++){
                for(int j = 0; j < 13; j++)
                    matrix[i][j] = inputStream.nextInt();
            inputStream.close();but it doesnt like the for loop.
    thanks

    morris7 wrote:
    but it doesnt like the for loop.1) You'll have to be much more specific than this if you want "helpful" help.
    2) You may need to call inputStream.nextLine() after the inner for loop but inside the outer for loop.
    3) Always, always use curly brackets "{" and "}" to enclose your for loops, while loops, if and else statements. They may seem like extra work if you are only processing one line, but they will save your life someday. Always.
    4) Consider closing your scanner in a finally block.

  • Powershell script - how to read a registry hive and store the value in text file and then again read the text file to write the values back in registry

    Hi All,
    powershell script Method required to read a value from registry and then taking the backup of that values in some text file.
    For example the hive is
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path
    and under path i need to take back up  of values in some text file and then put some value in the registry after back is taken in text file.
    Also how to read the text file values so that we can again write to registry hive  back from the back up text file.
    Your help is much appreciated.
    Umeed4u

    I think you need to read this first:
    http://social.technet.microsoft.com/Forums/scriptcenter/en-US/a0def745-4831-4de0-a040-63b63e7be7ae/posting-guidelines?forum=ITCG
    Don't retire TechNet! -
    (Don't give up yet - 12,830+ strong and growing)

  • To store text file having Multiple file formats in a database table

    Hi,
    Is it possible to store data of a text file which have different field separator between records
    Like
    palash;1111,noida
    xxxx;3333,delhi
    regards
    palash

    Hi,
    Ti load:
    1/ Declare a file with ; separator. Consider you have only to columns
    2/ Create or reverse a datastore with your 3 columns
    3/ Create an interface: file in source, table in target. For you mapping, First column is mapped on the first column of the file, second and thirth ones are parts of the second column of the file...using SQL possibilities of your target DB. For that mapping are made in the workspace.
    You have to know that you have to choice a staging area near your target schema to make those transformations.
    Regards,
    MA...different from RA.... ;o)

  • How to store 2C array to a singl binary file

    Hi
    I have to store multiple waveforms output that is each 1D array to a single binary file. how do i do this.
    i can store 1D array to binary file and retrieve it by reading the binary file and plotting the waveform. but i am not able to store 2D array to the single file. how do store many 1D array in one binary file,
    Thank you. Hema

    (We would also need to know your LabVIEW version. If you have a very old version, things would be quite different.)
    As Smercurio_fc already mentioned, the option to "prepend array size" would make things much easier here, because then you would read it as a single 2D array.
    Here's how that would look like. This is probably the recommended solution, just for simplicity reasons.
    Not prepending the size is more useful if you write the data in increments, but later want to read it as one.
    Message Edited by altenbach on 11-15-2008 09:02 AM
    LabVIEW Champion . Do more with less code and in less time .
    Attachments:
    SaveRestore2D.png ‏21 KB

  • Reading words from a text file

    I have written code to store words in a text file. they appear in the text file as shown below.
    word1
    word2
    word3
    etc.
    I want to read each word individually and store them in an array. Im trying to do it using a BufferedReader but it doesn't seem to work. The code for reading the words is shown below. Any suggestions would be appreciated.
    try
    FileReader reader = new FileReader("words.txt");
    BufferedReader bReader = new BufferedReader(reader);
    ArrayList words = new ArrayList();
    String line;
    while((line = bReader.readLine()) != null)
    line = bReader.readLine();
    words.add(line);
    bReader.close();
    catch (Exception e)
    System.err.println ("Error writing to file");
    }

    I think your code is wrong, because it Read two time from file,
    your code like....
    while((line = bReader.readLine()) != null)
    line = bReader.readLine();
    words.add(line);
    you have to change the above code like
    while((line = bReader.readLine()) != null)
    words.add(line);
    NOW ITS WORKING FINE,...........

  • Output query result to a text file in ORACLE

    I Need a procedure for generating output query into text file in Oracle.
    for Example,
    A table called Tab1
    the columns are c1,c2,c3,c4,c5.
    I want to store the data in text file in...
    First row as a column name & then, appear the records in second row onwards...
    c1|c2|c3|c4|c5.
    Please kindly provide the solution for this ASAP.

    In case you are using Oracle 9i or higher you should better create a DIRECTORY rather then setting utl_file_dir,e.g.
    SQL> CREATE DIRECTORY log_dir AS '/appl/gl/log';
    SQL> GRANT READ ON DIRECTORY log_dir TO DBA; http://download-west.oracle.com/docs/cd/B10501_01/appdev.920/a96612/u_file.htm#ARPLS069

  • How do I read a text file line by line and store to array

    I have a text file, I want to be able to store each line of the file in an array, how would I go about doing this? code example? tutorial? Thank you

    Well, you got the pseudo code:
    a) read line
    b) add to array
    So whats the problem? What don't you know how to do?
    What does your text book tell you about reading files. I'm sure it has an example, every book I've read does.
    By the way I would use an ArrayList to store each line of text, that way you don't need to know in advance.

  • How to store something in text file?

    Hello
    I have question and i do not know how to do this.
    can any one explain for me how i can save something in a text file.
    i create calculator,and there is one question i need to do it:((Store the record of the session in a text file. ))
    what i need to write some codes and so on.
    please can u give idea ?

    Alena,
    Here's my static FileUtilz class, which includes a writeFile method, and several other handy methods.
    package krc.io;
    import java.util.Collection;
    import java.util.List;
    import java.util.ArrayList;
    import java.io.File;
    import java.io.FileReader;
    import java.io.BufferedReader;
    import java.io.FileWriter;
    import java.io.PrintWriter;
    import java.io.InputStream;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.FileNotFoundException;
    import java.util.Arrays;
    public abstract class FileUtilz
        public static final int bfrSize = 4096;
        public static void writeFile(String content, String filename)
        throws IOException
            PrintWriter out = null;
            try {
                out = new PrintWriter(new FileWriter(filename));
                out.write(content);
            } finally {
                //try {if(out!=null)out.close();}catch(Exception ignore){}
                out.close();
        public static String readFile(String filename)
        throws IOException, FileNotFoundException
            FileReader in = null;
            StringBuffer out = new StringBuffer();
            try {
                in = new FileReader(filename);
                char[] cbuf = new char[bfrSize];
                int n = in.read(cbuf, 0, bfrSize);
                while(n > 0) {
                    out.append(cbuf);
                    n = in.read(cbuf, 0, bfrSize);
            } finally {
                //try {if(in!=null)in.close();}catch(Exception ignore){}
                in.close();
            return out.toString();
        public  static String[] readFileIntoArray(String filename)
        throws IOException, FileNotFoundException
            return readFileIntoList(filename).toArray(new String[0]);
        public  static List<String> readFileIntoList(String filename)
        throws IOException, FileNotFoundException
            BufferedReader in = null;
            List<String> out = new ArrayList<String>();
            try {
                in = new BufferedReader(new FileReader(filename));
                String line = null;
                while ( (line = in.readLine()) != null ) {
                    out.add(line);
            } finally {
                try {if(in!=null)in.close();}catch(Exception e){}
            return out;
        public static byte[] readBytes(String filename)
        throws IOException, FileNotFoundException
          //start = System.currentTimeMillis();
          File file = new File(filename);
          byte[] out = new byte[(int)file.length()];
          InputStream in = new FileInputStream(file);
          int size = in.read(out);
          in.close();
          //System.out.println("readBytes("+filename+"="+size") took "+(System.currentTimeMillis()-start));
          return out;
        public static boolean isSameFile(String filenameA, String filenameB)
        throws IOException, FileNotFoundException
          File fileA = new File(filenameA);
          File fileB = new File(filenameB);
          //check for same physical file
          if( fileA.equals(fileB) ) return(true);
          //compare sizes
          if( fileA.length() != fileB.length() ) return(false);
          //compare contents (buffer by buffer)
          boolean same=true;
          InputStream inA = null;
          InputStream inB = null;
          try {
            inA = new FileInputStream(fileA);
            inB = new FileInputStream(fileB);
            byte[] bfrA = new byte[bfrSize];
            byte[] bfrB = new byte[bfrSize];
            int sizeA=0, sizeB=0;
            do {
              sizeA = inA.read(bfrA);
              sizeB = inA.read(bfrB);
              if ( sizeA != sizeB ) {
                same=false;
              } else if ( sizeA == 0 ) {
                //do nothing
              } else if ( !Arrays.equals(bfrA,bfrB) ) {
                same=false;
            } while (same && sizeA != -1);
          } finally {
            if(inA!=null)inA.close();
            if(inB!=null)inB.close();
          return(same);
        public static String basename(String path, boolean cutExtension)
            String fname = (new File(path)).getName();
            if (cutExtension) {
                int i = fname.lastIndexOf(".");
                if (i > 0) {
                    fname = fname.substring(0,i);
            return fname;
        public static String dirname(String path)
            return (new File(path)).getParent();
    }

  • How to save input from a text file in to array

    I got this assignment, and almost have no clue, and my teacher doesn't even teaches us anything
    This is what i am suppose to do
    5. The program must store the following data about each student in this Talent Show:
    * The student's name (store as a String)
    * The student's average time score [only for the events they competed in] (store as a Double)
    Note: Some of the data comes directly from the text file, and some of the data must be calculated from the data that is read from the file.
    6. The program must search through all of the student's scores to find the student who has the minumum average time of all students.
    7. You must calculate how many events each student played. Zero values are not counted.
    8. You must then generate a report on the screen that prints out
    * the total number of students in the competition,
    * the winning student's ...
    o name
    o times
    o average time (rounded to one decimal places)
    Sample output
    btw this is the data file
    Bart Simpson
    7.5
    12.3
    7.8
    19.2
    9.9
    5.3
    8.5
    11.8
    2.2
    4.6
    Stewie Griffin
    9.5
    29.7
    7.8
    22.5
    9.9
    12.6
    8.5
    0
    8.2
    0
    Pinky
    2.5
    0
    1.8
    0
    3.9
    0
    6.5
    0
    5.2
    12.1
    Rocky N Bullwinkle
    10.0
    22.2
    9.5
    17.5
    9.9
    1.5
    8.7
    23.7
    9.2
    11.0
    Angelica Pickles
    5.5
    11.1
    6.8
    12.2
    7.9
    13.3
    8.1
    5.1
    7.2
    7.9
    Pink Panther
    8.5
    5.5
    8.8
    6.6
    8.9
    7.7
    9.9
    8.8
    2.2
    9.9
    Josie
    9.5
    0
    8.8
    12.2
    9.9
    0
    8.5
    0
    9.2
    5.3
    Rogue
    8.5
    1.1
    7.8
    2.2
    7.9
    3.3
    7.5
    4.4
    8.2
    5.5
    Usagi Tsukino
    8.5
    15.5
    8.8
    30.1
    9.9
    19.7
    9.5
    11.0
    8.2
    8.6
    Yosemite Sam
    0
    15.2
    0
    29.5
    3.9
    0
    0
    16.0
    0
    7.7
    My code so far
    import java.io.*;
    public class test
        public static void main (String [] args)
            FileInputStream File1;
            DataInputStream In;
            String fileInput = "";
            try
               File1 = new FileInputStream ("data.txt");
                In = new DataInputStream (File1);
                while (In.available () > 0)
                    fileInput = In.readLine ();
                    System.out.println (fileInput);
                In.close ();
            catch (FileNotFoundException e)
                System.out.println ("Error - this file does not exist");
            catch (IOException e)
                System.out.println ("error=" + e.toString ());
    }My question, how do i save the data in to an array, and how do i seperatly save names in to different array, and their scores in to different arrays
    bte he said you can use scanner class
    Thanks
    Edited by: supahsain08 on Mar 26, 2008 2:55 PM

    supahsain08 wrote:
    Well, you are not in my class and you don't even know my teacher
    who are you to judge meHe is jaded by our experiences here. 99% of posters who complain that the teacher doesn't teach have adequate (note that I didn't say good) teachers, but it's the student who is failing to take responsibility for his own education. It is after all your responsibility and complaining won't help you any.
    Good luck.

  • Read a text file into array

    Can anyone let me know how to get read the values column wise from the text file and put each column in each arrays.
    for eg: if we have 4 columns in the text file then the values will be stored in 4 arrays.
    Thanks in advance
    Regards
    Kutty

    Use a two dimensional array to store your data, and use the StringTokenizer class to split up the lines as you read them in from the file.

  • Retrieving  two dimensional arrays from a text file.

    Good evening
    I am having a problem with reading a text file, and putting this info into a 2D array to be put into a table.
    I have the table already(but no code to add rows to the table incase the array increases)
    The array is called Product[a][ b ]
    where a is the product number(the row), and b are details about this product.
    The file i want to be read looks like this:
    data.txt
    CPU|AMD|X2, 64bit, 2GHz|$150|9
    Video card|NVidia|7800 GTX 256MB|$400|4
    e.g. product[2][4] would equal 4
    Basically: Type, manufacturer, specifications, price, amount in stock.
    the "splitter" or "field terminator" is "|"
    This is what i want it to do:
    Store fileline into single string,
    split string and put it in product[ j ][ i ]
    repeat with next lines until nothing is read anymore.
    This is the code I have so far:
    try{
            FileReader textFileReader = new FileReader("C://BACKUP2//data.txt");
            BufferedReader textReader = new BufferedReader(textFileReader);
            for(int j=0; j<0; j++){
                 for(int i=0; i<6; i++){ //i starts at 0, condition: i is under 6, 1 is added to i at each loop/update.             
                       ProductT = textReader.readLine();
                       textReader.close();
                       for(String Product[j] : ProductT.split("|")){ //this line is completly wrong, but i don't know how else to do it.
         // for counting for the number of columns
    catch (IOException e) {System.out.println(e);}
    I don't even think this code makes any bloody sense, I don't know if I'm approaching it correctly.
    Would anyone be gracious enough to give me a helping hand and tell me what/how to change my code?
    from what I see, the problem lies less in the loops, but more in the splitting of the string. no function seems to like 2D arrays :(

    So this is where I stand:
         //Reading
        try{
            FileReader textFileReader = new FileReader("C://BACKUP2//data.txt");
            BufferedReader textReader = new BufferedReader(textFileReader);
            for(int j=0; j>0; j++){ // this will go on forever now or what?
    // yes, this will loop forever, which is fine as long as you break out of the loop when you reach the end of the file (which is indicated by textReader.readLine() returning null)
                 // replace the for(String currentField : ProductT.split("|")) loop with this one
                 //for(int i=0; i<6; i++){ //i starts at 0, condition: i is under 6, 1 is added to i at update.       
                      String inputline = textReader.readLine();    // read a single line from the file each time through the loop
                      //while (inputline != null) {
                            if (inputline != null) {    // check if the end of the file has been reached
                           //ProductT = textReader.readLine(); //put to single string
                       //textReader.close(); //shut it off    // move this line to the end of processing, you don't want to close the file until you've read everything from it.
                       //for(String currentField : ProductT.split("|")){   //Split string...      according to JBuilder "The local variable currentField is never read"
                             // switch this loop to use the regular for(  ;  ;  ) type, that way you will have another counter to use as the second index in the product assignment
                                         //currentField = Product[j];
                        product[i][] = currentField; // use the index of the second loop as the second index here
    else {
    break; // break out of the loop when end of file is reached
         //} // the for i etc. end here
    } // End for j et cetera...
         } //end Try
    catch (IOException e) {System.out.println(e);}
    //cut the crap
    // close the reader here
    ok - so I am not quite sure about what you mean here
    "Assign each of the 5 fields to the five [][j]
    positions of the array. "
    should i put 5 "fors" in there? - currently the
    program is compiling - although they do not appear in
    the table.I put comments in the code that should answer those questions.
    The code for the table is
    Object[][] data = {
              {Product[0][0], Product[0][1],
                   Product[0][2], Product[0][3], Product[0][4]},
                   {Product[1][0], Product[1][1],
    Product[1][2], Product[1][3],
    uct[1][3], Product[1][4]},
                          {Product[2][0], Product[2][1],
    Product[2][2], Product[2][3],
    ct[2][3], Product[2][4]},
                              {Product[3][0], Product[3][1],
    Product[3][2],
    Product[3][2], Product[3][3], Product[3][4]},
                    {Product[4][0], Product[4][1],
    Product[4][2],
    Product[4][2], Product[4][3], Product[4][4]},
    ;If I do not declare any of these strings (e.g. I have
    only declared Product[0-2][] oldschol style) an error
    occurs, telling me that
    Caused by:
    java.lang.ArrayIndexOutOfBoundsException: 3
         at data.<init>(data.java:112)
         ... 5 more
    When you declare product, you'll need to give it a size for both dimensions. The first dimension will be the number of lines to be read from the file. You'll need to know this before allocating the array, so either count them in the file, (if that's good enough for this assignment) or loop through the file once before reading it to count the lines. The second dimension of the array will be 5, since that's how many fields each line has.
    btw; do I give those duke stars when all problems of
    my life(currently: reading the two dimensional
    string) have been resolved?I don't know about [i]all the problems of your life, but when the topic of the thread has been solved, then give them to whoever helped. :)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

Maybe you are looking for

  • Help!  Brand new MBP and Mid '04 23" Cinema Display cable issue!

    Just got a new MBP, and I can't figure out which adapter I need to use my trusty 23" display. The mini display port to dual-link DVI adapter doesn't look right, and I already know the mini display port to DVI adapter doesn't work. Will the dual-link

  • Message - F5170

    Dear Gurus, In FBL3n there are several open items for a GL Account. But when i try to clear that GL Account using T.Code F-03, it shows the message NO Open items. Just to update you that GL is a Balance Sheet Account and has open item management and

  • Hard Drive (western digital) no longer mounting..?

    Hi guys. I have yet to find any resolution to a new problem I've been having with my Western Digital Hard Drive. I've had it for about 6 months. Has run with no problems, until recently. I have been running it on my Macbook Pro 2.66 GHz Intel Core i7

  • Help - Passing complextype arguement to WebService

    Hi, I'm trying to pass a complex type into a web service request. <definitions xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:s0="CEM_Services_HTT

  • What size for Time Machine

    Hi, I am backing up my iMac to a WD Mybook with 500 Go space. The iMac hard drive has 235 Go on it right now. I have been backing up with TM since June and the Mybook is now full. Is this normal? I seem to remember before, TM did not take up so much