0

Could you please explain why this loop doesn't work in case if user types "yes" and why there are errors with variables initialisations.

import java.util.ArrayList;
import java.util.Scanner;

public class Main { 

  public static void main(String args[]) {
    Scanner src;
    String amount;
    String counterparty;
    String dt;
    String ct;
    System.out.println("Create new transaction:yes/no");
    Scanner abc = new Scanner(System.in);
    String g = abc.nextLine();

    if (g=="yes") {
      System.out.println("Amount of transaction:");
      src = new Scanner(System.in);
      amount = src.nextLine();
      System.out.println("Counterparty:");
      counterparty = src.nextLine();
      System.out.println("Dt:");
      dt = src.nextLine();
      System.out.println("Ct:");
      ct = src.nextLine();
    }
    else if (g=="no") {
      amount="0";
    }

    System.out.println("Transaction:");
    ArrayList <String> Provodka = new ArrayList();
    Provodka.add(amount);
    Provodka.add(counterparty);
    Provodka.add(dt);
    Provodka.add(ct);
    for (int i = 0; i < Provodka.size(); i++) {
        String value = Provodka.get(i);
        System.out.println("Element: " + value);
    }
  }
}
thegauravmahawar
  • 2,802
  • 3
  • 14
  • 23
Lucky_girl
  • 4,543
  • 6
  • 42
  • 82
  • 1
    Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – SomeJavaGuy Oct 09 '15 at 11:00
  • Also you get errors, because you need to initialize Strings as either `amount = null` or `amount = ""`. – SomeJavaGuy Oct 09 '15 at 11:03

3 Answers3

1

Just initialize the local variables and use equals() method instead of "=="

public static void main(String args[]) {
    Scanner src;
    String amount = null;
    String counterparty = null;
    String dt = null;
    String ct = null;
    System.out.println("Create new transaction:yes/no");
    Scanner abc = new Scanner(System.in);
    String g = abc.nextLine();
           if (g.equals("yes"))
                   {
        System.out.println("Amount of transaction:");
         src = new Scanner(System.in);
        amount = src.nextLine();
        System.out.println("Counterparty:");
         counterparty = src.nextLine();
        System.out.println("Dt:");
         dt = src.nextLine();
        System.out.println("Ct:");
         ct = src.nextLine();
    }
  else if (g.equals("no")){
   amount="0";
    }

    System.out.println("Transaction:");
    ArrayList <String> Provodka = new ArrayList();
    Provodka.add(amount);
    Provodka.add(counterparty);
    Provodka.add(dt);
    Provodka.add(ct);
    for (int i = 0; i < Provodka.size(); i++) {
        String value = Provodka.get(i);
        System.out.println("Element: " + value);
    }
}
Shailesh Yadav
  • 1,061
  • 1
  • 15
  • 30
0

In your string comparison you are comparing strings with '==' . Use equals() method to compare strings. eg:-

if ("yes".equals(g)){

}
Vikas Sharma
  • 745
  • 9
  • 19
0

Firstly, there is a lot of unnecessary declaration of Scanner. Using one variable for scanner will work for all inputs. Secondly, declare your variables above the main method and make them static, here you will not always need to initialise them. Finally, use g.equalsIgnoreCase("yes") instead of g == "yes", this way if you type yes in CAPS or not it will still register. Try what was done below

    public static String g; 
    public static String amount;
    public static String counterparty;
    public static String dt;
    public static String ct;

    public static void main(String args[]) {

          Scanner s = new Scanner(System.in);


          System.out.println("Create new transaction:yes/no");
              g= s.nextLine();

          if (g.equalsIgnoreCase("yes")) {
              System.out.println("Amount of transaction: ");
                  amount = s.nextLine();

              System.out.println("Counterparty: ");
                  counterparty = s.nextLine();

              System.out.println("Dt: ");
                  dt = s.nextLine();

              System.out.println("Ct: ");
                  ct = s.nextLine();
          }
          else if (g.equalsIgnoreCase("no")) {

              amount = "0";
          }

          System.out.println("Transaction:");
          ArrayList <String> Provodka = new ArrayList();
          Provodka.add(amount);
          Provodka.add(counterparty);
          Provodka.add(dt);
          Provodka.add(ct);

          for (int i = 0; i < Provodka.size(); i++) {
              String value = Provodka.get(i);
              System.out.println("Element: " + value);
          }
      }
  }
darkhouse
  • 205
  • 5
  • 15