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

标签 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 */ 公共(public)类 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();

        }    
}

}

最佳答案

您无法调用带有类型参数的方法,您需要发送实际值。

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

如果您想要来自 readFile() 的值,一种选择是将变量移动到类作用域并创建 getter 以便它们在 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 中的变量应以小写字母开头。

关于java - 调用具有多个参数和一个字符串数组的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47448277/

相关文章:

Java - java.util.InputMismatchException

Java更改按钮名称并递增计数

java - 获取 double 组的最大 k 个元素

javascript - names.forEach 不是函数

c# - 如何在整数数组中找到最长的相等元素序列?

java - 如何将spring存储库条目的id设置为精确值?

java成员变量在父类和子类之间的可见性

python - 将不同长度的 numpy 数组保存到同一个 csv 文件的最佳方法是什么?

php - 如何将 pdo 返回的关联数组的内容获取到数组中

java - 如何使用 Spring Data Solr 实现多核和存储库的自定义 Solr 存储库