gpt4 book ai didi

java - 如何将双数组列表与字符串数组列表合并

转载 作者:行者123 更新时间:2023-11-29 04:05:24 28 4
gpt4 key购买 nike

上下文

我有一项作业要求我从文本文件中读取一份食谱,并处理其中的内容,例如测量单位和配料数量。我将食谱的各个部分分成一个数组列表来操作成分,因为用户将输入他们是想使用英制单位还是公制单位。从这里我创建了一个双数组列表,用于从原始列表中分离数量,所以 4 汤匙盐就变成了 4.0。在此之后,用户可以输入是否要更改食谱的份量,因此 4.0 可以变成任何正数,如 8.0。我已经对所有这些进行了编码,直到我需要将操纵的数组列表合并回一起以输出给用户为止。所以 4 种份量的原始字符串数组列表:

[2 tbsp olive oil, 3 tbsp garlic, minced, 2 tsp dried parsley, 1 cup eggplant, cut into half inch cubes, 0.5 tbsp salt, 1 cup Parmesan cheese]

必须与新的 8 份份量的相应双数组列表相结合:

[4.0, 6.0, 4.0, 2.0, 1.0, 2.0]

得到:

[4 tbsp olive oil, 6 tbsp garlic, minced, 4 tsp dried parsley, 2 cup eggplant, cut into half inch cubes, 1 tbsp salt, 2 cup Parmesan cheese]

如有任何帮助,我们将不胜感激。

代码

import java.util.*;
import java.io.*;
class Main {

/**
* Prints methods to user
*/
public static void main(String[] args) throws Exception{
printer1();
servingReader();
System.out.println(sSize);
ingrReader();
System.out.println(ingrList);
ingrSeperator();
System.out.println(SeperatorImperialList);
choice();
}//end main

/**
* Prints origanal recipe to user
*/
public static void printer1() throws Exception{
Scanner sc = new Scanner(new File("/home/runner/recipe.txt"));
//original text file printed to the user before manipulation
while (sc.hasNextLine()){
System.out.println(sc.nextLine());
}
sc.close();//scanner closed
}

static int sSize;//int for serving size

/**
* Reads text file
*/
public static void servingReader() throws Exception{

//recipe read
File recipe = new File("/home/runner/recipe.txt");
Scanner sc = new Scanner(recipe);

//loop to find serving size
while(sc.hasNextLine()){
String s = sc.nextLine().toUpperCase();
if(s.equals("SERVING SIZE")){
sSize = sc.nextInt();
}
}

sc.close();//scanner closed
}//servingReader

static ArrayList<String> ingrList = new ArrayList<>();//ArrayList for ingredients created

static String scan;

/**
* Reads recipe and adds ingredients section to list
*/
public static void ingrReader() throws Exception{

//recipe read
File recipe = new File("/home/runner/recipe.txt");
Scanner sc = new Scanner(recipe);

//loop to find ingredients section or recipe
while(sc.hasNextLine()){
String s = sc.nextLine().toUpperCase();
//if scanner finds header INGREDIENTS, section added to ingrList
if(s.equals("INGREDIENTS")){
while(sc.hasNextLine()){
int i = 0;
scan = sc.nextLine();
ingrList.add(i, scan);
i+= 1;
//if nothing is found, breaks
if(scan.equals("")){
break;
}
}
}
}
sc.close();//scanner closed

Collections.reverse(ingrList);//reverse method reverses order of elements in ingrList

int ingr1 = ingrList.size() - 1;
ingrList.remove(ingr1);


}//ingrReader

static ArrayList<Double> SeperatorImperialList = new ArrayList<Double>();//ArrayList for seperated ingredients created

/**
* Seperates ingredient quantitys from ingredints list and adds to seperated list
*/
public static void ingrSeperator(){

double ingr2 = 0;

for(int i = 0; i <ingrList.size(); i++){
String split [] = ingrList.get(i).split(" ");
ingr2 = Double.parseDouble(split[0]);
String str = String.format("%1.2f", ingr2);
ingr2 = Double.valueOf(str);
SeperatorImperialList.add(ingr2);

}

}//end ingrSeperator

static int unit;
/**
* Gets choice of imperial or metric
*/
public static void choice() throws Exception{
Scanner sc = new Scanner(System.in);
System.out.println("Use imperial(1) or metric(2) units");
System.out.print("Enter 1 or 2: ");//serving size enetered
unit = sc.nextInt();

if (unit == 1){
divideImperial();
}
else{
converter();
}

sc.close();//scanner closed

}//end choice

//for imperial
/**
* Gets serving size input from user and then adjusts seperated ingredinets accordingly for imperial
*/
public static void divideImperial() throws Exception{
Scanner sc = new Scanner(System.in);

System.out.print("Enter your Imperial serving size: ");//serving size enetered
int servingSize = sc.nextInt();

for(int i=0;i<SeperatorImperialList.size();i++){
SeperatorImperialList.set(i,SeperatorImperialList.get(i)*servingSize);//origanal quantiys multiplyed by new serving size
}

for(int i=0;i<SeperatorImperialList.size();i++){
SeperatorImperialList.set(i,SeperatorImperialList.get(i)/4);//multiplyed quantitys now divided by origanal serving size 4
}
mergImperial();

sc.close();//scanner closed

}//end divideImperial

static ArrayList<String> convertedList = new ArrayList<>();//ArrayList for metric ingredients created

static ArrayList<Double> SeperatedMetricList = new ArrayList<Double>();//ArrayList for seperated metric ingredients created



/**
* Converts imperial to metric
*/
public static void converter() throws Exception{
Scanner sc = new Scanner(System.in);

//convert tbsp to ml
for(int i = 0; i < ingrList .size(); i++){
//if ingrList contains tbsp replaced with ml
if(ingrList.get(i).contains("tbsp")){
convertedList.add(ingrList.get(i).replace("tbsp", "ml"));
//for(int j=0;j<ingrSeperatorList.size();j++){
//ingrSeperatorList.set(j,ingrSeperatorList.get(j)*15);
//}
}
}

//convert cup to ml
for(int i = 0; i < ingrList .size(); i++){
//if ingrList contains cup replaced with ml
if(ingrList.get(i).contains("cup")){
convertedList.add(ingrList.get(i).replace("cup", "ml"));

// for(int j=0;j<ingrSeperatorList.size();j++){
// ingrSeperatorList.set(j,ingrSeperatorList.get(j)*250);

// }
}
}

//convert tsp to ml
for(int i = 0; i < ingrList .size(); i++){
//if ingrList contains tsp replaced with ml
if(ingrList.get(i).contains("tsp")){
convertedList.add(ingrList.get(i).replace("tsp", "ml"));
// for(int j=0;j<ingrSeperatorList.size();j++){
// ingrSeperatorList.set(j,ingrSeperatorList.get(j)*5);
// }
}
}
SeperatorMetric();

sc.close();//scanner closed
}//end converter

/**
* Seperates metric list
*/
public static void SeperatorMetric() throws Exception{

double ingr2 = 0;

for(int i = 0; i <convertedList.size(); i++){
String split [] = convertedList.get(i).split(" ");
ingr2 = Double.parseDouble(split[0]);
String str = String.format("%1.2f", ingr2);
ingr2 = Double.valueOf(str);
SeperatedMetricList.add(ingr2);
divideMetric();
}

}//end SeperatorMetric

/**
* Gets serving size input from user and then adjusts seperated ingredinets accordingly for metric
*/
public static void divideMetric() throws Exception{
Scanner sc = new Scanner(System.in);

System.out.print("Enter your Metric serving size: ");//serving size enetered
int servingSize = sc.nextInt();

for(int i=0;i<SeperatedMetricList.size();i++){
SeperatedMetricList.set(i,SeperatedMetricList.get(i)*servingSize);//origanal quantiys multiplyed by new serving size
}

for(int i=0;i<SeperatedMetricList.size();i++){
SeperatedMetricList.set(i,SeperatedMetricList.get(i)/4);//multiplyed quantitys now divided by origanal serving size 4
}
mergMetric();
sc.close();//scanner closed

}//end divideImperial



/**
* Merges imperial lists together
*/
public static void mergImperial(){
System.out.print(SeperatorImperialList);
System.out.println("Imperial");
//here I would merge the ingredients list with the new serving size list
}//end mergImperial

/**
* Merges metric lists together
*/
public static void mergMetric(){
System.out.print(SeperatedMetricList);
System.out.println("Metric");
//here I would merge the metric converted ingredients list with the new serving size list
}//end mergMetric

}//end main

最佳答案

您可以使用下面的代码合并List<Double>List<String>

public static List<String> merge() throws Exception{

List<String> newStringList = new ArrayList<>();

for(int i = 0; i <convertedList.size(); i++){
String split [] = convertedList.get(i).split(" ");
split[0] = SeperatedMetricList.get(i).toString();
String joinedString = String.join(" ", split);
newStringList.add(joinedString);
}
return newStringList;
}

作为旁注:声明方法和变量时请遵循命名约定。 Java naming Convetions

关于java - 如何将双数组列表与字符串数组列表合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59187816/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com