0

Ok. So I am working on a program that gets information that has been put into textfields in a gui and puts that information into a text file and the name of the file is the name of the animal and name of the owners last name.

//that part is done.

Then able to search for the file using the name of the animal and the owners last name, and being able to have the information be put into separate text fields. That look like the page where you first put down the information. Then being able to save the information changed to the same text file.

Now my question is how do I get the information from a text file that has different lines and then put each line in its own String.

Here is the part of the program that reads the text file

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

public class ReadFile {

  private String path;


  public ReadFile(String file_path) {
    path = file_path;
  }

  public String[] OpenFile() throws IOException {

    FileReader fr = new FileReader (path);
    BufferedReader textReader = new BufferedReader(fr);

    int numberOfLines = 20; 
    String[] textData = new String[numberOfLines];

    int i;

    for (i=0; i < numberOfLines; i++) {
      textData[i] = textReader.readLine();

  }
    textReader.close();
    return textData;
} 
   int readLines() throws IOException {
    FileReader file_to_read = new FileReader(path);
    BufferedReader bf = new BufferedReader(file_to_read);

    String aLine;
    int numberOfLines = 0;

    while (( aLine = bf.readLine()) != null) {
      numberOfLines++;
    }
           bf.close();

           return numberOfLines;
           }
}
`
// Here is where I am using this code

    `JButton b7 = new JButton("Done");
         b7.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
             f4.setVisible(true);
             f3.setVisible(false);
             scrollPane.revalidate();
             scrollPane.repaint();
                  String namess = names.getText();
                 na.setText(namess);
                 String ownerslss = ownersls.getText();
                 ol.setText(ownerslss); 
                   String file_name =namess + " " + ownerslss + ".txt";
                                    na.setText(namess);

                 ol.setText(ownerslss); 
                 try { 
                   ReadFile file = new ReadFile (file_name);
                   String [] aryLines = file.OpenFile();
                    String aryLiness ="";
                   int item, space;
                   for (item=0; item < aryLines.length; item++) {
                     System.out.println(aryLines[item]);
                     aryLiness = Arrays.toString(aryLines);

                      space = aryLines.length;
                   }
                    space = aryLines.length;
    //< assname is a textarray that is the only way I could get the words to go down the page but it doesn't look good at all. Because it doesn't skip lines...
                   assname.setSize(20 ,space);
                   assname.append("" + aryLiness);
                     panimals.add(assname);  
                 }

                 catch (IOException wewe) {
                   System.out.println(wewe.getMessage() );

      }

     }
         });`
  • Please add working code, expected behavior, provided input and errors if any. For more information on how to ask a good question please refer http://stackoverflow.com/help/how-to-ask – Dipen Shah Jul 06 '15 at 19:51
  • 2
    This is just a list of requirements. What's the question? Keep in mind if you're just going to ask "how do I do it" then that's way too broad. – tnw Jul 06 '15 at 19:53
  • Would you like the full code so you can see what is happening? It won't let me put a picture in so I cant fully show you what the program is doing incorrectly. Also I am trying to make a Vet Program which is able to save information about the pet to a text file. - that i already accomplished. - Next the program should be able to access the text file read each line that was saved and put each line of the text file as a different String <- That is the hard part - . Which then those Strings will then be put into separate Text fields so that they can be edited. – Jordan Beavers Jul 06 '15 at 20:11
  • wewe! Your 2nd and 3rd paragraphs repeat their text. You should ask the question you are trying to have answered, rather than stating what it should do. – DoubleDouble Jul 06 '15 at 20:25
  • My question is how do i read text from a text file that has separate lines and then put each line of text into a different String... that is my question – Jordan Beavers Jul 06 '15 at 20:28
  • [Check out these answers](http://stackoverflow.com/questions/5868369/how-to-read-a-large-text-file-line-by-line-using-java) – DoubleDouble Jul 06 '15 at 20:30
  • Double Double is there an answer there that is able to separate each line into its own separate string? I do not like using arrays because I do not know how to separate each line of an array into a different string for each line. Would you like to skype so I can actually talk to you and show you where I am having issues? – Jordan Beavers Jul 06 '15 at 20:38
  • [This also shows a good example](http://stackoverflow.com/q/326390/2974766), though you'd want to use a new string each time instead of adding each line together with a StringBuilder. I am unable to Skype at the moment – DoubleDouble Jul 06 '15 at 20:41
  • What would you prefer to use instead of an array? An ArrayList of Strings? – DoubleDouble Jul 06 '15 at 20:46
  • Ok, it says you have to have 20 rep to open a chat. If you would like, we can use google+ hangout if you think that could work. – Jordan Beavers Jul 06 '15 at 20:47
  • I would like to use whatever would be able to be used in separate JTextFields. If I could just send you a picture of what I want it to look like... – Jordan Beavers Jul 06 '15 at 20:48
  • You can add an image in your question by using the picture icon within the top bar when you are editing. It goes.. `Bold, Italic, Hyperlink, BlockQuote, Code Sample, Image` (but with icons) – DoubleDouble Jul 06 '15 at 20:51
  • It says i need 10 rep to do that – Jordan Beavers Jul 06 '15 at 20:53
  • When will you be able to skype? – Jordan Beavers Jul 06 '15 at 20:57
  • My Skype name is beaverofpeeps just add me when you can. – Jordan Beavers Jul 06 '15 at 21:02
  • I don't mean to offend, but I just try to help in small doses of downtime - To call on skype would mean I would be home with my family, and probably doing something other than programming. – DoubleDouble Jul 06 '15 at 21:58
  • ok that worked but now i need to center the textfields. I know how to do c.gridx = 0 and c.gridy= 40 but I need gridy to increase by 5 each time the for statement completes itself, I have tried < int space = 40 > outside the for statement then c.gridy = (space); then space = space + 5; inside the for statement. But that didn't work. – – Jordan Beavers Jul 07 '15 at 03:37

1 Answers1

1

It's important to know how arrays work, because they are used very often.

String[] myStringArray = new String[3];

This array holds 3 String objects. It is essentially the same as if you had done:

String oneString = null;  //same as myStringArray[0]
String twoString = null;  //same as myStringArray[1]
String threeString = null;//same as myStringArray[2]

This means, after you read all your lines into Strings, you can create your JTextField like so:

JTextField myTextField1 = new JTextField(myStringArray[0]);
JTextField myTextField2 = new JTextField(myStringArray[1]);
JTextField myTextField3 = new JTextField(myStringArray[2]);

or, assigning an array one line at a time:

JTextField[] myTextFields = new JTextField[3];
myTextFields[0] = new JTextField(myStringArray[0]);
myTextFields[1] = new JTextField(myStringArray[1]);
myTextFields[2] = new JTextField(myStringArray[2]);

Now, imagine if you had 300 Strings and TextFields, there's no way you would want to type those lines 300 times. That's why for loops are awesome.

JTextField[] myTextFields = new JTextField[300];
for (int i = 0; i < myTextFields.length; i++)
{
    myTextFields[i] = new JTextField(myStringArray[i]);
}

Not sure if you realize, but you currently read every line in your textfile into an array already, named aryLines. You then print each String individually in your for loop:

System.out.println(aryLines[item]); //item is starting at 0, goes through every String

aryLines[0] is your first string. aryLines[1] is your second.. and so on until the last line. Theoretically, you could create a BufferedWriter like shown here and completely copy your current textfile into a new one just doing basically what you are already doing. You would just use this instead of the System.out.println() after creating your BufferedWriter:

writer.write(aryLines[item]);
writer.newLine();

After you print out every line to the console, I'm not sure what you mean to do. aryLiness with two ss is created by combining all the Strings in your array into one String. Then you set your TextArray with the entire combined String.

Community
  • 1
  • 1
DoubleDouble
  • 1,493
  • 10
  • 25
  • ok that worked but now i need to center the textfields. I know how to do c.gridx = 0 and c.gridy= 40 but I need gridy to increase by 5 each time the for statement completes itself, I have tried < int space = 40 > outside the for statement then c.gridy = (space); then space = space + 5; inside the for statement. But that didn't work. – Jordan Beavers Jul 06 '15 at 22:56
  • @JordanBeavers I'm not really familiar with java GUI work, and I'm not sure whether I'd recognize the issue without seeing how you've got the code - you may have to ask another question for that issue. – DoubleDouble Jul 07 '15 at 15:14