currency converter- why does my for loop in switch wont work? when i run my code only the first for loop works

AAAAAAAAAAAAAAA :

`I have to do a code which user will input dollar amount and currency(which where the dollar will be converted) for example 15 YEN which 15 is the dollar amount and YEN is where to be converted.My code only runs in the first for loop which it will scan a string and split it but on second for loop which the conversion happens wont work.

       for(i=0;i<=3;i++){
       temp = sc.nextLine();
       Temp = temp.split(" ");
    }

       for(i=0,j=1;i<=3;i+=2,j+=2){
           switch (Temp[j]) {
               case "PHP":
                   conversion = Double.parseDouble(Temp[i])*51.23;
                   System.out.println("$"+Temp[0]+" CONVERTS TO "+df.format(conversion)+" PHP.");
                   break;
               case "POUNDS":
                   conversion = Double.parseDouble(Temp[i])*0.84;
                   System.out.println("$"+Temp[2]+" CONVERTS TO "+df.format(conversion)+" POUNDS.");
                   break;
               case "LIRA":
                   conversion = Double.parseDouble(Temp[i])*2040;
                   System.out.println("$"+Temp[4]+" CONVERTS TO "+df.format(conversion)+" LIRA.");
                   break;
               case "FRANCS":
                   conversion = Double.parseDouble(Temp[i])*9.85;
                   System.out.println("$"+Temp[6]+" CONVERTS TO "+df.format(conversion)+" FRANCS.");
                   break;
               case "MARKS":
                   conversion = Double.parseDouble(Temp[i])*3.23;
                   System.out.println("$"+Temp[8]+" CONVERTS TO "+df.format(conversion)+" MARKS.");
                   break;
               case "YEN":
                   conversion = Double.parseDouble(Temp[i])*260;
                   System.out.println("$"+Temp[10]+" CONVERTS TO "+df.format(conversion)+" YEN.");
                   break;
               default:
                   ;
                   break;
           }
        System.out.println("Invalid input, Please try again");
       }
XBlueCode :

You can omit the second loop and include the switch statement inside the first loop:

  for(i=0;i<=3;i++)
  {
   temp = sc.nextLine();
   Temp = temp.split(" ");

       switch (Temp[1]) {
           case "PHP":
               conversion = Double.parseDouble(Temp[0])*51.23;
               System.out.println("$"+Temp[0]+" CONVERTS TO "+df.format(conversion)+" PHP.");
               break;
           // Other cases
           // ...
           // ...
           default:
               i--;
               System.out.println("Invalid input, Please try again");
       }
  }

and if you want to read all the 4 inputs (1 input per line) first before conversion then you need to store them in ArrayList of Strings then pass them to the loop.

        ArrayList<String> lines = new ArrayList<>();

        for(int i=0;i<=3;i++)
            lines.add(sc.nextLine());

        for(int i=0;i<=3;i++)
        {
            String Temp[] = lines.get(i).split(" ");

            switch (Temp[1]) {
                case "PHP":
                    conversion = Double.parseDouble(Temp[0])*51.23;
                    System.out.println(Temp[1]);
                    System.out.println("$"+Temp[0]+" CONVERTS TO "+df.format(conversion)+" PHP.");
                    break;
                // Other cases
                // ...
                // ...
                default:
                    continue;
                    //i--;
                    //System.out.println("Invalid input, Please try again");
            }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=165227&siteId=1