Results 1 to 9 of 9
  1. #1
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    89
    Rep Power
    0

    Default Reading data from a file and putting that data in arrays

    I am creating a program where it reads the data inside a file and then places this data into arrays. The file I created has numbers 1-30 in it, file named, testing1.txt .

    Java Code:
    public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		
    
    		// Variables Declaration Section
    		//******************************			 
    			BufferedReader br;
    			String sCurrentLine;
    			String str;
    	 
    			
    		
    
    	    
    
    	    
    	    // Variables Initialization Section
    	    //*********************************
    			br = null;
    			
    			
    
    	    
    	    // Code Section
    	    //*************
    			
    			try {
    				
    				br = new BufferedReader(new FileReader("C:\\testing1.txt"));
    				
    				List<String> list = new ArrayList<String>();
    				
    				while ((sCurrentLine = br.readLine()) != null)
    				{
    					list.add(str);
                                 ^
    				}
    								
    				String[] stringArr = list.toArray(new String[0]);
    			}
    							
    			 catch (IOException e) 
    			 {
    				e.printStackTrace();
    			} 
    			 finally 
    			 {
    				try {
    					if (br != null)br.close();
    				} 
    				catch (IOException ex)
    				{
    					ex.printStackTrace();
    				}
    				
    			 }
    			 
    			
    			
    
    	    
    	    // Output Section
    	    //***************
    
    		
    		// Cleaning Section
    		//*******************
    			
    			 
    			 
    			}
    	}
    My issue is that the 'str' value is not initialized, but if I initialize it, it ruins the code. I'm not sure what to do in the situation.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Reading data from a file and putting that data in arrays

    Quote Originally Posted by sithclone3 View Post
    My issue is that the 'str' value is not initialized, but if I initialize it, it ruins the code. I'm not sure what to do in the situation.
    So... you claim the code is ruined because after you get rid of the compilation errors, it doesn't work properly. That is considered normal unless you are very lucky, have a simple program, or are new at programming. Perhaps you should post the code with the initialization. Otherwise, it will be near impossible to assist.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    89
    Rep Power
    0

    Default Re: Reading data from a file and putting that data in arrays

    Java Code:
    // Variables Declaration Section
    		//******************************			 
    			BufferedReader br;
    			String sCurrentLine;
    			String str;
    			
    	 
    			
    		
    
    	    
    
    	    
    	    // Variables Initialization Section
    	    //*********************************
    			str = null;
    			br = null;
    			
    			
    			
    
    	    
    	    // Code Section
    	    //*************
    			
    			try {
    				
    				br = new BufferedReader(new FileReader("C:\\testing1.txt"));
    				
    				List<String> list = new ArrayList<String>();
    				
    				while ((sCurrentLine = br.readLine()) != null)
    				{
    					list.add(str);
    				}
    								
    				String[] stringArr = list.toArray(new String[0]);
    			}
    							
    			 catch (IOException e) 
    			 {
    				e.printStackTrace();
    			} 
    			 finally 
    			 {
    				try {
    					if (br != null)br.close();
    				} 
    				catch (IOException ex)
    				{
    					ex.printStackTrace();
    				}
    			
    			 }
                   // Output Section
    	    //***************
    			
    			
    
    		
    		// Cleaning Section
    		//*******************
    	        
    			
    			 
    			 
    			}
    	}

    P.S. I am getting redirected to 'Page Not Found' almost everytime I press 'Post Quick Reply'

    So When I initialize 'str', 'stringArr' becomes "not used" but when I don't initialize 'str', 'stringArr' is used. I'm just confused, and yes I am still a beginner in Java and this is the first time I am reading data from a file and putting it into arrays.
    Last edited by sithclone3; 02-08-2015 at 05:42 AM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Reading data from a file and putting that data in arrays

    The following is taken from above:

    Java Code:
    String str = null;
    ...
    ...
    while ((sCurrentLine = br.readLine()) != null)
    {
         list.add(str);
    }
    Do you notice a problem?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    89
    Rep Power
    0

    Default Re: Reading data from a file and putting that data in arrays

    // Variables Declaration Section
    //******************************
    BufferedReader br;
    String sCurrentLine;
    String str;








    // Variables Initialization Section
    //*********************************
    str = null;
    br = null;





    // Code Section
    //*************

    try {

    br = new BufferedReader(new FileReader("C:\\testing1.txt"));

    List<String> list = new ArrayList<String>();

    while ((sCurrentLine = br.readLine()) != null)
    {
    list.add(str);
    }

    String[] stringArr = list.toArray(new String[0]);
    }

    catch (IOException e)
    {
    e.printStackTrace();
    }
    finally
    {
    try {
    if (br != null)br.close();
    }
    catch (IOException ex)
    {
    ex.printStackTrace();
    }

    }


    P.S. I am getting redirected to 'Page Not Found' almost everytime I press 'Post Quick Reply'

  6. #6
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    89
    Rep Power
    0

    Default Re: Reading data from a file and putting that data in arrays

    // Code Section
    //*************

    try {

    br = new BufferedReader(new FileReader("C:\\testing1.txt"));

    List<String> list = new ArrayList<String>();

    while ((sCurrentLine = br.readLine()) != null)
    {
    list.add(str);
    }

    String[] stringArr = list.toArray(new String[0]);
    }

    catch (IOException e)
    {
    e.printStackTrace();
    }
    finally
    {
    try {
    if (br != null)br.close();
    }
    catch (IOException ex)
    {
    ex.printStackTrace();
    }

    }





    // Output Section
    //***************
    System.out.print(stringArr);

    // Cleaning Section
    //*******************




    }
    }


    I have written this and it seems to use 'str', but 'stringArr' becomes an error. The error message is 'stringArr cannot be resolved to a variable'



    P.S. I am getting redirected to 'Page Not Found' almost everytime I press 'Post Quick Reply'

  7. #7
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    89
    Rep Power
    0

    Default Re: Reading data from a file and putting that data in arrays

    This is the top part of the code

    public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub


    // Variables Declaration Section
    //******************************
    BufferedReader br;
    String sCurrentLine;
    String str;









    // Variables Initialization Section
    //*********************************
    str = null;
    br = null;

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Reading data from a file and putting that data in arrays

    The array stringArr is local to the block. It will go out of scope and be unusable when you leave try/catch block. Whenever you want to assign a local variable which is between two braces {}, declare the variable outside the braces. Then it will be visible (at least within any other braces or methods in which it is declared). Here is an example:

    Java Code:
          public static void main(String[] args) {
          int z = 10;
          {
             int y = 20;
             {
                int x = 30;
                System.out.println(x); // x visible
                y = 50;
             }
             System.out.println(x);// x not visible, compiler error
             System.out.println(y); // y visible, now 50
             System.out.println(z); // z visible
             z = 100;
          }
          System.out.print(x); // x not visible, compiler error
          System.out.print(y); // y not visible, compiler error
          System.out.println(z); // z visible, now 100
         }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    89
    Rep Power
    0

    Default Re: Reading data from a file and putting that data in arrays

    Quote Originally Posted by jim829 View Post
    The following is taken from above:

    Java Code:
    String str = null;
    ...
    ...
    while ((sCurrentLine = br.readLine()) != null)
    {
         list.add(str);
    }
    Do you notice a problem?

    Regards,
    Jim
    I do, I am calling a 'null' variable. So the variable is already not working. But, if I dont have 'str = null' it says that 'str' is not initialized. But, when I do, it says its initialized but 'stringArray' is not being used....

    Sorry for all the random replies, I keep getting redirected to 'Page Not Found' randomly and sometimes I'm not, didn't know they actually got posted.

Similar Threads

  1. Replies: 1
    Last Post: 07-20-2014, 07:23 PM
  2. Reading data from file
    By Roberto1989 in forum New To Java
    Replies: 1
    Last Post: 04-28-2011, 05:52 PM
  3. Replies: 3
    Last Post: 06-02-2010, 06:58 PM
  4. Reading data to file
    By puk284 in forum Advanced Java
    Replies: 1
    Last Post: 04-28-2009, 03:19 PM
  5. Reading Data from a file
    By ramachandran in forum New To Java
    Replies: 2
    Last Post: 10-24-2007, 07:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •