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 13 of 13

Thread: Reading data from a file into parallel arrays

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Reading data from a file into parallel arrays

    hi all,

    I am having a problem reading the data from a text file into my array. it skips every other character and string in the text file

    heres my code

    import java.io.*;
    import java.util.Scanner;

    public class Morse
    {
    public static void main(String [] args) throws IOException
    {
    File file = new File("Morse.txt");
    Scanner input = new Scanner(file);
    String sentence;



    char[] characters = new char[36];
    String[] symbols = new String[36];





    for(int i = 0; i < characters.length; i++)
    {
    for(int j = 0; j < symbols.length; j++)
    {
    char characs = input.nextLine().charAt(0);
    String syms = input.nextLine();

    characters[i] = characs;
    symbols[i] = syms;

    System.out.print(characters[i] + " " + symbols[i] + "\n");

    }
    }
    }
    }

    and its returning this

    1 2 ..---
    3 4 ....-
    5 6 -....
    7 8 ---..
    9 0 -----
    A B -...
    C D -..
    E F ..-.
    G H ....
    I J .---
    K L .-..
    M N -.
    O P .--.
    Q R .-.
    S T -
    U V ...-
    W X -..-
    Y Z --..


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Reading data from a file into parallel arrays

    A new line is read each time the nextLine() method is called. Is the code doing what you want?

    What should the output look like?
    What is wrong with what is printed?

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE 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
    Mar 2013
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Reading data from a file into parallel arrays

    It's a program for translating a sentence into morse code. The file morse.txt
    Has characters 1-9 . , ? and A-Z
    And the morse code translation right next to character
    I want to read the contents of the file and fill the array so it'll display like this

    1 ...
    2 ...
    3 ...
    4 ...
    ...
    ...
    ...
    ...
    A ...
    B ....
    C ...
    D ...
    And etc until all the lines are read

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Reading data from a file into parallel arrays

    I want to read the contents of the file and fill the array
    Did you miss this part of my last post?
    A new line is read each time the nextLine() method is called.

    Look at the code and see how it reads lines from the file and see what it does with each line it reads.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Reading data from a file into parallel arrays

    char characs = input.nextLine().charAt(0);  //This read a line
    String syms = input.nextLine();  //This reads another line

    Given your input format, I would read a line, then split it in two using say the space character as delimiter. Then feed your characters and symbols arrays with the splitted line. There is probably a better way but I am just a beginner.

  6. #6
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Reading data from a file into parallel arrays

    Yes but isn't that the method you use when using scanner methods for a string?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Reading data from a file into parallel arrays

    it skips every other
    Think about this problem and what the code is doing.
    What happens to the data following the "1" on the first line?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Reading data from a file into parallel arrays

    Quote Originally Posted by Norm View Post
    Think about this problem and what the code is doing.
    What happens to the data following the "1" on the first line?
    im not quite sure. I think its reading the line at 1 in the position of the array. it goes through the loop 36 times because there are 36 lines in the file
    it stores the data at position i and loops around until finished?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Reading data from a file into parallel arrays

    With the nested loops, it should try to loop 36*36 times.

    Are there any error messages when the program executes?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Apr 2013
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Reading data from a file into parallel arrays

    It is not the number of lines in your file being scanned that drives the number of loops. It's the conditions in the for loops. Well unless you have a break condition in the loops.
    If the application you use allows you to set breakpoints and step through the codes, you should do so. You will be surprise by the number of loops being executed.

  11. #11
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Reading data from a file into parallel arrays

    well i edited my code to look like this with one loop

     
    for(int i = 0; i < characters.length; i++) 
          { 
             char characs = input.nextLine().charAt(0);
             String syms = input.nextLine();
     
             characters[i] = characs;
             symbols[i] = syms;
     
             System.out.println(characters[i] + " " + symbols[i]);
     
          }


    --- Update ---

    i get the same display

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Reading data from a file into parallel arrays

    Are there any error messages when the program is executed?

    The posted code is still reading TWO lines every time it goes around the loop.
    It should get an error on the 19th time around if the file has 36 lines.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Apr 2013
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Reading data from a file into parallel arrays

    Of course, because for each loop turn, you still read two lines... Do you see the TWO calls to nextLine()
    Remove one of them as shown below and see what happens it might help you understand.

    for(int i = 0; i < characters.length; i++) 
          { 
             //char characs = input.nextLine().charAt(0);
             String syms = input.nextLine();
     
             //characters[i] = characs;
             symbols[i] = syms;
     
             System.out.println(symbols[i]);
     
          }
    Last edited by Norm; April 14th, 2013 at 09:00 PM. Reason: Uncommented symbols[i]=

Similar Threads

  1. Two Parallel Arrays
    By jsmnr77 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 31st, 2013, 09:41 AM
  2. Reading File and Outputting Data
    By ChrisMessersmith in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 13th, 2011, 01:40 PM
  3. Help with Scanner class - Reading Data from a file
    By billias in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 28th, 2011, 12:07 PM
  4. Help reading data from file, manipulating then rewriting out.
    By Nismoz3255 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 19th, 2011, 09:13 AM
  5. Reading data from a text file into an object
    By surfbumb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2011, 08:37 PM