Results 1 to 4 of 4
  1. #1
    j@v@ is offline Member
    Join Date
    Oct 2010
    Location
    Baltimore, MD
    Posts
    5
    Rep Power
    0

    Default how to send scanned in arrary to class?

    I have no idea how to send a scanned in file (array) to a class.

    The file has 4 doubles..
    10.1 20.2 25.6 30.7


    try
    {
    Scanner inFile;
    inFile = new Scanner(new File(fileName));

    while (inFile.hasNextDouble())
    {

    grades[i] = inFile.nextDouble();
    i++;
    }
    inFile.close();


    {
    Stat elements = new Stat(grades, i); // after the file is scanned in, this is how I am trying to send the arrary over to the "Stat" Class..

    public Stat(double[] scores, int i)
    {
    array = scores;
    count = i;
    }

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    841
    Rep Power
    6

    Default

    1.Declare the array.
    2.Then pass the double value file as a parameter to Scanner.
    3.Then assign scanned double value to array using nextDouble().
    while(scanObject.hasNextDouble())
    {
    assign to array by incrementing the counter.
    }
    4.Finally pass the array to a method what you asked for.
    Ramya:cool:

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by j@v@ View Post
    I have no idea how to send a scanned in file (array) to a class.

    in your code you don't need to send anything to the method Stat, since the count of elements is in the field length of your array grades and it's not clear where the variable array comes from and what you are trying to do. i just modified the code so that i compile it without errors and i added a method to print out the readed doubles. by the way, if the number of elements in your file can change then i would suggest to use an ArrayList.

    Java Code:
    import java.io.File;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class ScannerExample {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		double[] grades = new double[4];
    
    		try {
    			Scanner inFile;
    			inFile = new Scanner(new File("drive:\\path\\doubles.txt"));
    
    			int i = 0;
    			while (inFile.hasNextDouble()) {
    
    				grades[i] = inFile.nextDouble();
    				i++;
    			}
    			inFile.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    
    		Stat(grades);
    	}
    
    	public static void Stat(double[] scores) {
    		for (int i = 0; i < scores.length; i++) {
    			System.out.println("score " + (i + 1) + ": " + scores[i]);
    		}
    	}
    }

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    841
    Rep Power
    6

    Default

    It's better not to give code.Let them try and come out with code.
    Ramya:cool:

Similar Threads

  1. scanned image save by swing dialog box
    By gaurav17may in forum AWT / Swing
    Replies: 8
    Last Post: 07-20-2010, 12:48 PM
  2. Replies: 10
    Last Post: 06-06-2010, 06:49 PM
  3. how to send sms
    By islam in forum Eclipse
    Replies: 5
    Last Post: 03-16-2010, 11:57 AM
  4. Send selective objects in a class over network
    By RDReavis in forum Advanced Java
    Replies: 2
    Last Post: 01-18-2010, 08:45 AM
  5. How 2d Pixel Arrary Is Stored In .txt/.dat File
    By Mazharul in forum New To Java
    Replies: 1
    Last Post: 08-18-2008, 12:23 PM

Posting Permissions

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