How do I output a draw from a vote (In Java)? - java

I have no idea on how to output a draw from a vote any help on with this would be appreciated.
At the moment it will allow a user to select the amount of candidates and then the user inputs details. Then the user will enter a vote, which is the candidate number, and type 999 to finish. The output will be the winner or winners(draw)the candidates with details and votes and the amount of spolit votes that is votes not in the range declared at the start.
int x;
char highestChar = '1';
char nextHighestChar = '1';
String alpha = "123456";
int largest=intVoteCount[1];
int nextLargest=intVoteCount[1];
for( x=1; x<=range; x++){
if(intVoteCount[x]>largest){
largest = intVoteCount[x];
highestChar = alpha.charAt(intLoopCount);
}
if(intVoteCount[x]>highestChar){
nextLargest = intVoteCount[x];
nextHighestChar = alpha.charAt(intLoopCount);
}
}
System.out.println("The winner is Candidate number "+ highestChar + " with " + largest + " votes.");
System.out.println("The winner is Candidate number "+ nextHighestChar + " with " + nextLargest + " votes.");
System.out.println("-----------------------------");
System.out.println("The Candidate votes are as follows.");
for (intLoopCount = 1; intLoopCount <= range; intLoopCount++) {
// Display all records.
// New Instance
System.out.println("");
System.out.println("Candidate " + intLoopCount + " "
+ strCandidateTitle[intLoopCount] + " "
+ strCandidateFirstname[intLoopCount] + " "
+ strCandidateSurname[intLoopCount] + " votes "
+ intVoteCount[intLoopCount]);
}
System.out.println("-----------------------------");
System.out.println("Vote Count Spolit: " + intVoteCountSpolit);
}
}

If largest == nextLargest you have a draw, so print out an appropriate message saying so; otherwise, you have an explicit winner.

Related

Java error handling for project- Line matched except underscores: Invalid Event Code_?

I completed this java project for a class, but I cannot seem to fix the error I'm getting for the web software that grades it- it's called webcat. I've tried the test input the software suggests for a reference test against my solution, and my output looks exactly the same, but I still lost points for this error-
"Error in method main of class Event: Line number 2 of your output is incorrect (line numbers begin at 1). Your main method does not print the correct output when the input is "This is a short test " (input less than 26 characters) [] Line matched except underscores: Invalid Event Code_".
How can I fix this error when the expected ouput looks fine? Thanks in advance!
Code:
public class Event {
/**
* accepts coded event info, prints the info back to std output, and
actual cost and prize number.
*
* #param args Command line arguments - not used.
*/
public static void main(String[] args) {
// variables needed
String shrink, event, date, time, section, row, seat;
double price, discount, cost;
int prizeNum;
// accept input
Scanner userInput = new Scanner(System.in);
// format the numbers
DecimalFormat formatNum = new DecimalFormat("$#,##0.00");
// enter input and trim the space
System.out.print("Enter your event code: ");
shrink = userInput.nextLine().trim();
if (shrink.length() < 26) {
System.out.println();
System.out.println("Invalid Event Code");
System.out.println("Event code must have at least 26 characters.");
return;
}
// locates spot in index of code and assigns
event = shrink.substring(25, shrink.length());
date = shrink.substring(0, 8);
time = shrink.substring(8, 12);
section = shrink.substring(19, 21);
row = shrink.substring(21, 23);
seat = shrink.substring(23, 25);
price = Double.parseDouble(shrink.substring(12, 15)
+ "." + shrink.substring(15, 17));
discount = Double.parseDouble(shrink.substring(17, 19));
// calculates final cost
cost = price - (price * (discount / 100));
// random final number
prizeNum = (int) (Math.random() * 1000 + 1);
// prints the data to std output
System.out.println();
System.out.print("Event: " + event + " " + " " + " ");
System.out.print("Date: " + date.substring(0, 2) + "/"
+ date.substring(2, 4) + "/" + date.substring(4, 8) + " "
+ " " + " ");
System.out.println("Time: " + time.substring(0, 2) + ":"
+ time.substring(2, 4) + " " + " " + " ");
System.out.print("Section: " + section + " " + " " + " ");
System.out.print("Row: " + row + " " + " " + " ");
System.out.println("Seat: " + seat);
System.out.print("Price: " + formatNum.format(price) + " " + " " + " ");
// formats discount before print
formatNum.applyPattern("#.#'%'");
System.out.print("Discount: " + formatNum.format(discount) + " "
+ " " + " ");
// formats cost before print
formatNum.applyPattern("$#,##0.00");
System.out.println("Cost: " + formatNum.format(cost));
System.out.print("Prize Number: " + prizeNum);
Output:
Enter your event code: This is a short test
Invalid Event Code
Event code must have at least 26 characters.

After asking the user for how many questions they want, how can I get my code to show that number of random problems?

I need to first ask the user to input how many problems they want to do. Then generate the first, then the second after they answer the first and so on.
public static void main(String[] args) {
int number1 = (int) (Math.random() * 40 + 10), number2 = (int) (Math.random() * 40 + 10), uanswer, ianswer, counter, icounter,
acounter, counter1, ui, aacounter, bcounter;
Scanner input = new Scanner(System.in);
System.out.println("How many problems do you want to do?");
ui = input.nextInt();
counter = 1;
icounter = 1;
acounter = counter + icounter;
{
System.out.print("What is " + number1 + " + " + number2 + "? ");
}
uanswer = input.nextInt();
ianswer = number1 + number2;
while (counter < 10000 && icounter < 1000 && acounter < 1000 && number1
+ number2 != uanswer) {
System.out.println("Incorrect, the answer is "
+ ianswer + ", " + icounter + " out of " + icounter + " incorrect. Try again?");
icounter++;
acounter++;
uanswer = input.nextInt();
}
if (ianswer == ianswer) {
aacounter = acounter - 1;
bcounter = icounter - 1;
System.out.println("Correct, the answer is " + ianswer
+ ", " + counter + " out of " + aacounter + " correct, "
+ bcounter + " out of " + aacounter + " incorrect.");
}
}
With my current code, I only see one problem, even though I asked for 2 or more problems at the beginning.
You need to add a loop around this statement:
System.out.print("What is " + number1 + " + " + number2 + "? ");
like:
for(int i=0; i<ui; i++){
System.out.print("What is " + number1 + " + " + number2 + "? ");
...

Sports schedule not showing string [duplicate]

This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 4 years ago.
It's a program that is a sports league, a group of teams plays through a Schedule of Games and determine the winner.
The program runs fine to the point where the final output won't let me see the winner, it shows this instead:
The season winner(s) with 9 points: [Ljava.lang.String;#e73f9ac
I changed it to teams.length which made the program work but it would show me the teams (i) number instead of the string name like "Vancouver".
Thanks in advance.
}
int peak = 0;
int[] total = new int[teams.length];
for (int i=0; i<teams.length; i++){
total[i] = 2*wins[i]+ties[i];
if (total[i] > peak) peak = total[i];
System.out.println(teams[i]+" - " + wins[i] + " wins, " + losses[i] + " losses, " + ties[i] + " ties = " + total[i]);
}
System.out.println("The season winner(s) with " + peak + " points: " + teams);
for (int i=0; i<teams.length; i++){
if (peak < total[i]) peak = total[i];
}
}
static int indexOfTeam(String team, String[] teams){
for (int i=0; i<teams.length; i++)
if (team.compareTo(teams[i]) == 0) return i;
return -1;
}
}
Instead of printing the winning team you're printing the teams array.
While iterating store besides the peak, the index of the winning team:
int index = -1;
for (int i=0; i<teams.length; i++){
total[i] = 2*wins[i]+ties[i];
if (total[i] > peak) {
index = i;
peak = total[i];
}
System.out.println(teams[i]+" - " + wins[i] + " wins, " + losses[i] + "
losses, " + ties[i] + " ties = " + total[i]);
}
and finally:
System.out.println("The season winner(s) with " + peak + " points: " +
teams[index]);
teams is an array of Strings, you must insert the index after the name
If you want to print all teams use Arrays.toString(teams), but I think that you would like to print only part of teams array, so you can create list of winners
List<String> winners = new ArrayList<Integer>;
for (int i=0; i<teams.length; i++){
total[i] = 2*wins[i]+ties[i];
if (total[i] > peak) {
winners.add(teams[i]);
peak = total[i];
}
System.out.println(teams[i]+" - " + wins[i] + " wins, " + losses[i] + "
losses, " + ties[i] + " ties = " + total[i]);
}
indexes object you can simply print
System.out.println("The season winner(s) with " + peak + " points: " +winners);
If you want to have an array you can use toArray() on winners object.

How do I pick an array position when pressing a key for example a number from 1 to 5?

When I run the program it shows me on the first values on the list which is the one with the [0] here is my array code:
KeyStroke[] clientsDetails = new KeyStroke[5];
clientsDetails[0] = new KeyStroke(9,"OX5BJM","Peter",2039489);
clientsDetails[1] = new KeyStroke(12,"OX1BOL","Kim",2434587);
clientsDetails[2] = new KeyStroke(67,"OX2VBN","Patrick",2233842);
clientsDetails[3] = new KeyStroke(34,"OX2XHB","Liam",2432340);
clientsDetails[4] = new KeyStroke(54,"OX3BUN","Bob",2234098);
And here is the code to support the array:
if(input.matches("S")){
System.out.println("Enter an array postion from 1 to 4 to show paitient's details");
number1 = enterNumber0.nextInt();
System.out.println("Name: " +clientsDetails[0].nameLable);
System.out.println("Age: " +clientsDetails[0].howOld);
System.out.println("Postcode: " +clientsDetails[0].postcode);
System.out.println("Phone Number: " + clientsDetails[0].cellPhoneNumber);
number2 = enterNumber1.nextInt();
System.out.println("Name: " +clientsDetails[1].nameLable);
System.out.println("Age: " +clientsDetails[1].howOld);
System.out.println("Postcode: " +clientsDetails[1].postcode);
System.out.println("Phone Number: " + clientsDetails[1].cellPhoneNumber);
number3 = enterNumber2.nextInt();
System.out.println("Name: " +clientsDetails[2].nameLable);
System.out.println("Age: " +clientsDetails[2].howOld);
System.out.println("Postcode: " +clientsDetails[2].postcode);
System.out.println("Phone Number: " + clientsDetails[2].cellPhoneNumber);
number4 = enterNumber1.nextInt();
System.out.println("Name: " +clientsDetails[3].nameLable);
System.out.println("Age: " +clientsDetails[3].howOld);
System.out.println("Postcode: " +clientsDetails[3].postcode);
System.out.println("Phone Number: " + clientsDetails[3].cellPhoneNumber);
}
Help will be greatly appreciated this is the last bit of my work that I'm stuck on. I have no teacher to advice this for me as to why I added a question on this forum I hope nobody gets offended.
Assuming enterNumber0 is a Scanner that is already created, we can do something like this:
// Populate the array
KeyStroke[] clientsDetails = new KeyStroke[]{
new KeyStroke(9,"OX5BJM","Peter",2039489),
new KeyStroke(12,"OX1BOL","Kim",2434587),
new KeyStroke(67,"OX2VBN","Patrick",2233842),
new KeyStroke(34,"OX2XHB","Liam",2432340),
new KeyStroke(54,"OX3BUN","Bob",2234098)
}
Not 100% sure what input does here, so I've kept this part. Also note that we fetch the ID from the user non-zero-indexed, so we have to subtract 1 to all the values. You also seem to have misunderstood how to use this value to return the correct client details. You use this value as the index in the array.
if (input.matches("S")) {
System.out.println("Enter an array postion from 1 to 5 to show paitient's details");
int number = enterNumber0.nextInt();
if (number >= 1 && number <= 5) {
System.out.println("Name: " +clientsDetails[number - 1].nameLable);
System.out.println("Age: " +clientsDetails[number - 1].howOld);
System.out.println("Postcode: " +clientsDetails[number - 1].postcode);
System.out.println("Phone Number: " + clientsDetails[number - 1].cellPhoneNumber);
}
}
You can use matches which can accept an integer from 1 to 5:
String number = enterNumber0.nextLine();
if(number.matches("[1-5]")){
int index = Integer.parseInt(input) - 1;
//use this index to get the value from your array
System.out.println("Name: " +clientsDetails[index ].nameLable);
System.out.println("Age: " +clientsDetails[index ].howOld);
System.out.println("Postcode: " +clientsDetails[index ].postcode);
System.out.println("Phone Number: " + clientsDetails[index ].cellPhoneNumber);
}
Note -1, because the array start from 0.

How do i keep track of correct answers?

public static void main(String[] args) {
Scanner Keyboard = new Scanner(System.in);
System.out.println("Enter your name: ");
String firstname =Keyboard.nextLine();
System.out.println("Welcome "+ firstname+ "!"+ " Please answer the following questions:");
int x = (int)(20 * Math.random()) + 1;
int y = (int)(20 * Math.random()) + 1;
int sum = (x+y);
System.out.println(x + " + " + y + " = ");
String sInput = Keyboard.nextLine();
int answer1 = Integer.parseInt(sInput);
if (answer1 ==sum){
System.out.println("Correct!");
}else{
System.out.println("Wrong!");
}
System.out.println("The correct answer is " +sum);
I have no clue on how to keep track of the correct answers. I need something to keep track of when it prints correct. I don't know what to do though. I know I just need to record the corrects and divide by four. Four because thats how many questions I have in my quiz.
If you just need to keep track of how many right answers were provided, just add an int variable starting with 0 as a value and increment it. If you want to keep track of the questions and answers which were right, create an empty ArrayList and add a string every time a correct answer is provided.
Here an example of the second option:
ArrayList<String> correctAnswers = new ArrayList<String>();
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter your name: ");
String firstname =keyboard.nextLine();
System.out.println("Welcome "+ firstname+ "!"+ " Please answer the following questions:");
for (int i=0;i<10;i++) {
int x = (int)(20 * Math.random()) + 1;
int y = (int)(20 * Math.random()) + 1;
int sum = (x+y);
System.out.println(x + " + " + y + " = ");
String sInput = keyboard.nextLine();
int answer1 = Integer.parseInt(sInput);
if (answer1 ==sum){
System.out.println("Correct!");
correctAnswers.add(x + " + " + y + " = " + sum);
}
else{
System.out.println("Wrong!");
System.out.println("The correct answer is " +sum);
}
}
System.out.println("Correct answers:");
for (String correctAnswer : correctAnswers) {
System.out.println(correctAnswer);
}
It asks 10 questions, keeps track of the right answers and outputs them after the 10th question.
An example for the first option:
int correctAnswers=0;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter your name: ");
String firstname =keyboard.nextLine();
System.out.println("Welcome "+ firstname+ "!"+ " Please answer the following questions:");
int totalAnswers=4;
for (int i=0;i<totalAnswers;i++) {
int x = (int)(20 * Math.random()) + 1;
int y = (int)(20 * Math.random()) + 1;
int sum = (x+y);
System.out.println(x + " + " + y + " = ");
String sInput = keyboard.nextLine();
int answer1 = Integer.parseInt(sInput);
if (answer1 ==sum){
System.out.println("Correct!");
correctAnswers++;
}
else{
System.out.println("Wrong!");
System.out.println("The correct answer is " +sum);
}
}
System.out.println("Correct answers: "+ correctAnswers + "("+correctAnswers*100/totalAnswers+"%)");

Categories

Resources