biloonline
asked on
Prompt the user to select one of four patterns
I asked to prompt the user to select one of 4 patterns and display the pattern selected. How do I prompt the user which one to choose from, (either Pattern I or Pattern II)?
<code>
public class Pattern {
public static void main(String[] args) {
final int NUM_OF_LINES = 6;
for (int row = 1; row <= NUM_OF_LINES; row++) {
for (int num = 2; num <= row; num++)
System.out.print(num);
for (int column = 1; column <= NUM_OF_LINES - row; column++)
System.out.print(" ");
System.out.println();
}
for (int row = 1; row <= NUM_OF_LINES; row++) {
for (int column = 1; column <= NUM_OF_LINES - row; column++)
System.out.print(" ");
for (int num = 2; num <= row; num++)
System.out.print(num);
System.out.println();
}
}
}
<code>
public class Pattern {
public static void main(String[] args) {
final int NUM_OF_LINES = 6;
for (int row = 1; row <= NUM_OF_LINES; row++) {
for (int num = 2; num <= row; num++)
System.out.print(num);
for (int column = 1; column <= NUM_OF_LINES - row; column++)
System.out.print(" ");
System.out.println();
}
for (int row = 1; row <= NUM_OF_LINES; row++) {
for (int column = 1; column <= NUM_OF_LINES - row; column++)
System.out.print(" ");
for (int num = 2; num <= row; num++)
System.out.print(num);
System.out.println();
}
}
}
use
System.in.read()
System.in.read()
ASKER
I know how to input an answer from the user, in my case( Pattern I, Pattern II), but how would the program know what to display, whether Pattern I or II?
ASKER
Also how can I construct a for loop to display the following pattern:
1
21
321
4321
54321
654321
Thanks
1
21
321
4321
54321
654321
Thanks
You could also use JOptionPane.
At the top of your class file put "import javax.swing.JOptionPane;"
Code:
String inString = JOptionPane.showInputDialo g("Some text to display telling the user what to enter.");
/*At this point you can just check if they entered the right string. In the "Some text to display..." area you should tell them what the possible inputs are.*/
if (inString.equals("Pattern 1")){//Do Pattern 1
}
else if (inString.equals("Pattern 2")){//Do Pattern 2
}
else if (inString.equals("Pattern 3")){//Do Pattern 3
}
else if (inString.equals("Pattern 4")){//Do Pattern 4
}
else{JOptionPane.showMessa geDialog(n ull, "Bad input.");}
At the top of your class file put "import javax.swing.JOptionPane;"
Code:
String inString = JOptionPane.showInputDialo
/*At this point you can just check if they entered the right string. In the "Some text to display..." area you should tell them what the possible inputs are.*/
if (inString.equals("Pattern 1")){//Do Pattern 1
}
else if (inString.equals("Pattern 2")){//Do Pattern 2
}
else if (inString.equals("Pattern 3")){//Do Pattern 3
}
else if (inString.equals("Pattern 4")){//Do Pattern 4
}
else{JOptionPane.showMessa
ASKER
Disregard the last question, I got it.
ASKER
CI-Ia0s,
I am asked to use:
BufferedReader stdin = new BufferedReader (new InputStreamReader(System.i n));
Thanks
I am asked to use:
BufferedReader stdin = new BufferedReader (new InputStreamReader(System.i
Thanks
int number = 0;
try{
number = System.in.read();
}catch(Exception ex){
}
if((char)number == '1'){
// first pattern
}else if((char)number == '2'){
//pattern 2
}
try{
number = System.in.read();
}catch(Exception ex){
}
if((char)number == '1'){
// first pattern
}else if((char)number == '2'){
//pattern 2
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
First question: Use an if statement.
Second question:
//Declare numDigits to be the number of digits you want
int wholeNumber = 0;
int digitHolder = 1;
for (int c = 1; c < numDigits; c++){
wholeNumber = wholeNumber + (c * digitHolder);
digitHolder = digitHolder * 10;
}
Second question:
//Declare numDigits to be the number of digits you want
int wholeNumber = 0;
int digitHolder = 1;
for (int c = 1; c < numDigits; c++){
wholeNumber = wholeNumber + (c * digitHolder);
digitHolder = digitHolder * 10;
}
ASKER
<code>
import java.io.*;
public class Pattern4 {
public static void main(String[] args) throws IOException {
final int NUM_OF_LINES = 6;
int number;
BufferedReader stdin = new BufferedReader (new InputStreamReader(System.i n));
System.out.println("Select Pattern 1 or 2 or 3 or 4 or 9 to exit the application");
number = stdin.readLine();
if (number == '1')
{
for (int row = 1; row <= NUM_OF_LINES; row++) {
for (int num = 1; num <= row; num++)
System.out.print(num);
for (int column = 1; column <= NUM_OF_LINES - row; column++)
System.out.print(" ");
System.out.println();
}
}
if (number == '2')
{
for (int row = 1; row <= NUM_OF_LINES; row++) {
for (int column = 1; column <= NUM_OF_LINES - row; column++)
System.out.print(" ");
for (int num = row; num >= 1; num--)
System.out.print(num);
System.out.println();
}
}
if (number == '3')
{
for (int row = 1; row <= NUM_OF_LINES; row++) {
for (int num = 1; num < row; num++)
System.out.print(" ");
for (int column = 1; column <= NUM_OF_LINES - row + 1; column++)
System.out.print(column);
System.out.print(" ");
}
if (number == '4')
{
for (int row = 1; row <= NUM_OF_LINES; row++) {
for (int num = 1; num < row; num++)
System.out.print(" ");
for (int column = 1; column <= NUM_OF_LINES - row + 1; column++)
System.out.print(column);
System.out.println();
}
}
}
}
}
</code>
This is what I have gotten so far, I am getting 1 error
E:\Program Files\Xinox Software\JCreatorV3 LE\MyProjects\Hw3Pr1\Patte rn4.java:1 2: incompatible types
found : java.lang.String
required: int
number = stdin.readLine();
^
1 error
Process completed.
Please advise,
Thanks
import java.io.*;
public class Pattern4 {
public static void main(String[] args) throws IOException {
final int NUM_OF_LINES = 6;
int number;
BufferedReader stdin = new BufferedReader (new InputStreamReader(System.i
System.out.println("Select
number = stdin.readLine();
if (number == '1')
{
for (int row = 1; row <= NUM_OF_LINES; row++) {
for (int num = 1; num <= row; num++)
System.out.print(num);
for (int column = 1; column <= NUM_OF_LINES - row; column++)
System.out.print(" ");
System.out.println();
}
}
if (number == '2')
{
for (int row = 1; row <= NUM_OF_LINES; row++) {
for (int column = 1; column <= NUM_OF_LINES - row; column++)
System.out.print(" ");
for (int num = row; num >= 1; num--)
System.out.print(num);
System.out.println();
}
}
if (number == '3')
{
for (int row = 1; row <= NUM_OF_LINES; row++) {
for (int num = 1; num < row; num++)
System.out.print(" ");
for (int column = 1; column <= NUM_OF_LINES - row + 1; column++)
System.out.print(column);
System.out.print(" ");
}
if (number == '4')
{
for (int row = 1; row <= NUM_OF_LINES; row++) {
for (int num = 1; num < row; num++)
System.out.print(" ");
for (int column = 1; column <= NUM_OF_LINES - row + 1; column++)
System.out.print(column);
System.out.println();
}
}
}
}
}
</code>
This is what I have gotten so far, I am getting 1 error
E:\Program Files\Xinox Software\JCreatorV3 LE\MyProjects\Hw3Pr1\Patte
found : java.lang.String
required: int
number = stdin.readLine();
^
1 error
Process completed.
Please advise,
Thanks
ASKER
if (number == '1') <-- I guess that would not be right, since in this case '1' is considered as a CHAR.
How do I convert from String to Char?
How do I convert from String to Char?
ASKER
Here is an updated version, It compiled with no errors, I will run it now ...
import java.io.*;
public class Pattern4 {
public static void main(String[] args) throws IOException {
final int NUM_OF_LINES = 6;
BufferedReader stdin = new BufferedReader (new InputStreamReader(System.i n));
System.out.println("Select Pattern 1 or 2 or 3 or 4 or 9 to exit the application");
String number = stdin.readLine();
int numb = Integer.parseInt(number);
if (numb == 1)
{
for (int row = 1; row <= NUM_OF_LINES; row++) {
for (int num = 1; num <= row; num++)
System.out.print(num);
for (int column = 1; column <= NUM_OF_LINES - row; column++)
System.out.print(" ");
System.out.println();
}
}
if (numb == 2)
{
for (int row = 1; row <= NUM_OF_LINES; row++) {
for (int column = 1; column <= NUM_OF_LINES - row; column++)
System.out.print(" ");
for (int num = row; num >= 1; num--)
System.out.print(num);
System.out.println();
}
}
if (numb == 3)
{
for (int row = 1; row <= NUM_OF_LINES; row++) {
for (int num = 1; num < row; num++)
System.out.print(" ");
for (int column = 1; column <= NUM_OF_LINES - row + 1; column++)
System.out.print(column);
System.out.print(" ");
}
if (numb == 4)
{
for (int row = 1; row <= NUM_OF_LINES; row++) {
for (int num = 1; num < row; num++)
System.out.print(" ");
for (int column = 1; column <= NUM_OF_LINES - row + 1; column++)
System.out.print(column);
System.out.println();
}
}
}
}
}
import java.io.*;
public class Pattern4 {
public static void main(String[] args) throws IOException {
final int NUM_OF_LINES = 6;
BufferedReader stdin = new BufferedReader (new InputStreamReader(System.i
System.out.println("Select
String number = stdin.readLine();
int numb = Integer.parseInt(number);
if (numb == 1)
{
for (int row = 1; row <= NUM_OF_LINES; row++) {
for (int num = 1; num <= row; num++)
System.out.print(num);
for (int column = 1; column <= NUM_OF_LINES - row; column++)
System.out.print(" ");
System.out.println();
}
}
if (numb == 2)
{
for (int row = 1; row <= NUM_OF_LINES; row++) {
for (int column = 1; column <= NUM_OF_LINES - row; column++)
System.out.print(" ");
for (int num = row; num >= 1; num--)
System.out.print(num);
System.out.println();
}
}
if (numb == 3)
{
for (int row = 1; row <= NUM_OF_LINES; row++) {
for (int num = 1; num < row; num++)
System.out.print(" ");
for (int column = 1; column <= NUM_OF_LINES - row + 1; column++)
System.out.print(column);
System.out.print(" ");
}
if (numb == 4)
{
for (int row = 1; row <= NUM_OF_LINES; row++) {
for (int num = 1; num < row; num++)
System.out.print(" ");
for (int column = 1; column <= NUM_OF_LINES - row + 1; column++)
System.out.print(column);
System.out.println();
}
}
}
}
}
ASKER
and added this at the end:
if (numb == 9)
return;
Looks like it's working fine.
Thanks guys.
if (numb == 9)
return;
Looks like it's working fine.
Thanks guys.
if (number == '1')
should be
if ((char)number == '1')
should be
if ((char)number == '1')
ASKER
But I am working with Int and not Char, the program compiles and works fine.
from api
read
public abstract int read()
throws IOExceptionReads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.
A subclass must provide an implementation of this method.
read
public abstract int read()
throws IOExceptionReads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.
A subclass must provide an implementation of this method.
ASKER
Thanks again.
ASKER