Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

in the QuestionB4_Poetry i have done it and got the same output but i need help in reducing redundancy.

+4 votes
Scanner console= new Scanner(System.in);

System.out.print("Choose limerick or haiku: ");

String poetry = console.nextLine();

int longestLine = 0;

int x = 0;

String line = "";

if (poetry.equals ("limerick")){

for ( int i = 1; i <= 5; i++){

System.out.print("Enter line " + i + ": ");

line = console.nextLine();

x = x + line.length();

if ( line.length()> longestLine){

longestLine = line.length();

}

}

}else if (poetry.equals ("haiku")){

for ( int i = 1; i <= 3; i++){

System.out.print("Enter line " + i + ": ");

line = console.nextLine();

x = x + line.length();

if ( line.length()> longestLine){

longestLine = line.length();

}

}

System.out.println();

System.out.println("You typed " + x + " characters.");

System.out.println("Your longest line had " + longestLine + " characters.");

}

}
asked Oct 10 in CSC 211 (F17) by Thomas Ayele (2,875 points)
which year is this?
It's the 2015-16winter term one

1 Answer

0 votes
If you really wanted to, you could make another method for the inside of the if/else statement that would be called.  The main method would look much cleaner.  Although, you would still have the messy/redundant code in a different method, it just looks nicer.
answered Oct 11 by Nicholas Garling (2,304 points)
...