After getting help and configuring my diamond program, I still am having trouble
ID: 3621952 • Letter: A
Question
After getting help and configuring my diamond program, I still am having trouble getting the do while loop to end after entering the correct input. If anybody can help me resolve this issue, I would greatly appreciate it!
Here is my program:
// This program displays diamonds as text using a character provided by the user
import java.util.Scanner;
public class Pattern
{
public static void main(String [] args)
{
Scanner kb = new Scanner(System.in);
String input;
int size;
char character;
do
{
System.out.print("Enter a diamond size ("short", "average","tall"): ");
input = kb.nextLine().toLowerCase();
//function call to get size
size = checkSize(input);
System.out.print("Enter pattern character: ");
input=kb.nextLine();
character=input.charAt(0);
//function call to display pattern
displayPattern(size,character);
}while((input!="short")&&(input!="average")&&(input!="tall"));;
}
public static int checkSize(String size)
{
if(size.equals("short"))
return 6;
else if(size.equals("average"))
return 12;
else if(size.equals("tall"))
return 22;
else
return -1;
}
public static void displayPattern(int size, char ch)
{
for(int i = size; i >= 1; i-- )
{
int j;
for( j = 1; j < i; j++ )
{
System.out.print(" ");
}
for(int k=size; k >= j*2; k-- )
{
System.out.print(ch);
}
System.out.println();
}
for(int i = 2; i <= size; i++ )
{
System.out.print(" ");
int j;
for( j = 2; j < i; j++ )
{
System.out.print(" ");
}
for( int k = size; k >= j * 2; k-- )
{
System.out.print(ch);
}
System.out.println();
}
}
}
Here is the output:
java Pattern
Enter a diamond size ("short", "average","tall"): short
Enter pattern character: m
m
mmm
mmmmm
mmm
m
Enter a diamond size ("short", "average","tall"):
Explanation / Answer
Hope this helps. Let me know if you have any questions. Please rate. :) The problem lies in how Java handles Strings. You are comparing the actual object references with the "!=" where you actually want to compare the contents. Instead of using "!=" use "!str1.equalsIgnoreCase(str2)". You also were overwriting the size input with the character input, so I split it into two different String inputs (input1, and input2). This should work: // This program displays diamonds as text using a character provided by the user import java.util.Scanner; public class Pattern { public static void main(String[] args) { Scanner kb = new Scanner(System.in); String input1; String input2; int size; char character; do { System.out .print("Enter a diamond size ("short", "average","tall"): "); input1 = kb.nextLine().toLowerCase(); // function call to get size size = checkSize(input1); System.out.print("Enter pattern character: "); input2 = kb.nextLine(); character = input2.charAt(0); // function call to display pattern displayPattern(size, character); } while (!(input1.equalsIgnoreCase("short") || input1.equalsIgnoreCase("average") || input1.equalsIgnoreCase("tall"))); } public static int checkSize(String size) { if (size.equals("short")) return 6; else if (size.equals("average")) return 12; else if (size.equals("tall")) return 22; else return -1; } public static void displayPattern(int size, char ch) { for (int i = size; i >= 1; i--) { int j; for (j = 1; j = j * 2; k--) { System.out.print(ch); } System.out.println(); } for (int i = 2; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.