HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

State flowers and birds

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
andstatebirdsflowers

Problem

This runs as is, but I'm looking for an alternative way to write it.

```
import java.util.Scanner;

public class StateBirdAndFlowerProgram {

public static void main(String args[]) {

// Begin Scanner for user input later on
Scanner input = new Scanner( System.in );

//Begin array of information
String[][] states={
{"Alabama", "Flower: Camelia", "Bird: Northern flicker"},
{"Alaska", "Flower: Forget-me-not", "Bird: Willow ptarmigan "},
{"Arizona", "Flower: Saguaro cactus blossom", "Bird: Cactus wren "},
{"Arkansas", "Flower: Apple blossom", "Bird: Northern mockingbird "},
{"Califorina", "Flower: Califorina poppy", "Bird: California quail"},
{"Colorado", "Flower: Rocky Mountian columbine", "Bird: Lark bunting"},
{"Connecticut", "Flower: Mountain laurel ", "Bird: American robin"},
{"Delaware", "Flower: Peach blossom", "Bird: Delaware Blue Hen "},
{"Florida", "Flower: Orange blossom ", "Bird: Northern mockingbird "},
{"Georgia", "Flower: Cherokee rose ", "Bird: Brown thrasher ", },
{"Hawaii", "Flower: Hawaiian hibiscus ", "Bird: Nēnē or Hawaiian goose"},
{"Idaho", "Flower: Syringa, mock orange ", "Bird: Mountain bluebird "},
{"Illinois", "Flower: Violet ", "Bird: Northern cardinal "},
{"Indiana", "Flower: Peony ", "Bird: Northern cardinal "},
{"Iowa ", "Flower: Wild prairie rose ", "Bird: Eastern goldfinch "},
{"Kansas", "Flower: Sunflower", "Bird: Western meadowlark"},
{"Kentucky", "Flower: Goldenrod ", "Bird: Northern cardinal"},
{"Louisiana", "Flower: Magnolia ", "Bird: Brown pelican "},

Solution

Bug

I ran your code, and got this output:

Enter a state:
 Indiana
Bird: Flower: Peony
Flower: Bird: Northern cardinal


So, two things:

1) Your columns are in the wrong order or incorrectly labeled;

2) Your labels are duplicated. They are already in your data (which is unusual) so you could remove the labels from your print clause

System.out.println(states[row][column+1]);
                      System.out.println(states[row][column+2] + "\n");


In a more realistic situation though, very likely the labels would not be included along with the data

Better UI

Your user input is not very intuitive. Take this series of input:

Enter a state: 
 Boston
Enter a state: 
 Canada
Enter a state: 
 42
Enter a state: 
 None


This should be pretty easy for you to change the code to add a System.out.println("State not found: " + stateName) that's reached if the value entered is not found in the array.

Also consider adding this System.out.println("Enter a state: (or None to exit)"); as the way to exit your program is not obvious as it is.

Code comments

I'm not sure whether or not commenting every line of code was a requirement by your teacher, but most programmers would find it very excessive. In production code, you don't often see code comments (besides language-specific constructs like JavaDoc) as the code should speak for itself as to what it does. You normally only need to add comment if the reasoning behind a certain piece of code is not clear.

These are particularly excessive:

} // end if

                } // end for

           } // end else

      } // end while

 } // end main
} // end class


It is actually better in many cases not to declare variables until shortly before you need them. It makes code much more difficult to follow if you have to always look up and down to find variable declarations their usage. It also make it easier to keep variables in the correct scope.

// Begin Scanner for user input later on
  Scanner input = new Scanner( System.in );


I'm sure others will find plenty of other things to critique and help you with your code, this is just a start.

Code Snippets

Enter a state:
 Indiana
Bird: Flower: Peony
Flower: Bird: Northern cardinal
System.out.println(states[row][column+1]);
                      System.out.println(states[row][column+2] + "\n");
Enter a state: 
 Boston
Enter a state: 
 Canada
Enter a state: 
 42
Enter a state: 
 None
} // end if

                } // end for

           } // end else

      } // end while

 } // end main
} // end class
// Begin Scanner for user input later on
  Scanner input = new Scanner( System.in );

Context

StackExchange Code Review Q#106052, answer score: 8

Revisions (0)

No revisions yet.