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

Thread: Please help with my code

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

    Default Please help with my code

    This code is to have a method that takes ints from a txt file and puts them in an array. I created a method that takes the name of the file nad the size of the array need to be created. The data is extracted from the file seems to be working fine when I print it out. But the array when printed only has one number instead of the many different numbers.

    The method code

    public static int[] fillarray(int n , String filename)
    {
     
     int [] arrs;
     arrs = new int[n];
    try
    { FileReader fr = new FileReader(filename);
      BufferedReader inputStream = new BufferedReader(fr);
    String line = null ;
    while((line = inputStream.readLine()) != null)
    { System.out.println(line);
    int temp = Integer.parseInt(line);
     
     for( int i = 0; i < n; i++)
     {
      arrs[i] = temp;
    }
    }
    inputStream.close();
    }
    catch(IOException e)
    {
    System.out.print("Error in Reading the file");
    }
    return arrs;
    }

    Thank You for any help you can provide
    Last edited by dacoach1549; April 28th, 2012 at 01:11 PM.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Please help with my code

    the array when printed only has one number
    That's exactly what the for loop is doing: it's putting temp (which was obtained from a line in the file) into every array slot.

    for( int i = 0; i < n; i++)
    {
        arrs[i] = temp;
    }

    A for loop isn't appropriate. Rather you should assign the value of temp to some particular array position, which means worrying about which position.
    Last edited by pbrockway2; April 28th, 2012 at 05:06 PM. Reason: sorry for corrrections: first coffee of am

  3. #3
    Junior Member hackthisred's Avatar
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Please help with my code

    I recommend using a List instead of an array for this operation, assuming you don't already know the number of entries that will be recorded from the file as input.
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
     
     
    public class Stuff {
     
    	public static List fillarray(String filename)
    	{
     
    		List<Integer>  arrs = new ArrayList<Integer>();
    	try
    	{ 
    		FileReader fr = new FileReader(filename);
    		BufferedReader inputStream = new BufferedReader(fr);
    		String line = null ;
    		while((line = inputStream.readLine()) != null)
    		{ 
    			System.out.println(line);
    			arrs.add(Integer.parseInt(line)) ;
    		}
     
    		inputStream.close();
    	}
    	catch(IOException e)
    	{
     
    		System.out.print("Error in Reading the file");
    	}
    		return arrs;
    	}
     
    }
    p.s. formatting is important, make it a habit now while your learning the language
    Last edited by hackthisred; April 29th, 2012 at 10:29 PM. Reason: remove uneeded parameters as part of the method
    f34r th3 kut3 1z

Similar Threads

  1. code to refresh and check the code of a webpage..
    By vaggelis in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2012, 07:43 AM
  2. Help merging program code with GUI code
    By Wilha in forum AWT / Java Swing
    Replies: 2
    Last Post: January 25th, 2012, 07:03 PM
  3. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM
  4. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 09:30 PM
  5. describe this program code by code ....
    By izzahmed in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2011, 11:03 PM