Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Input text file to BufferReader-Array to jcombobox

  1. #1
    Junior Member
    Join Date
    Apr 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Input text file to BufferReader-Array to jcombobox

    Hope you can help here. I want to read a text file which has 6 lines of text into an array. I then want to break out each of the 6 lines and assign each to a different jcombobox. I can do the first two parts –read the file and assign to an array but when I print it out I get multiple loop outputs and I can’t assign the individual lines to individual jcomboboxes. The program reads the first line and assigns nulls to the other 5 lines. It then reads the first and second lines and assigns nulls to the other lines, and so on. My problem is that I need to capture each line to a jcombobox independent of the other lines entries from the array[]. The array capture all the lines as one input. Is there a way to capture each line separately?

    public void inputFile() throws IOException{
      	//File reader method
     
           	FileReader file = new FileReader("c:\\jcboEntries.dat");
            	BufferedReader br = new BufferedReader(file);
     
            	String line;
            	String[] lines = new String [6];
     
           for (int i =0; i<6;i++){
     
            		line = br.readLine();
            		lines[i] = line;
     
                            jcbo1 = lines[0];
            		jcbo2 = lines[1];
            		jcbo3 = lines[2];
            		jcbo4 = lines[3];
           		        jcbo5 = lines[4];
            		jcbo6 = lines[5];
     
                    System.out.println(jcbo1);
           		System.out.println(jcbo2);
           		System.out.println(jcbo3);
           		System.out.println(jcbo4); 
          		 System.out.println(jcbo5);
           		System.out.println(jcbo6);
               }
            if (br = null) br.close();
    }
    ----------------------------
    Output:

    Selection Number One
    null
    null
    null
    null
    null
    Selection Number One
    Selection Number Two
    null
    null
    null
    null
    Selection Number One
    Selection Number Two
    Selection Number Three
    null
    null
    null
    Selection Number One
    Selection Number Two
    Selection Number Three
    Selection Number four
    null
    null
    Selection Number One
    Selection Number Two
    Selection Number Three
    Selection Number four
    Selection Number Five
    null
    Selection Number One
    Selection Number Two
    Selection Number Three
    Selection Number four
    Selection Number Five
    Selection Number Six
    Last edited by txguy5199; April 13th, 2019 at 04:47 PM. Reason: needed to state "code" & clarify question

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Input text file to BufferReader-Array to jcombobox

    Do you have any questions? Can you explain what problems you are having?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Input text file to BufferReader-Array to jcombobox

    Hi all: Solved the problem. I needed to be able to grab each line independent of the other so I can use just one Text file but break it out to different j combo boxes. below was what I did. I will have 20 jcombobox with each having around 7 select entries. The entries for the drop down boxes are around 50 lines of selections. Is there a shorter way of doing this?

     
    public void inputFile() throws IOException{
     
      //File reader method
     
            FileReader file = new FileReader("c:\\jcboEntries.dat");
            try (BufferedReader br = new BufferedReader(file)) {
     
     
             String[] lines = new String [6];
             String [] jcbo = new String [6];
             try {
     
                int i =0;
                lines[0] = br.readLine();
                jcbo[0] = lines[0];
                jcbo0 = jcbo[0];
                jcboNUMONE.addItem(jcbo0);
                System.out.println(jcbo0);
     
     
                lines[1] = br.readLine();
                jcbo[1] = lines[1];
                jcbo1 = jcbo[1];
                jcboNUMONE.addItem(jcbo1);
                System.out.println(jcbo1);
     
                lines[2] = br.readLine();
                jcbo[2] = lines[2];
                jcbo2 = jcbo[2];
                jcboNUMONE.addItem(jcbo2);
                System.out.println(jcbo2);
     
                lines[3] = br.readLine();
                jcbo[3] = lines[3];
                jcbo3 = jcbo[3];
                jcboNUMONE.addItem(jcbo3);
                System.out.println(jcbo3);
     
                lines[4] = br.readLine();
                jcbo[4] = lines[4];
                jcbo4 = jcbo[4];
     
                jcboNUMONE.addItem(jcbo4);
                System.out.println(jcbo4);
     
                 lines[5] = br.readLine();
                jcbo[5] = lines[5];
                jcbo5 = jcbo[5];
                jcboNUMONE.addItem(jcbo5);
                System.out.println(jcbo5);
     
     
             }catch (IOException e){}
     
         } catch (IOException e){}    
     
    }

  4. #4
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Input text file to BufferReader-Array to jcombobox

    Why do you keep transferring the input from one variable to another? And do you know about for loops?
    Would this not do the same thing?

    for (int i = 0; i < 6; i++) {
         String jcbo = br.readLine();
         jcboNUMONE.addItem(jcbo0);
         System.out.println(jcbo0);
    }

    Regards,
    Jim

Similar Threads

  1. Trying to input a text file but the program won't read it.
    By Spanky_10 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 27th, 2013, 11:50 AM
  2. open multiple text file to input to mysql
    By csharp100 in forum JDBC & Databases
    Replies: 2
    Last Post: November 26th, 2012, 09:58 PM
  3. Replies: 0
    Last Post: October 29th, 2012, 12:17 AM
  4. My text input file is not working correctly?
    By MLeclerc182 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 17th, 2012, 08:34 PM
  5. Allow user input to edit text file??
    By dannyyy in forum Java Theory & Questions
    Replies: 2
    Last Post: April 6th, 2011, 06:53 AM

Tags for this Thread