调用具有多个参数和一个字符串数组的方法

时间:2017-11-23 05:36:37

标签: java arrays

我必须创建一个程序,要求我使用名为DayPrices的类不断询问要搜索的日期。我已经完成并弄清楚了大部分程序,但是当我调用函数

时,我认为它会工作
get.Data(String[], double,double,double,double,double,double) I 

我在netbeans中收到此错误,声明" .class"预期

import java.util.Scanner;
import java.lang.*;
import java.util.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.ParseException;
import java.util.Date;

/**
 *
 * @author Emad Khalique
 */
public class DayPrices {
private static Scanner input = new Scanner(System.in);
private static Scanner x;
private static Formatter y;

        public void openFile()
        {
            try
            {
                x = new Scanner(new File("C:/Users/Emad Khalique/Desktop/google.txt"));
            }
            catch (Exception e)
            {
                System.out.println("could not find file");
            }
        }

        public void readFile()
        {                
            int counter = 1;

            String[] date1 = new String[24];
            String[] open1 = new String[24];
            String[] high1 = new String[24];
            String[] low1 = new String[24];
            String[] close1 = new String[24];
            String[] adjclose1 = new String[24];
            String[] volume1 = new String[24];


            //store data into the array in a while loop that increments the counter and the array
            while(x.hasNext())
                {
                    date1[counter] = x.next();
                    open1[counter] = x.next();
                    high1[counter] = x.next();
                    low1[counter] = x.next();
                    close1[counter] = x.next();
                    adjclose1[counter] = x.next();
                    volume1[counter] = x.next();

                    // create a function that only prints form the specefied array. to search create a system where it checks for the specefic date
                    counter++;
                }



            //convert the array of strings to their corresponding demand of type
            double Open = Double.parseDouble(open1[counter]);        
            double High = Double.parseDouble(high1[counter]);
            double Low = Double.parseDouble(low1[counter]);
            double Close = Double.parseDouble(close1[counter]);
            double AdjClose = Double.parseDouble(adjclose1[counter]);
            double Volume = Double.parseDouble(volume1[counter]);               
        }

        //while loop of function for a function that continuously asks for the next date and displays the corresponding counter#
        //a system to configure the methods of search
        public void getData(String date1[], double Open, double High, double Low, double Close, double AdjClose, double Volume)
        {
            int counter = 0;
            Scanner input = new Scanner(System.in);
            String datefinder;

            System.out.println("Please enter a date to search");
            datefinder = input.nextLine();

            while (datefinder != date1[counter])
            {
                while(datefinder != date1[counter])
                {
                    counter++;
                }
                System.out.println(date1[counter]);
                counter++;
            }


        }

        public void closeFile()
        {
            x.close();
        }

}

import com.sun.org.apache.xpath.internal.operations.String;
import java.util.Scanner;
import java.lang.*;
import java.util.*;
import java.io.*;

/ **  * * @author Emad Khalique  * / 公共课CST1201 {

public static void main(String[] args){


    DayPrices pricesCall = new DayPrices ();

    int runAgain = 1;
        while(runAgain == 1)  //create a method to increment runAgain variable
        {    
            Scanner input = new Scanner(System.in);
            DayPrices prices = new DayPrices();
            prices.openFile();
            prices.readFile();

            while(runAgain == 1){
            //saidfunction that will get the data    
                prices.getData(String[], double, double, double, double, double, double);

            }

            prices.closeFile();

            System.out.println("Would you like to continue? ");
            runAgain = input.nextInt();

        }    
}

}

2 个答案:

答案 0 :(得分:1)

您不能使用类型参数调用方法,您需要发送实际值。

prices.getData(new String[3], 3.4, 17, 5.5, ...);

如果您想要readFile()的值,一个选项是将变量移动到类范围并为main

中的访问创建获取者
public class DayPrices {
    private double open;        
    private double high;
    private double low;
    // more variables

    public getOpen() {
        return open;
    }

    // more getters

    public void readFile() {
        open = Double.parseDouble(open1[counter]);        
        high = Double.parseDouble(high1[counter]);
        low = Double.parseDouble(low1[counter]);
        //...
    }
}

并在main

prices.getData(prices.getData1(), prices.getOpen(), ...);

作为旁注,Java中的变量应该以小写开头。

答案 1 :(得分:0)

我认为你不应该发送'String []',因为编译器认为你传递的是class-type,所以你可以简单地在prices.getData()函数中传递'string []'变量。