[英]How to merge a double arraylist with a string arraylist
我有一项作业要求我从文本文件中读取食谱并处理测量单位和成分数量等内容。 我将食谱的各个部分分成一个数组列表来操作成分,因为用户将输入他们想要使用英制还是公制单位。 从这里我创建了一个双数组列表,用于从原始列表中分离数量,所以 4 汤匙盐就变成了 4.0.0.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 命名约定
您可以创建一个单独的数组列表并使用 for 循环将两个值一起转储,前提是您对它们进行了适当的索引。
public static void main(String[] args)
{
String unit = "Units";
int indexVariable = 0;
ArrayList<Double> doubleArrayList = new ArrayList<Double>();
doubleArrayList.add(0, 0.0);
doubleArrayList.add(1, 1.0);
doubleArrayList.add(2, 2.0);
doubleArrayList.add(3, 3.0);
doubleArrayList.add(4, 4.0);
ArrayList<String> stringArrayList = new ArrayList<String>();
stringArrayList.add(0, "Flour");
stringArrayList.add(1, "Egg");
stringArrayList.add(2, "butter");
stringArrayList.add(3, "water");
stringArrayList.add(4, "Pot");
ArrayList<String> combinedArrayList = new ArrayList<String>();
for(Double d : doubleArrayList)
{
combinedArrayList.add(indexVariable, "[" + indexVariable + "]: " + d + " " + unit + " of " + stringArrayList.get(indexVariable));
System.out.println(combinedArrayList.get(indexVariable));
indexVariable++;
}
}
您还可以使用doubleArrayList.indexOf(d)
来替换 indexVariable。
首先,创建一个内部成分类来保存您需要合并的信息。
private static class Ingredient {
final String name;
final double quantity
Ingredient(String name, double quantity) {
this.name = name;
this.quantity = quantity;
}
}
然后更改合并方法(我使用了公制方法)。 [我假设convertedList是这里的成分列表,如果有误请更正。]
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
ArrayList<Ingredient> ingredients = new ArrayList<Ingredient>();
int length = Math.Min(SeperatedMetricList.size(), convertedList.size());
for (int i = 0; i < length; i++) {
ingredients.add(new Ingredient(convertedList.get(i), SeperatedMetricList.get(i)));
}
// The list 'ingredients' should be done now, do whatever you want with it.
}//end mergMetric
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.