Hey everyone so I am stuck on the last part of this program that I have been doing for an assignment. So generally I am given a large list of names and numbers that represent popularity ranks for names in certain decades. This is what the file looks like <----- this is a link to see the names.txt
So I created a method that sorts the list in a certain decade which is the getIndexOfSmallest and interchange. Every decade has two names that are ranked the same so between the years of 1900-1909 there are two names that are ranked 1, two names that are ranked 2, two names that are ranked 3 and so on. The last part of the program and the one I need help with is suppose to sort through the object array and find any decades where there is only one name for one rank or no names for a certain rank. The output will be sent to a file and it will look like this. <------ this is another link to see the expected output
This is what my code looks like. This is my Name code that creates the objects:
public class Name{
private String givenName;
private int[] ranks = new int[12];
public Name(String name, int[] popularityRanks){
givenName = name;
for (int i = 0; i < 11; i++){
ranks[i] = popularityRanks[i];
}
}
public String getName(){
return givenName;
}
public int getPop(int decade){
if (decade >= 0 && decade <= 10){
return ranks[decade];
}
else{
return -1;
}
}
public String getHistoLine(int decade){
String histoLine = ranks[decade] + ": ";
double popularity = (1000 - ranks[decade]) / 11.7;
int histo = (int)popularity;
if(popularity != 0){
for (int i = 0; i < histo; i++){
histoLine += "*";
}
}
return histoLine;
}
public String getHistogram(){
String histogram = "";
for (int i = 0; i < 11; i++){
histogram += this.getHistoLine(i) + "\n";
}
return histogram;
}
}
This is my NameApp which is where my main is at:
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class NameApp{
private static boolean validInput;
private static boolean stillWorking = true;
private static boolean validDecade;
private static boolean validName;
static Scanner keyboard = new Scanner(System.in);
// Main method
public static void main(String[] args) throws FileNotFoundException{
String[] nameArray = readNamesFile();
Name[] list = new Name[nameArray.length];
loadNames(list, nameArray);
char choice;
do {
do {
displayMenu();
choice = getUserInput();
} while (!validInput);
switch (choice){
case 'A':
displayHistogram(list);
break;
case 'B':
compareTwoNames(list);
break;
case 'C':
displayTopTenNames(list);
break;
case 'D':
writeAnomaliesToFile(list);
stillWorking = false;
break;
default:
break;
}
} while (stillWorking);
}
/*
* This method will read the file name names.txt and load the names and populations into a string array.
*/
private static String[] readNamesFile() throws FileNotFoundException{
String[] nameArray = new String[4429];
Scanner inputStream = null;
String fileName = "names.txt";
inputStream = new Scanner (new File(fileName));
int i = 0;
while (inputStream.hasNextLine()){
nameArray[i] = inputStream.nextLine();
i++;
}
inputStream.close();
return nameArray;
}
/*
* load names method will take a Name array and the string array from the readNamesFile method. This method will split the names and the population ranks and send them to the constructor in Name.java.
*/
private static void loadNames(Name[] list, String[] nameArray){
int length;
int spacePos;
int[] popRanks = new int[11];
String name;
String linePop;
for (int i = 0; i < nameArray.length; i++){
length = nameArray[i].length();
spacePos = nameArray[i].indexOf(" ");
name = nameArray[i].substring(0,spacePos);
linePop = nameArray[i].substring(spacePos + 1, length);
for (int j = 0; j < 11; j++){
popRanks[j] = Integer.parseInt(linePop.split(" ")[j]);
}
list[i] = new Name(name, popRanks);
}
}
/*
* displayMenu method will display the menu that the user will select their program function.
*/
private static void displayMenu(){
System.out.println("Enter the character corresponding to your selection:");
System.out.println("\ta - Print histogram for a name");
System.out.println("\tb - Compare two names in a decade");
System.out.println("\tc - Print top ten names for a decade");
System.out.println("\td - Quit (display file anomalies)");
}
/*
* getUserInput is a method that will accept a string input from the user it will send this string to two different helper methods.
*/
private static char getUserInput(){
String selection = keyboard.nextLine();
System.out.println(" Your selection: " + selection);
checkUserInput(selection);
char choice = stringToChar(selection);
return choice;
}
/*
* helper method: checkUserInput will accept the user input from getUserInput and test the input to see if the input is a valid input from the user. If it is not set the instance boolean variable to false if it is set it to true.
*/
private static boolean checkUserInput(String selection){
if (!selection.equalsIgnoreCase("a") && !selection.equalsIgnoreCase("b") && !selection.equalsIgnoreCase("c") && !selection.equalsIgnoreCase("d")){
System.out.println("Invalid input. Try again...");
return validInput = false;
}
else {
return validInput = true;
}
}
/*
* helper method: stringToChar method will take the input that the user entered after it has been tested to see if it is valid and this method will change the input to a character value. This will also make the character to an upper case letter.
*/
private static char stringToChar(String selection){
char choice = selection.charAt(0);
choice = Character.toUpperCase(choice);
return choice;
}
/*
* Menu option: A. This method will take a user input for the name they want and display the histogram for the selected name.
*/
private static void displayHistogram(Name[] list){
String nameInput;
String histogram;
int nameLocation;
do {
nameInput = nameEntry();
nameLocation = checkListArray(nameInput, list);
if (!validName){
System.out.println("The name, " + nameInput + ", was not found!");
}
} while (!validName);
histogram = list[nameLocation].getHistogram();
System.out.println("Histogram for name, " + list[nameLocation].getName() + ":");
System.out.println(histogram);
}
private static void compareTwoNames(Name[] list){
String nameOne;
String nameTwo;
String oneHistoLine;
String twoHistoLine;
int oneLocation;
int twoLocation;
int decade;
do {
nameOne = nameEntry();
oneLocation = checkListArray(nameOne, list);
if (!validName){
System.out.println("The first name, " + nameOne + ", was not found!");
}
} while (!validName);
do {
nameTwo = nameEntry();
twoLocation = checkListArray(nameTwo, list);
if (!validName){
System.out.println("The second name, " + nameTwo + ", was not found!");
}
} while(!validName);
decadeMenu();
decade = decadeSelection();
oneHistoLine = list[oneLocation].getHistoLine(decade);
twoHistoLine = list[twoLocation].getHistoLine(decade);
System.out.println("Data for " + list[oneLocation].getName());
System.out.println(" " + oneHistoLine);
System.out.println("Data for " + list[twoLocation].getName());
System.out.println(" " + twoHistoLine);
}
private static void displayTopTenNames(Name[] list){
int decade;
int count = 0;
int l = 0;
String[] decadeName = new String[20];
Name[] temp = new Name[list.length];
decadeMenu();
decade = decadeSelection();
for (int i = 0; i < list.length; i++){
temp[i] = list[i];
}
for (int index = 0; index < temp.length; index++){
int smallestIndex = getIndexOfSmallest(decade, index, temp);
interchange(decade, index, smallestIndex, temp);
}
do {
if (temp[l].getPop(decade) == 0){
l++;
}
else {
decadeName[count] = temp[l].getName() + " " + "(" + temp[l].getPop(decade) + ")";
count++;
l++;
}
} while (count < 20);
writeTopTen(decadeName, decade);
}
private static void writeAnomaliesToFile(Name[] list){
System.out.println("Terminating... but first the anomalies in the data file:");
checkAnomalies(list);
System.out.println("Anomalies written to anomalies.txt.");
}
private static String nameEntry(){
String nameInput = "";
System.out.println("Enter a name: ");
nameInput = keyboard.nextLine();
return nameInput;
}
private static int checkListArray(String nameInput, Name[] list){
int nameLocation = -1;
int listLength = list.length;
for (int i = 0; i < listLength; i++){
if (nameInput.equalsIgnoreCase(list[i].getName())){
validName = true;
return nameLocation = i;
}
}
if (nameLocation == -1){
validName = false;
return nameLocation;
}
return nameLocation;
}
private static void decadeMenu(){
System.out.println("Enter number correpsonding to your decade:");
System.out.println(" 1 - 1900-1909");
System.out.println(" 2 - 1910-1919");
System.out.println(" 3 - 1920-1929");
System.out.println(" 4 - 1930-1939");
System.out.println(" 5 - 1940-1949");
System.out.println(" 6 - 1950-1959");
System.out.println(" 7 - 1960-1969");
System.out.println(" 8 - 1970-1979");
System.out.println(" 9 - 1980-1989");
System.out.println(" 10 - 1990-1999");
System.out.println(" 11 - 2000-2005");
}
private static int decadeSelection(){
String decadeChoice;
int decade;
do {
System.out.println("Enter a decade: ");
decadeChoice = keyboard.nextLine();
decade = checkDecade(decadeChoice);
} while (!validDecade);
return decade;
}
private static int checkDecade(String decadeChoice){
int decade = 0;
try {
decade = Integer.parseInt(decadeChoice);
}
catch (Exception e){
System.out.println("That is not an integer. Please try again.");
validDecade = false;
return decade;
}
if (decade < 1 || decade > 11){
System.out.println("Enter an integer between 1 and 11");
validDecade = false;
return decade;
}
else {
validDecade = true;
decade = changeDecade(decade);
return decade;
}
}
private static int changeDecade(int decade){
int newDecade = 0;
switch (decade){
case 1:
newDecade = 0;
break;
case 2:
newDecade = 1;
break;
case 3:
newDecade = 2;
break;
case 4:
newDecade = 3;
break;
case 5:
newDecade = 4;
break;
case 6:
newDecade = 5;
break;
case 7:
newDecade = 6;
break;
case 8:
newDecade = 7;
break;
case 9:
newDecade = 8;
break;
case 10:
newDecade = 9;
break;
case 11:
newDecade = 10;
break;
default:
break;
}
return newDecade;
}
private static int getIndexOfSmallest(int decade, int startIndex, Name[] temp){
int min = temp[startIndex].getPop(decade);
int indexOfMin = startIndex;
for (int index = startIndex + 1; index < temp.length; index++){
if (temp[index].getPop(decade) < min){
min = temp[index].getPop(decade);
indexOfMin = index;
}
}
return indexOfMin;
}
private static void interchange(int decade, int i, int j, Name[] temp){
Name tempInt = temp[i];
temp[i] = temp[j];
temp[j] = tempInt;
}
private static String decadeYears(int decade){
String decadeYear = "";
switch (decade){
case 0:
decadeYear = "1900 - 1909";
break;
case 1:
decadeYear = "1910 - 1919";
break;
case 2:
decadeYear = "1920 - 1929";
break;
case 3:
decadeYear = "1930 - 1939";
break;
case 4:
decadeYear = "1940 - 1949";
break;
case 5:
decadeYear = "1950 - 1959";
break;
case 6:
decadeYear = "1960 - 1969";
break;
case 7:
decadeYear = "1970 - 1979";
break;
case 8:
decadeYear = "1980 - 1989";
break;
case 9:
decadeYear = "1990 - 1999";
break;
case 10:
decadeYear = "2000 - 2005";
break;
default:
break;
}
return decadeYear;
}
private static void writeTopTen(String[] decadeName, int decade){
String years;
years = decadeYears(decade);
System.out.println("Ten most popular names (male and female) during the decade " + years + " were:");
for (int i = 0; i < 20; i += 2){
System.out.printf("%20s\t%20s\n", decadeName[i],decadeName[i + 1]);
}
}
private static void checkAnomalies(Name[] list){
Name[] temp = new Name[list.length];
int anomalyCount = 0;
int popTwo = 0;
int popOne = 0;
String[] anomalies = new String[list.length];
for (int i = 0; i < list.length; i++){
temp[i] = list[i];
}
for (int decade = 0; decade < 11; decade++){
for (int index = 0; index < temp.length; index++){
int smallestIndex = getIndexOfSmallest(decade, index, temp);
interchange(decade, index, smallestIndex, temp);
}
int rank = 0;
for (int i = 1; i < temp.length - 1; i += 2){
popOne = temp[i].getPop(decade);
popTwo = temp[i+1].getPop(decade);
if (popOne != 0){
rank++;
}
if (popOne == rank && popTwo != rank){
String decadeYear = decadeYears(decade);
anomalies[anomalyCount] = "One name (" + temp[i].getName() + ") for " + decadeYear + ", rank " + temp[i].getPop(decade) + ".";
anomalyCount++;
}
else if (popOne != rank && popTwo == rank){
String decadeYear = decadeYears(decade);
anomalies[anomalyCount] = "One name (" + temp[i+1].getName() + ") for " + decadeYear + ", rank " + temp[i+1].getPop(decade) + ".";
anomalyCount++;
}
else if (popOne != rank && popTwo != rank){
String decadeYear = decadeYears(decade);
anomalies[anomalyCount] = "No names for " + decadeYear + ", rank " + temp[i].getPop(decade) + ".";
anomalyCount++;
}
}
}
}
}
This is the information provided by my professor:
There are 1,065 anomalies: in some cases, only one name with a particular rank in a decade or in other cases, no names in a decade with a particular rank. With 11 decades and 999 ranks to check, 1065 is only about 9.7% of the name-pairs that are broken or anomalous. There should be a pair of names for each rank (999) in each decade (11): this gives 10,989 name-pairs. Your code to do this will need to check each name in each decade for each rank in order to find all 1065 anomalies.
When I was debugging my program I saw that the ranks end on odd numbers. which is why I started my loop in checkAnomalies at 1 instead of 0 (considering that at 0 the names rank will be 0 and that is considered to be a name that was picked 1000 or less). When I run the program with:
String[] anomalies = new String[1065];
I get this error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1065
at NameApp.checkAnomalies(NameApp.java:512)
at NameApp.writeAnomaliesToFile(NameApp.java:259)
at NameApp.main(NameApp.java:43)
and when I change my code to be:
String[] anomalies = new String[temp.length];
I get this error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4429
at NameApp.checkAnomalies(NameApp.java:502)
at NameApp.writeAnomaliesToFile(NameApp.java:259)
at NameApp.main(NameApp.java:43)
I figure my error is in my loops but I can't figure out where.. I figured my loops are finding way more errors then their really is.. Just wondering if anyone can help me out with this.. It is really the only part that I need left. Thanks in advance
EDIT:
This is the relevant code for what I need help with:
private static void writeAnomaliesToFile(Name[] list){
System.out.println("Terminating... but first the anomalies in the data file:");
checkAnomalies(list);
System.out.println("Anomalies written to anomalies.txt.");
}
method that is called:
private static void checkAnomalies(Name[] list){
Name[] temp = new Name[list.length];
int anomalyCount = 0;
int popTwo = 0;
int popOne = 0;
String[] anomalies = new String[list.length];
for (int i = 0; i < list.length; i++){
temp[i] = list[i];
}
for (int decade = 0; decade < 11; decade++){
for (int index = 0; index < temp.length; index++){
int smallestIndex = getIndexOfSmallest(decade, index, temp);
interchange(decade, index, smallestIndex, temp);
}
int rank = 0;
for (int i = 1; i < temp.length - 1; i += 2){
popOne = temp[i].getPop(decade);
popTwo = temp[i+1].getPop(decade);
if (popOne != 0){
rank++;
}
if (popOne == rank && popTwo != rank){
String decadeYear = decadeYears(decade);
anomalies[anomalyCount] = "One name (" + temp[i].getName() + ") for " + decadeYear + ", rank " + temp[i].getPop(decade) + ".";
anomalyCount++;
}
else if (popOne != rank && popTwo == rank){
String decadeYear = decadeYears(decade);
anomalies[anomalyCount] = "One name (" + temp[i+1].getName() + ") for " + decadeYear + ", rank " + temp[i+1].getPop(decade) + ".";
anomalyCount++;
}
else if (popOne != rank && popTwo != rank){
String decadeYear = decadeYears(decade);
anomalies[anomalyCount] = "No names for " + decadeYear + ", rank " + temp[i].getPop(decade) + ".";
anomalyCount++;
}
}
}
}
Solved myself just had to sleep on it:
private static void checkAnomalies(Name[] list)
throws FileNotFoundException{
Name[] temp = new Name[list.length];
String[] anomalies = new String[1065];
int anomalyCount = 0;
for (int i = 0; i < list.length; i++){
temp[i] = list[i];
}
for (int decade = 0; decade < 11; decade++){
for (int ranks = 1; ranks < 1000; ranks++){
int found = 0;
String tempName = "";
String decadeYear = decadeYears(decade);
for (int i = 0; i < temp.length; i++){
if (temp[i].getPop(decade) == ranks){
found++;
}
if (found == 1 &&
temp[i].getPop(decade) == ranks){
tempName = temp[i].getName();
}
}
if (found == 0){
anomalies[anomalyCount] = "No names "
+ "for " + decadeYear + ", rank "
+ ranks + ".";
anomalyCount++;
}
else if (found == 1){
anomalies[anomalyCount] = "One "
+ "Name (" + tempName + ") for "
+ decadeYear + ", rank "
+ ranks + ".";
anomalyCount++;
}
}
}
writeAnomaliesFile(anomalies);
}
/*
* Helper method: this method will create and write
* each anomalies to a text document called
* anomalies.txt.
*/
private static void
writeAnomaliesFile(String[] anomalies)
throws FileNotFoundException{
String fileName = "anomolies.txt";
PrintWriter outputStream = null;
outputStream = new PrintWriter(fileName);
for (int i = 0; i < anomalies.length; i++){
outputStream.println(anomalies[i]);
}
outputStream.close();
}
Related
From this program I am creating an array and finding the total number of letter characters and finding a percentages of the total letters from A-D. I created a method that enables me to find the percentage of a character appears in a sequence.
For example for an array of AABD the character A would be 50% of the array. I have tested this method out and it mostly works until I try the character D in which I inexplicably get a 26 instead of a 30% which confuses me.
The data I am using is from this data text is this down below which ignores letters not between A-D
I am trying to print out a percentage of 30 from the method but I keep getting 26 and I am unsure why
Example #1:
AAAAABBBBBCCCCCDDDDD
AAEBCBAFBBCDCECDADDEFEEFFF
Example #2:
AAATAABTBBBBCCCCTCDDTDDD
AASAABBSBBCCSCCDSDDDEEEAEEFBFFFDDF
My Code:
import java.io.*;
import java.util.*;
public class findSequence {
public static int findPercentages(int perNum, int totalNum) {
int percent = (int) Math.round(perNum * 100.0 / totalNum);
return percent;
}
public static void main(String[] args) {
File inputFile = new File("ShortData.txt");
File outputFile = new File("newDNAOutput");
PrintStream outputStream = null;
Scanner fileScan = null;
try {
fileScan = new Scanner(inputFile);
outputStream = new PrintStream(outputFile);
} catch(Exception e) {
System.out.println("File not found " + e);
System.out.println("Exiting program");
System.exit(1);
}
int numA = 0;
int numB = 0;
int numC = 0;
int numD = 0;
while(fileScan.hasNextLine()) {
String theLine = fileScan.nextLine();
Scanner lineScan = new Scanner(theLine);
if(theLine.length() > 10) {
theLine = theLine.toUpperCase();
char[] dnaArray = new char[theLine.length()];
for(int j = 0; j < dnaArray.length; j++) {
dnaArray[j] = theLine.charAt(j);
}
Arrays.sort(dnaArray);
for(int i = 0; i < dnaArray.length; i++) {
if(dnaArray[i] == 'A') {
numA = numA + 1;
}
if(dnaArray[i] == 'B') {
numB = numB + 1;
}
if(dnaArray[i] == 'C') {
numC = numC + 1;
}
if(dnaArray[i] == 'D') {
numD = numD + 1;
}
}
int totalSum = numA + numB + numC + numD;
int numAPer = findPercentages(numA, totalSum);
int numBPer = findPercentages(numB, totalSum);
int numCPer = findPercentages(numC, totalSum);
int numDPer = findPercentages(numD, totalSum);
outputStream.println(numDPer);
}
}
}
}
You have 20 Ds in 76 characters (excluding the two as in the words Example #1 and Example #2). That's a percentage of: 26.31578947368421, roughly 26%. If you want to go for the 30% you should have a total of around 66 characters.
To have the percentage of each line you just need to move the declarations of the letter count inside the while loop:
while(fileScan.hasNextLine()) {
int numA = 0;
int numB = 0;
int numC = 0;
int numD = 0;
String theLine = fileScan.nextLine();
if(theLine.length() > 10) {
theLine = theLine.toUpperCase();
char[] dnaArray = new char[theLine.length()];
for(int j = 0; j < dnaArray.length; j++) {
dnaArray[j] = theLine.charAt(j);
}
Arrays.sort(dnaArray);
for (char c : dnaArray) {
switch (c) {
case 'A':
numA += 1;
break;
case 'B':
numB += 1;
break;
case 'C':
numC += 1;
break;
case 'D':
numD += 1;
break;
}
}
int totalSum = numA + numB + numC + numD;
int numAPer = findPercentages(numA, totalSum);
int numBPer = findPercentages(numB, totalSum);
int numCPer = findPercentages(numC, totalSum);
int numDPer = findPercentages(numD, totalSum);
outputStream.println(numDPer);
}
}
This will clear the values each time the loop runs.
For this java program, I'm trying to use a binary search for the array I have created in the Class Numbers, however, when I enter the 4th choice, the program just ends. The method binSearch is in Class Numbers. I can enter the required the size of the array, generate random numbers, search for specific numbers, and display them. However, when I want to use a binary search, the program ends as previously said. What is the reason why the program ends and what needs to be done to fix that certain method?
public class Numbers {
int[] array;
private int sizeOfArray;
public Numbers() {
sizeOfArray = 0;
array= new int [sizeOfArray];
}
public Numbers(int sizeOfArray) {
this.sizeOfArray = sizeOfArray;
array= new int [sizeOfArray];
}
public void generateNumbers() {
Random randomNumber = new Random();
int theNumber = 0;
for (int i = 0; i < sizeOfArray; i++) {
theNumber = randomNumber.nextInt(50);
array[i] = theNumber;
}
}
public int count(int num) {
int theNumbers = 0;
for (int i = 0; i < sizeOfArray; i++) {
if (array[i] == num) {
theNumbers++;
}
}
return theNumbers;
} // end count method
public String toString() {
String myArray = "";
for (int i = 0; i < sizeOfArray; i++) {
myArray += array[i] + " ";
}
return myArray;
}
public int binSearch(int[] array, int key) {
int low = 0;
int high = sizeOfArray - 1;
//int middle = (low + high + 1) /2;
int location = -1;
while (high >= low) {
int middle1 = (low + high) / 2;
if (array[middle1] == key ) {
//return true;
}
if (array[middle1] < key) {
low = middle1 + 1;
}
if (array[middle1] > key) {
high = middle1 - 1;
}
}
//return false;
return location;
}
}
and here is the main menu:
boolean isDone = false;
String input = null;
Numbers theNumber = new Numbers();
Scanner scanner = new Scanner (System.in);
try {
while (isDone == false) {
/*Menu options */
System.out.println("Enter 1 to create array size");
System.out.println("Enter 2 to generate random numbers");
System.out.println("Enter 3 to search and display number of occurrences");
System.out.println("Enter 4 to binary search to find whether specific number exists");
System.out.println("Enter 5 to display the array");
System.out.println("Enter 6 to quit the program");
input = scanner.nextLine();
switch (input) {
case "1":
int intNumber1 = 0;
System.out.println("Enter required size:");
intNumber1 = Integer.valueOf(scanner.nextLine());
theNumber = new Numbers(intNumber1);
System.out.println("Array has been generated.");
break;
case "2":
//theNumber = new Numbers();
theNumber.generateNumbers();
System.out.println("Numbers have been generated and stored.");
break;
case "3":
int intNumber2 = 0;
System.out.println("Enter number to search for: ");
intNumber2 = Integer.valueOf(scanner.nextLine());
System.out.println("Number of occurences of " + intNumber2 + " in the array is " + theNumber.count(intNumber2) + ".");
break;
case "4":
int key = 0;
theNumber.binSearch(null, key);
System.out.println("Array is sorted: ");
break;
case "5":
int theNumbers = 0;
if (theNumbers == 0)
{
System.out.println("No array has not been generated yet.");
}
else
{
System.out.println("The numbers are: ");
}
System.out.println(theNumber.toString());
break;
case "6":
isDone = true;
System.out.println("Bye... See you again");
scanner.close();
break;
default:
System.out.println("These are invalid choices...please reenter.");
break;
}
}
}
catch (Exception exception)
{
System.out.println("This is an invalid choice...please reenter.");
scanner.nextLine();
return;
}
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Hangman {
public static void main(String[] args) {
String playAgainMsg = "Would you like to play again?";
String pickCategoryMsg = "You've tried all the words in this category!\nWould you like to choose another category?";
int winCounter = 0, loseCounter = 0, score = 0;
String[] words;
int attempts = 0;
String wordToGuess;
boolean playCategory = true, playGame = true;
int totalCounter = 0, counter;
while (playCategory && playGame)
{
while (playCategory && playGame) {
words = getWords();
counter = 0;
while (playGame && counter < words.length) {
wordToGuess = words[counter++];
if (playHangman(wordToGuess)) {
winCounter++;
System.out.println("You win! You have won " + winCounter + " game(s)." + " You have lost " + loseCounter + " game(s).");
} else {
loseCounter++;
System.out.println("You lose! You have lost " + loseCounter + " game(s)." + " You have won " + winCounter + " game(s).");
}
if (counter < words.length) playGame = askYesNoQuestion(playAgainMsg);
}
if (playGame) playCategory = askYesNoQuestion(pickCategoryMsg);
}
}
}
public static boolean playHangman(String wordToGuess) {
String[] computerWord = new String[wordToGuess.length()];
String[] wordWithDashes = new String[wordToGuess.length()];
for (int i = 0; i < computerWord.length; i++) {
computerWord[i] = wordToGuess.substring(i, i+1);
wordWithDashes[i] = "_";
}
Scanner in = new Scanner(System.in);
int attempts = 0, maxAttempts = 7;
boolean won = false;
int points = 0;
while (attempts < maxAttempts && !won) {
String displayWord = "";
for (String s : wordWithDashes) displayWord += " " + s;
System.out.println("\nWord is:" + displayWord);
System.out.print("\nEnter a letter or guess the whole word: ");
String guess = in.nextLine().toLowerCase();
if (guess.length() > 1 && guess.equals(wordToGuess)) {
won = true;
} else if (wordToGuess.indexOf(guess) != -1) {
boolean dashes = false;
for (int i = 0; i < computerWord.length; i++) {
if (computerWord[i].equals(guess)) wordWithDashes[i] = guess;
else if (wordWithDashes[i].equals("_")) dashes = true;
}
won = !dashes; // If there are no dashes left, the whole word has been guessed
} else {
drawHangmanDiagram(attempts);
System.out.println("You've used " + ++attempts + " out of " + maxAttempts + " attempts.");
}
}
int score = 0;
score = scoreGame(attempts);
System.out.println("Your score is: " + score);
return won;
}
//should take in a failure int from the main method that increments after every failed attempt
public static void drawHangmanDiagram(int failure)
{
if (failure == 0)
System.out.println("\t+--+\n\t| |\n\t|\n\t|\n\t|\n\t|\n\t|\n\t|\n\t+--");
else if (failure == 1)
System.out.println("\t+--+\n\t| |\n\t| #\n\t|\n\t|\n\t|\n\t|\n\t|\n\t+--");
else if (failure == 2)
System.out.println("\t+--+\n\t| |\n\t| #\n\t| /\n\t|\n\t|\n\t|\n\t|\n\t+--");
else if (failure == 3)
System.out.println("\t+--+\n\t| |\n\t| #\n\t| / \\\n\t|\n\t|\n\t|\n\t|\n\t+--");
else if (failure == 4)
System.out.println("\t+--+\n\t| |\n\t| #\n\t| /|\\\n\t| |\n\t|\n\t|\n\t|\n\t+--");
else if (failure == 5)
System.out.println("\t+--+\n\t| |\n\t| #\n\t| /|\\\n\t| |\n\t| /\n\t|\n\t|\n\t+--");
else if (failure == 6)
System.out.println("\t+--+\n\t| |\n\t| #\n\t| /|\\\n\t| |\n\t| / \\\n\t|\n\t|\n\t+--");
}
// Asks user a yes/no question, ensures valid input
public static boolean askYesNoQuestion(String message) {
Scanner in = new Scanner(System.in);
boolean validAnswer = false;
String answer;
do {
System.out.println(message + " (Y/N)");
answer = in.nextLine().toLowerCase();
if (answer.matches("[yn]")) validAnswer = true;
else System.out.println("Invalid input! Enter 'Y' or 'N'.");
} while (!validAnswer);
return answer.equals("y");
}
public static boolean askForCategory(int category) {
Scanner in = new Scanner(System.in);
boolean validAnswer = false;
String answer;
do {
System.out.println("\nWould you like to play again? (Y/N)");
answer = in.nextLine().toLowerCase();
if (answer.matches("[yn]")) validAnswer = true;
else System.out.println("Invalid input! Enter 'Y' or 'N'.");
} while (!validAnswer);
return answer.equals("y");
}
// Asks the user to pick a category
public static String[] getWords() {
String[] programming = {"java", "pascal", "python", "javascript", "fortran", "cobol"};
String[] sports = {"gymnastics", "badminton", "athletics", "soccer", "curling", "snooker", "hurling", "gaelic", "football", "darts"};
String[] result = {""};
Scanner in = new Scanner(System.in);
boolean validAnswer = false;
String answer;
do {
System.out.println("Pick a category:\n1. Programming\n2. Sports");
answer = in.nextLine().toLowerCase();
if (answer.matches("[1-2]")) validAnswer = true;
else System.out.println("Invalid input! Enter the number of the category you want.");
} while (!validAnswer);
int selection = Integer.parseInt(answer);
switch (selection) {
case 1: result = randomOrder(programming); break;
case 2: result = randomOrder(sports); break;
}
return result;
}
// Sorts a String array in random order
public static String[] randomOrder(String[] array) {
int[] order = uniqueRandoms(array.length);
String[] result = new String[array.length];
for (int i = 0; i < order.length; i++) {
result[i] = array[order[i]];
}
return result;
}
// Generates an array of n random numbers from 0 to n-1
public static int[] uniqueRandoms(int n) {
int[] array = new int[n];
int random, duplicateIndex;
for (int i = 0; i < n; ) {
random = (int) (Math.random() * n);
array[i] = random;
for (duplicateIndex = 0; array[duplicateIndex] != random; duplicateIndex++);
if (duplicateIndex == i) i++;
}
return array;
}
public static int scoreGame(int attempts)
{
int score = 0;
switch (attempts)
{
case 0: score = 70; break;
case 1: score = 60; break;
case 2: score = 50; break;
case 3: score = 40; break;
case 4: score = 30; break;
case 5: score = 20; break;
case 6: score = 10; break;
case 7: score = 0; break;
}
return score;
}
}
I have got it working so that it keeps count of the games won and lost, as well as assigning a score based on the amount of attempts/lives saved but I haven't been able to find a way to get it to keep a total score for all of the games played. Each game unfortunately has a seperate score. If anyone can advise me on a way of doing this, it would be greatly appreciated.
Create an int totalScore variable where winCounter, loseCounter and score are defined. Then increment it after each call to scoreGame()
score = scoreGame(attempts);
totalScore += score;
System.out.println("Your score is: " + score);
If you want to permanently save statistics between sessions then it's a whole nother story. You would need to write your scores to a file after each round and then start your program by reading this score file. It's hardly impossible, but requires a bit more code.
So my code works completely; the only thing now is a very simple problem that I cannot figure out. I have to make the program loop back to the beginning if the user type restart and I cannot think how to that.
import java.util.Scanner;
public class MutantGerbil {
public static String []foodName;
public static int [] maxAmount;
public static Gerbil [] gerbilAttributes;
public static int [] consumption;
public static void main(String [] args){
Scanner keyboard = new Scanner(System.in);
String userInput1 = keyboard.nextLine();
int food = Integer.parseInt(userInput1);
foodName = new String[food]; //array of food names
maxAmount = new int[food]; // array of max amount of food given everyday
for (int i = 0; i < food; i++){
System.out.println("Name of food item" + (i+1) +": " );
String foodType = keyboard.nextLine();
foodName[i] = foodType;
System.out.println("Maximum consumed per gerbil: ");
String consumption = keyboard.nextLine();
int consumption2 = Integer.parseInt(consumption);
maxAmount [i] = consumption2;
}
System.out.println("How many gerbils are in the lab?");
String userInput2 = keyboard.nextLine();
int numberOfGerbils = Integer.parseInt(userInput2);
gerbilAttributes = new Gerbil [numberOfGerbils];// Array with the gerbil attributes
for (int i = 0; i < numberOfGerbils; i++)
{
consumption = new int[food]; // Array with amount of the food the gerbil eat each day
System.out.println("Gerbil" + (i+1) + "'s" + "lab ID: ");
String gerbilID = keyboard.nextLine();
System.out.println("What name did the students give to "+ gerbilID);
String gerbilName = keyboard.nextLine();
for (int j = 0; j < food; j++)
{
System.out.println(gerbilID + " eats how many " + foodName[j] + " per day?");
String gerbilComsumption = keyboard.nextLine();
int gerbilFood = Integer.parseInt(gerbilComsumption);
if (gerbilFood <= maxAmount[j])
{
consumption[j] = gerbilFood;
}
}
System.out.println("Does " + gerbilID + " bite?");
boolean biter = keyboard.nextBoolean();
System.out.println("Does " + gerbilID + " try to escape?");
boolean flightRisk = keyboard.nextBoolean();
keyboard.nextLine();
Gerbil temp = new Gerbil (gerbilID, gerbilName, consumption, biter, flightRisk);
gerbilAttributes [i] = temp;
}
boolean end1 = false;
while (! end1){
System.out.println("What information would you like to know");
sortGerbilArray (gerbilAttributes);
String userInput3 = keyboard.nextLine();
if (userInput3.equalsIgnoreCase("average"))
{
String average1 = foodAverage();
System.out.println(average1);
}
if (userInput3.equalsIgnoreCase("search"))
{
System.out.println("Enter a Gerbil ID to search for");
userInput3 = keyboard.nextLine();
Gerbil findGerbil = searchForGerbil(userInput3);
if (findGerbil == null)
{
System.out.println("Error");
}
else {
String h = ("Name: " + findGerbil.getName());
if (findGerbil.getAttacker())
{
h+= " (will bite, ";
}
else
{
h+= " (will not bite, ";
}
if (findGerbil.getFilghtRisk())
{
h+= "will run away), ";
}
else
{
h+= "will not run away), ";
}
h+= "Food:";
for (int i = 0; i < foodName.length; i++){
h+=" " + foodName[i] + "-";
h+= findGerbil.getConsumption2()+"/"+ maxAmount[i];
if (i < foodName.length-1)
{
h+=",";
}
System.out.println(h);
}
}
}
if (userInput3.equalsIgnoreCase("restart")){
}
if (userInput3.equalsIgnoreCase("quit"))
{
System.out.println("Good-bye!");
break;
}
}
}
private static void sortGerbilArray(Gerbil[]sortGerbil){
for (int i = 0; i < sortGerbil.length; i++){
for (int j = 0; j < sortGerbil.length - i - 1; j++){
if(sortGerbil[j].getID().compareToIgnoreCase(sortGerbil[j+1].getID())>0){
Gerbil t = sortGerbil[j];
sortGerbil[j] = sortGerbil[j+1];
sortGerbil[j+1] = t;
}
}
}
}
private static String foodAverage()
{
double average = 0.0;
double totalMaxAmount = 0;
double totalConsumption = 0;
String averageFood = "";
for (int i = 0 ; i < gerbilAttributes.length;i++)
{
for(int j = 0; j < maxAmount.length; j++)
{
totalMaxAmount += maxAmount[j];
}
totalConsumption += gerbilAttributes[i].getConsumption();
average = totalConsumption/totalMaxAmount*100;
averageFood += gerbilAttributes[i].getID() + "(" + gerbilAttributes[i].getName() + ")" + Math.round(average) + "%\n";
totalMaxAmount = 0;
totalConsumption = 0;
}
return averageFood;
}
private static Gerbil searchForGerbil (String x) {
for (Gerbil l: gerbilAttributes){
if (l.getID().equals(x)){
return l;
}
}
return null;
}
}
there are two solutions.
1. wrap your code in "while" loop. but it will be difficult for you because your code is so messy :(
change the code as follow
import java.util.Scanner; public class MutantGerbil {
public static String []foodName;
public static int [] maxAmount;
public static Gerbil [] gerbilAttributes;
public static int [] consumption;
public static void main(String [] args){
Scanner keyboard = new Scanner(System.in);
String userInput1 = keyboard.nextLine();
int food = Integer.parseInt(userInput1);
foodName = new String[food]; //array of food names
maxAmount = new int[food]; // array of max amount of food given everyday
for (int i = 0; i < food; i++){
System.out.println("Name of food item" + (i+1) +": " );
String foodType = keyboard.nextLine();
foodName[i] = foodType;
System.out.println("Maximum consumed per gerbil: ");
String consumption = keyboard.nextLine();
int consumption2 = Integer.parseInt(consumption);
maxAmount [i] = consumption2;
}
System.out.println("How many gerbils are in the lab?");
String userInput2 = keyboard.nextLine();
int numberOfGerbils = Integer.parseInt(userInput2);
gerbilAttributes = new Gerbil [numberOfGerbils];// Array with the gerbil attributes
for (int i = 0; i < numberOfGerbils; i++)
{
consumption = new int[food]; // Array with amount of the food the gerbil eat each day
System.out.println("Gerbil" + (i+1) + "'s" + "lab ID: ");
String gerbilID = keyboard.nextLine();
System.out.println("What name did the students give to "+ gerbilID);
String gerbilName = keyboard.nextLine();
for (int j = 0; j < food; j++)
{
System.out.println(gerbilID + " eats how many " + foodName[j] + " per day?");
String gerbilComsumption = keyboard.nextLine();
int gerbilFood = Integer.parseInt(gerbilComsumption);
if (gerbilFood <= maxAmount[j])
{
consumption[j] = gerbilFood;
}
}
System.out.println("Does " + gerbilID + " bite?");
boolean biter = keyboard.nextBoolean();
System.out.println("Does " + gerbilID + " try to escape?");
boolean flightRisk = keyboard.nextBoolean();
keyboard.nextLine();
Gerbil temp = new Gerbil (gerbilID, gerbilName, consumption, biter, flightRisk);
gerbilAttributes [i] = temp;
}
boolean end1 = false;
while (! end1){
System.out.println("What information would you like to know");
sortGerbilArray (gerbilAttributes);
String userInput3 = keyboard.nextLine();
if (userInput3.equalsIgnoreCase("average"))
{
String average1 = foodAverage();
System.out.println(average1);
}
if (userInput3.equalsIgnoreCase("search"))
{
System.out.println("Enter a Gerbil ID to search for");
userInput3 = keyboard.nextLine();
Gerbil findGerbil = searchForGerbil(userInput3);
if (findGerbil == null)
{
System.out.println("Error");
}
else {
String h = ("Name: " + findGerbil.getName());
if (findGerbil.getAttacker())
{
h+= " (will bite, ";
}
else
{
h+= " (will not bite, ";
}
if (findGerbil.getFilghtRisk())
{
h+= "will run away), ";
}
else
{
h+= "will not run away), ";
}
h+= "Food:";
for (int i = 0; i < foodName.length; i++){
h+=" " + foodName[i] + "-";
h+= findGerbil.getConsumption2()+"/"+ maxAmount[i];
if (i < foodName.length-1)
{
h+=",";
}
System.out.println(h);
}
}
}
if (userInput3.equalsIgnoreCase("restart")){
main();
}
if (userInput3.equalsIgnoreCase("quit"))
{
System.out.println("Good-bye!");
break;
}
}
}
private static void sortGerbilArray(Gerbil[]sortGerbil){
for (int i = 0; i < sortGerbil.length; i++){
for (int j = 0; j < sortGerbil.length - i - 1; j++){
if(sortGerbil[j].getID().compareToIgnoreCase(sortGerbil[j+1].getID())>0){
Gerbil t = sortGerbil[j];
sortGerbil[j] = sortGerbil[j+1];
sortGerbil[j+1] = t;
}
}
}
}
private static String foodAverage()
{
double average = 0.0;
double totalMaxAmount = 0;
double totalConsumption = 0;
String averageFood = "";
for (int i = 0 ; i < gerbilAttributes.length;i++)
{
for(int j = 0; j < maxAmount.length; j++)
{
totalMaxAmount += maxAmount[j];
}
totalConsumption += gerbilAttributes[i].getConsumption();
average = totalConsumption/totalMaxAmount*100;
averageFood += gerbilAttributes[i].getID() + "(" + gerbilAttributes[i].getName() + ")" + Math.round(average) + "%\n";
totalMaxAmount = 0;
totalConsumption = 0;
}
return averageFood;
}
private static Gerbil searchForGerbil (String x) {
for (Gerbil l: gerbilAttributes){
if (l.getID().equals(x)){
return l;
}
}
return null;
} }
Currently, I'm trying to read in a .dat file and assign various lines into an array. The file will provide items like "a100" and "q80" which I will have to separate into categories by letter and then have different grades as an array for each category. Right now, this is what I have, but I'm getting a lot of run-time errors when I try various things. Is there something I'm missing here?
Some of the errors I'm having:
When I execute case 'P', it prints this out: WeightedGrades#13105f32
When I try to execute cases C, A or D, this happens: Exception in thread "main" java.lang.NoSuchMethodError: WeightedGrades.deleteGrade(Ljava/lang/String;)Z
WeightedGrades class:
public class WeightedGrades {
private String name;
private int numGrades;
private String[] grades;
public static final double ACTV_WT = 0.05, QUIZ_WT = 0.10, PROJ_WT = 0.25, EXAM_WT = 0.30, FINAL_EXAM_WT = 0.30;
public WeightedGrades(String nameIn, int numGradesIn, String[] gradesIn) {
name = nameIn;
numGrades = numGradesIn;
grades = gradesIn;
}
public String getName() {
return name;
}
public int getNumGrades() {
return numGrades;
}
public String[] getGrades() {
return grades;
}
public double[] gradesByCategory(char categoryChar) {
int count = 0;
for (int i = 0; i < grades.length; i++) {
if (categoryChar == grades[i].charAt(0)) {
count++;
}
}
double[] gradesNew = new double[count];
count = 0;
for( int i = 0; i < numGrades; i++) {
if (categoryChar == grades[i].charAt(0)) {
gradesNew[count] = Double.parseDouble(grades[i].substring(1));
count++;
}
}
return gradesNew;
}
public String toString() {
String result = "\tStudent Name: " + getName()
+ "\n\tActivities: " + gradesByCategory('A')
+ "\n\tQuizzes: " + gradesByCategory('Q')
+ "\n\tProjects: " + gradesByCategory('P')
+ "\n\tExams: " + gradesByCategory('E')
+ "\n\tFinal Exam: " + gradesByCategory('F')
+ "\n\tCourse Average: " + courseAvg();
return result;
}
public void addGrade(String newGrade) {
if (numGrades >= grades.length) {
increaseGradesCapacity();
}
grades[numGrades] = newGrade;
numGrades++;
}
public boolean deleteGrade(String gradeDelete) {
boolean delete = false;
int deleteIndex = -1;
for (int i = 0; i < numGrades; i++) {
if (gradeDelete.charAt(0) == grades[i].charAt(0) &&
Double.parseDouble(gradeDelete.substring(1))
== Double.parseDouble(grades[i].substring(1))) {
deleteIndex = i;
}
}
if (deleteIndex > -1) {
for (int i = deleteIndex; i < numGrades - 1; i++) {
grades[i] = grades[i + 1];
}
grades[numGrades - 1] = "";
numGrades--;
return true;
}
else {
return false;
}
}
public void increaseGradesCapacity() {
String[] temporary = new String[grades.length + 1];
for (int i = 0; i < grades.length; i++) {
temporary[i] = grades[i];
}
grades = temporary;
}
public double average(double[] newArray) {
if (newArray.length == 0) {
return 0.0;
}
double sum = 0;
double average = 0;
for ( int i = 0; i < newArray.length; i++) {
sum += newArray[i];
average = sum / newArray.length;
}
return average;
}
public double courseAvg() {
double actvAvg = 0.0;
double quizAvg = 0.0;
double projAvg = 0.0;
double examAvg = 0.0;
double finalAvg = 0.0;
double avg = 0.0;
if (!numGrades.length == 0) {
avg = actvAvg * ACTV_WT + quizAvg * QUIZ_WT + projAvg * PROJ_WT + examAvg * EXAM_WT + finalAvg * FINAL_EXAM_WT;
}
return avg;
}
}
Second class
import java.util.Scanner;
import java.io.IOException;
public class WeightedGradesApp {
public static void main(String[] args) throws IOException {
String name = "";
int numGrades = 0;
String[] grades = new String[13];
String code = "";
String gradeAdd = "";
String gradeDelete = "";
String categoryIn = "";
WeightedGrades student = new WeightedGrades(name, numGrades, grades);
Scanner userInput = new Scanner(System.in);
if (args == null) {
System.out.println("File name was expected as a run argument.");
System.out.println("Program ending.");
return;
}
else {
System.out.println("File read in and WeightedGrades object created.");
System.out.println("");
System.out.println("Player App Menu");
System.out.println("P - Print Report");
System.out.println("C - Print Category");
System.out.println("A - Add Grade");
System.out.println("D - Delete Grade");
System.out.println("Q - Quit ");
do {
System.out.print("Enter Code [P, C, A, D, or Q]: ");
code = userInput.nextLine();
if (code.length() == 0) {
continue;
}
code = code.toUpperCase();
char codeChar = code.charAt(0);
switch (codeChar) {
case 'P':
System.out.println(student.toString());
break;
case 'C':
System.out.print(" Category: ");
categoryIn = userInput.nextLine();
char categoryChar = categoryIn.charAt(0);
System.out.println(student.gradesByCategory(categoryChar));
break;
case 'A':
System.out.print(" Grade to add: ");
gradeAdd = userInput.nextLine();
student.addGrade(gradeAdd);
break;
case 'D':
System.out.print(" Grade to delete: ");
gradeDelete = userInput.nextLine();
boolean isDeleted = student.deleteGrade(gradeDelete);
if (isDeleted) {
System.out.println(" Grade deleted");
}
else {
System.out.println(" Grade not found");
}
break;
case 'Q':
break;
default:
}
} while (!code.equalsIgnoreCase("Q"));
}
}
}
For starters your code as is doesn't compile due to the line
if (!numGrades.length == 0) {
This is because numGrades is an int it is a primative type and therefore does not have any property length. I'm assuming what you want here is
if (numGrades != 0) {
Also as I mentioned you are not dealing with reading in the file, you supply the file name but never actually read it, I suggest you look at the Java tutorial on File IO
On this note you do the check args == null this will not check that no args are supplied, try it. what you want is args.length == 0
On your other error I have no idea how you even produced that... I'm assuming it is using an older compiled version of the class where the methods have not being written.