12 Replies - 1005 Views - Last Post: 12 June 2009 - 10:32 AM Rate Topic: -----

#1 usamor   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 03-March 09

arrays in java

Post icon  Posted 12 June 2009 - 08:25 AM

can someone help me please with this. i have been working on this a couple of days but still i didn't get the solution.
the code read .txt file (with values(double)).
here is how the .txt file looks:
99.43
101.17
100.66
98.24
101.03
101.69
100.30
97.10
96.39
94.03
93.66
Press any key to continue...
what i want exactly to do is to add one method like:
public static calculate()
{
}



to do the following:calculate the sum of the elements in the array and also calculate the sum of the first 12 elements.
something like this:
for(int i=0; i<=11; i++)
				  {
					  sum12 = sum12 + Close[i];
				  }
  

and
for(int i=0; i<=close.length; i++)
				  {
					  sum= sum+ close[i];
				  }
	

so that i can this method in main function.
thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: arrays in java

#2 mostyfriedman   User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: arrays in java

Posted 12 June 2009 - 08:52 AM

so what's the problem now???
Was This Post Helpful? 0
  • +
  • -

#3 usamor   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 03-March 09

Re: arrays in java

Posted 12 June 2009 - 09:11 AM

the problem is that i have an error : invalid method declaration, return type required. the function returns sum.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.StringTokenizer; 

import java.util.ArrayList;
import java.io.*;



public class ReadFile
{
	public static void main(String[] args) 
	{
		ArrayList<Double> close = loadFile("c://JCreatorV3LE//LoadCsvFile//stocks.txt");
		
	   calculate();
	   
	}

	public static ArrayList loadFile(String fileName)
	{
		if ((fileName.length() ==0))
			throw new IllegalArgumentException();
		
		//String line;
		ArrayList file = new ArrayList();

			   	
	 	try	
	 	{
	 		//Create our bufferedreader to read the file(file containing data)
			BufferedReader reader = new BufferedReader(new FileReader("c://JCreatorV3LE//LoadCsvFile//stocks.txt"));
			
			//Line to hold the line read from file
			String line = "";
		 	
		 			
	 		//StringTokenizer st = null;
	 		int lineNumber = 0;
	 		 		 		
	 		// Loop through the file reading in lines and storing in "line".
	 		// do this until readLine returns null (end of file)
			// Add each line to our arraylist object
	 		//read comma separated file line by line
	 		while( (line = reader.readLine()) != null)
			{
				
				file.add(line);
				
				lineNumber++; 			
				
				//break comma separated line using ","
				StringTokenizer st = new StringTokenizer(line, ","); 
				tokenAsDouble = Double.parseDouble(token);

				while(st.hasMoreTokens())
				{
					// Now use for loop to read each string in our arraylist.
					//display csv values
					
					System.out.println(""+ st.nextToken());			
				} 	
						
								
			
		  }	  
		  
				   
				}
		
			 catch(Exception e){System.out.println("Exception while reading the file: " + e);}
			 return file;
		   }
		   
		  
		 public static calculate()
		 {
		 	for(int i=0; i<=close.size; i++)
				  {
					  sum= sum+ close[i];
					  
				  }
			return sum;
		 }
}  
		 


Was This Post Helpful? 0
  • +
  • -

#4 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1528
  • View blog
  • Posts: 4,448
  • Joined: 12-May 09

Re: arrays in java

Posted 12 June 2009 - 09:14 AM

Here:
public static calculate()



You need a return type in the method declaration:
public static double calculate()


Was This Post Helpful? 0
  • +
  • -

#5 usamor   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 03-March 09

Re: arrays in java

Posted 12 June 2009 - 09:30 AM

now when i modified the code i have the following errors:
- cannot find symbol variable close next to (for int i=0; i<close.size; i++)
and next to the line sum = sum + close[i];
- operator + cannot be applied to double, any next to the line sum = sum + close[i];
- incompatible types next to the line sum = sum + close[i];
Was This Post Helpful? 0
  • +
  • -

#6 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1528
  • View blog
  • Posts: 4,448
  • Joined: 12-May 09

Re: arrays in java

Posted 12 June 2009 - 09:34 AM

View Postusamor, on 12 Jun, 2009 - 10:30 AM, said:

now when i modified the code i have the following errors:
- cannot find symbol variable close next to (for int i=0; i<close.size; i++)

Your arraylist close is not visible to your method calculate: you can either make it global and define at at the top of your class, or pass it into you calculate method as a parameter.

Quote

and next to the line sum = sum + close[i];

Same problem.

Quote

- operator + cannot be applied to double, any next to the line sum = sum + close[i];
- incompatible types next to the line sum = sum + close[i];

Where is sum declared? You should have a line like
double sum = 0;


Near the beginning of your calculate method.
Was This Post Helpful? 0
  • +
  • -

#7 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: arrays in java

Posted 12 June 2009 - 09:40 AM

Does the txt file look like this:
99.43
101.17
100.66
98.24

or like this?

99.43, 101.17, 100.66, 98.24
Was This Post Helpful? 0
  • +
  • -

#8 usamor   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 03-March 09

Re: arrays in java

Posted 12 June 2009 - 09:44 AM

it looks like
99.43
101.17
100.66
98.24
Was This Post Helpful? 0
  • +
  • -

#9 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: arrays in java

Posted 12 June 2009 - 09:51 AM

Well then you dont need to use a String tokenizer, you can use a regular scanner if it is all on one line. theere arent even commas in the format.
ArrayList<Double> file = new ArrayList<Double>();
Scanner in = new Scanner(new File("c://JCreatorV3LE//LoadCsvFile//stocks.txt"));
while(in.hasNextLine())
{
file.add(Double.parseDouble(in.nextLine()));
}
return file;


makes a Arraylist to hold the doubles reads each line and converts it into a double, and adds it to the arraylist. You have it set so your method returns a arraylist, so you simply return file.

Hope this helps!


btw- for(int i=0; i<=close.size; i++) needs to be i<close.size()or you will get IndexOutofBoundsError

This post has been edited by Fuzzyness: 12 June 2009 - 09:56 AM

Was This Post Helpful? 0
  • +
  • -

#10 usamor   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 03-March 09

Re: arrays in java

Posted 12 June 2009 - 09:59 AM

no i am sorry the .txt file looks like
99.43, 101.17, 100.66, 98.24
Was This Post Helpful? 0
  • +
  • -

#11 usamor   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 03-March 09

Re: arrays in java

Posted 12 June 2009 - 10:06 AM

if you can please show me exactly where i can have close and sum declared because i tried to have them as global and tried to declare them at the beginning of the function but i am having the same errors.

thanks
Was This Post Helpful? 0
  • +
  • -

#12 usamor   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 03-March 09

Re: arrays in java

Posted 12 June 2009 - 10:19 AM

even if i try to do without function just the for loop in main function i have errors:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.StringTokenizer; 

import java.util.ArrayList;
import java.io.*;



public class ReadFile


{
	public static double sum;
	
	public static void main(String[] args) 
	{
		ArrayList<Double> close = loadFile("c://JCreatorV3LE//LoadCsvFile//stocks.txt");
		
	  for(int i=0; i<=close.size(); i++)
				  {
					  sum= sum+ close[i];
					  
				  }
	   
	}
	
	public static ArrayList loadFile(String fileName)
	{
		if ((fileName.length() ==0))
			throw new IllegalArgumentException();
		
		//String line;
		ArrayList file = new ArrayList();

			   	
	 	try	
	 	{
	 		//Create our bufferedreader to read the file(file containing data)
			BufferedReader reader = new BufferedReader(new FileReader("c://JCreatorV3LE//LoadCsvFile//stocks.txt"));
			
			//Line to hold the line read from file
			String line = "";
		 	
		 			
	 		//StringTokenizer st = null;
	 		int lineNumber = 0;
	 		 		 		
	 		// Loop through the file reading in lines and storing in "line".
	 		// do this until readLine returns null (end of file)
			// Add each line to our arraylist object
	 		//read comma separated file line by line
	 		while( (line = reader.readLine()) != null)
			{
				
				file.add(line);
				
				lineNumber++; 			
				
				//break comma separated line using ","
				StringTokenizer st = new StringTokenizer(line, ","); 
			

				while(st.hasMoreTokens())
				{
										
					System.out.println(""+ st.nextToken());			
				} 	
						
								
			
		  }	  
		  
				   
				}
		
			 catch(Exception e){System.out.println("Exception while reading the file: " + e);}
			 return file;
		   }
		  
				 
}  
		 


Was This Post Helpful? 0
  • +
  • -

#13 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: arrays in java

Posted 12 June 2009 - 10:32 AM

You need a few adjustments here and there. Prefer String.split to StringTokenizer, but i've left it in below. I would also pass the filename as a parameter to the app:

import java.io.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;

import java.util.List;
import java.util.ArrayList;
import java.util.StringTokenizer;


public class ReadFile {
    public static double calculate(List<Double> close) {
	double sum = 0.0;
	for (int i = 0; i < close.size(); i++) {
	    sum = sum + close.get(i);
	}

	return sum;
    }

    public static void main(String[] args) {
	ArrayList<Double> close = loadFile(args[0]);
	double sum = calculate(close);
	System.out.printf("The sum of the values in file %s is %.2f to 2dp\n", args[0], sum);
    }

    public static ArrayList loadFile(String fileName) {
	if ((fileName.length() == 0)) {
	    throw new IllegalArgumentException();
	}

	//String line;
	ArrayList<Double> file = new ArrayList<Double>();

	try {
	    //Create our bufferedreader to read the file(file containing data)
	    BufferedReader reader = new BufferedReader(new FileReader(fileName));

	    //Line to hold the line read from file
	    String line = "";

	    //StringTokenizer st = null;
	    int lineNumber = 0;

	    // Loop through the file reading in lines and storing in "line".
	    // do this until readLine returns null (end of file)
	    // Add each line to our arraylist object
	    //read comma separated file line by line
	    while ((line = reader.readLine()) != null) {

		//break comma separated line using ","
		StringTokenizer st = new StringTokenizer(line, ",");

		while (st.hasMoreTokens()) {
		    // Now use for loop to read each string in our arraylist.
		    //display csv values
		    double tokenAsDouble = Double.parseDouble(st.nextToken().trim());
		    file.add(tokenAsDouble);
		}
	    }
	}
	catch (Exception e) {
	    System.out.println("Exception while reading the file: " + e);
	}

	return file;
    }
}



This post has been edited by g00se: 12 June 2009 - 10:33 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1