[英]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.