This discussion is archived
1 2 Previous Next 15 Replies Latest reply: Mar 20, 2008 9:29 PM by 807601 RSS

Help filling array from Fie input

807601 Newbie
Currently Being Moderated
Can anyone help me with a method to fill an array from an input file or direct me to a tutorial that I can read about it. I can do it from the console and i cant seem to find a way to do it from an input file.

Thank you
  • 1. Re: Help filling array from Fie input
    807601 Newbie
    Currently Being Moderated
    There are alot of ways to get data from a file. Show the method and then someone can help you better.
  • 2. Re: Help filling array from Fie input
    807601 Newbie
    Currently Being Moderated
    This is what I have now. It compiles error free and prints 26 zeros (The array is to hold letters). I have a code to assign the letter C to the 2 but it doesnt do that. Here is what I have so far.
       import java.io.*;
       import java.util.*;
        public class CodeBreaker
       {
          
           public static void main(String[]args)
                                    throws FileNotFoundException,IOException,
                                                   InputMismatchException
          {
             
            
             int[]letterCount=new int[25];
          
             FileReader inputStream=new FileReader("MessageIn.txt");
          
            
             
             int letterC;
             int index;
             int i;
             int ch=0;
             String s="ABCDEFGH";
             
             char C=s.charAt(2);
             index=(int)ch-65;
             if(index>=0&&index<25)
                letterCount[index]++;
                
             for(index=0;index<25; index++)
                System.out.print(letterCount[index]+"");
          }
       }
            
            
  • 3. Re: Help filling array from Fie input
    807601 Newbie
    Currently Being Moderated
    And you want an array filled with characters? So a character array?
  • 4. Re: Help filling array from Fie input
    807601 Newbie
    Currently Being Moderated
    Yes....I'm gonna guess that this is wrong then?
     int[]letterCount=new int[25];
  • 5. Re: Help filling array from Fie input
    807601 Newbie
    Currently Being Moderated
    Thats an array for an array of ints.
  • 6. Re: Help filling array from Fie input
    807601 Newbie
    Currently Being Moderated
    Yeh I figured that after your last post. I feel kinda dumb for not catching that. Ok I now have
     char[]letterCount=new char[25];
    My ouput is now
     00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    Where as with the array of ints It was
    00000000000000000000000000
  • 7. Re: Help filling array from Fie input
    807601 Newbie
    Currently Being Moderated
    You need to create an object of Buffered Reader as well. I can show you a method that works for Strings. Its pretty simple but it works.
     BufferedReader fI = new BufferedReader(new FileReader(name));
            System.out.println("File contents:   ");
            System.out.println();
            
            String firstLine = fI.readLine();
            while(firstLine!=null)
            {
            System.out.println(firstLine);        
            firstLine = fI.readLine();
            }
            fI.close();
  • 8. Re: Help filling array from Fie input
    807601 Newbie
    Currently Being Moderated
    From there just add a counter and use that as the index to the array. Keep in mind that this reads lines. It is a little different to read each individual character but you still need the BufferedReader

    Edited by: pberardi1 on Mar 20, 2008 9:23 PM
  • 9. Re: Help filling array from Fie input
    807601 Newbie
    Currently Being Moderated
    pberardi1 wrote:
    Thats an array for an array of ints.
    You've come a long way since last year, pberardi. It's nice to see the progression.

    -petes1234
  • 10. Re: Help filling array from Fie input
    807601 Newbie
    Currently Being Moderated
    Will this help fill the array also. I added it to my code and it works fine and it prints out the file. But the array is still empty am doing something wrong?
  • 11. Re: Help filling array from Fie input
    807601 Newbie
    Currently Being Moderated
    Sorry didn't see that last post
  • 12. Re: Help filling array from Fie input
    807601 Newbie
    Currently Being Moderated
    If I were you I would make sure first that it is actually reading the way you want it to so print it to console first. If that is working, then its easy to get it into the array. Post the code.
  • 13. Re: Help filling array from Fie input
    807601 Newbie
    Currently Being Moderated
    Well this is what I have but no change. Im pretty sure it isn't working cause i'm missing something else.
       import java.io.*;
       import java.util.*;
        public class CodeBreaker
       {
           public static void main(String[]args)
                                    throws FileNotFoundException,IOException,
                                                   InputMismatchException
          {   
             char[]letterCount=new char[25];
           
             BufferedReader fI = new BufferedReader(new FileReader("MessageIn.txt"));
             
                   
             System.out.println("File contents:   ");
             System.out.println();
            
             String firstLine = fI.readLine();
             while(firstLine!=null)
             {
                System.out.println(firstLine);        
                firstLine = fI.readLine();
             }
             fI.close();
             int count;
             for(count=0;count<25;count++)
             {
                System.out.print(letterCount[count]+"");
             }
          
          }
       }
     
  • 14. Re: Help filling array from Fie input
    807601 Newbie
    Currently Being Moderated
    Ok Pal here you go. I used some extra variables just for debugging purposes. The thing about this is that isr.read() returns an int. This must be cast to a char. Anyway, this is what you want. Let me know how it works out for you.
    import java.io.*;
    import java.util.*;
    
    public class CodeBreaker {
    
        public static void main(String[] args) throws IOException {
            char[] a = new char[25];
            InputStreamReader isr = new InputStreamReader(new FileInputStream("test.txt"));
            int c;
            int i=0;
            char b;
            char d;
            while ((c = isr.read()) != -1) {
                b = (char)c;
                System.out.println((char) c);
                a=b;
    i++;
    }
    isr.close();
    for(int j=0;j<a.length;j++)
    {
    d = a[j];
    System.out.println(a[j]);
    }
    }
    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
1 2 Previous Next