0

I have implemented factory pattern for calculating Series(triangle,fibonacci etc) using the following code. However, my scanner input is not getting read. Here is the code for explanation.

public interface Series {
    void calculate();

}

public class SeriesFactory {

    public Series getSeries(String series,int length) {
        // TODO Auto-generated constructor stub
        System.out.println("-------------- " + series);

        System.out.println("-------------- " + length);



            if(series=="fibonacci")
            return new Fibonacci();
            else if(series=="triangle")
            return new Triangle();
            return null;
    }

}

public class Triangle implements Series {

    public Triangle() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public void calculate() {
        System.out.println("I am in triangle");

    }

}

Main:

import java.util.Scanner;

public class Series_Main {
    public static void main(String[] args) {
        String s="";
        int ing=0;
        Scanner in=new Scanner(System.in);
        s=in.next();
        ing=in.nextInt();
        toString(ing, s);    
    }
    public static void toString(int a, String s) { 
        System.out.println("The number is "+a+", and the word is "+s); 
        SeriesFactory seriesFactory=new SeriesFactory();
        Series series1=seriesFactory.getSeries(s,a);
        series1.calculate();
    } 
}

My question is that the main method does pass my arguments which I give on command prompt,but it does not get inside triangle class's draw method. The following arguments were given by me:

triangle 1

durron597
  • 31,968
  • 17
  • 99
  • 158
user3400060
  • 299
  • 3
  • 9
  • 23

1 Answers1

0

nextInt() can be a drama man, try doing a empty in.nextLine() statement after ing=in.nextInt(); and let me know how you go.

LukeLad
  • 36
  • 3