Why does my code throw a NoSuchElementException? - java

The Java program for my school work gives me an error:
Welcome to Surinam Airlines - We may get you there!
We have 6 seats available for flight JAVA123.
How many seats do you want to book? 3
Enter passenger #1's name: Taylor Swift
Enter passenger #1's food option
(V = vegetarian, N = no preference, H = Halal): V
Enter passenger #1's class (1 = first, 2 = business, 3 = economy): 1
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown source)
at Passenger.enterPassenger(Passenger.java:64)
at RunAirLine.main(RunAirLine.java:23)
It seems to have something to do with the object not being instantiated though I can't quite figure out why. Does anyone know what's wrong?
import java.util.*;
public class RunAirLine {
private static Passenger seatArray[][] = new Passenger[2][3]; // declares new
// array of
// Passengers
static Scanner kb = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Welcome to Surinam Airlines – We may get you there!");
System.out.println();
int seats = 6; // available seats
while (seats > 0) {
System.out.println("We have " + seats
+ " seats available for flight JAVA123.");
System.out.println();
System.out.print("How many seats do you want to book? ");
int n = kb.nextInt();
System.out.println();
for (int i = 0; i < n; i++) {
int x = emptySeat()[0];
int y = emptySeat()[1];
// retrieves info from emptySeat() function
seatArray[x][y] = new Passenger();
seatArray[x][y].enterPassenger(7 - seats); // gives the method the nth
// passenger number
seats--; // takes away 1 from the available seats
}
System.out.println();
}
System.out.println("No seats available.");
System.out.println();
System.out.println("Flight manifest:");
System.out.println();
for (int y = 0; y < 3; y++) { // prints out the list of flyers
for (int x = 0; x < 2; x++) {
System.out.println(seatArray[x][y].getSeat() + " "
+ seatArray[x][y].printPassengerCode());
}
}
kb.close();
}
public static int[] emptySeat() {
for (int y = 0; y < 3; y++) {// finds the next empty seat
for (int x = 0; x < 2; x++) {
if (seatArray[x][y] == null || seatArray[x][y].getSeat().equals("")) {
// if the spot hasn't been declared yet or is empty
return new int[] { x, y };
}
}
}
return null; // if none found then return null
}
}
Passenger.java
import java.util.*;
public class Passenger {
private String name;
private String seat;
private char foodOption;
private int seatClass;
// reduces some redundancy
private String classStr;
private String prefStr;
public Passenger() { // constructors
name = "";
seat = "";
foodOption = ' ';
seatClass = -1;
}
public Passenger(String n, String s, char f, int sc) { // assigns values
// according to input
name = n;
seat = s;
foodOption = f;
seatClass = sc;
}
// get and set methods
public void setName(String n) {
name = n;
}
public void setSeat(String s) {
seat = s;
}
public void setFoodOption(char f) {
foodOption = f;
}
public void setSeatClass(int sc) {
seatClass = sc;
}
public String getName() {
return name;
}
public String getSeat() {
return seat;
}
public char getFoodOption() {
return foodOption;
}
public int getSeatClass() {
return seatClass;
}
// end of get and set methods
public void enterPassenger(int i) {
Scanner kb = new Scanner(System.in);
// asks the important questions
System.out.print("Enter passenger #" + i + "’s name: ");
name = kb.nextLine();
System.out.println("Enter passenger #" + i + "’s food option ");
System.out.print("(V = vegetarian, N = no preference, H = Halal): ");
foodOption = kb.nextLine().toUpperCase().charAt(0);
System.out.print("Enter passenger #" + i
+ "’s class (1 = first, 2 = business, 3 = economy): ");
seatClass = kb.nextInt();
System.out.println();
char seatChar = 'A'; // calculates the seat number
if (i % 2 == 0)
seatChar = 'B';
seat = Integer.toString(i / 2 + i % 2) + seatChar;
classStr = "Economy Class"; // transfers the class choice int into string
switch (seatClass) {
case 1:
classStr = "First Class";
break;
case 2:
classStr = "Business Class";
break;
}
prefStr = "No preference"; // transfers the preference char into string
switch (foodOption) {
case 'V':
prefStr = "Vegetarian";
break;
case 'H':
prefStr = "Halal";
break;
}
System.out.println("Done – Seat " + seat + " booked");
System.out
.println("-------------------------------------------------------------------");
System.out.println(name + "(" + classStr + " - Seat " + seat + ") - "
+ prefStr);
System.out
.println("-------------------------------------------------------------------");
kb.close();
}
public void printTicketStub() {
char initial = name.split(" ")[0].toUpperCase().charAt(0); // splits name
// into first
// name and
// second name
System.out.println(initial + ". " + name.split(" ")[1] + "(" + classStr
+ " - Seat " + seat + ") - " + prefStr);
}
public String printPassengerCode() { // forms the code as required
return seatClass + name.toUpperCase().substring(name.length() - 4)
+ foodOption;
}
}

You have two problems:
You create a Scanner for System.in twice. You should only do this once. I have commented out the one you create in Passenger.java, and changed the reference to the one in RunAirLine.java:
public void enterPassenger(int i) {
// Scanner kb = new Scanner(System.in);
// asks the important questions
System.out.print("Enter passenger #" + i + "’s name: ");
name = RunAirLine.kb.nextLine();
System.out.println("Enter passenger #" + i + "’s food option ");
System.out.print("(V = vegetarian, N = no preference, H = Halal): ");
foodOption = RunAirLine.kb.nextLine().toUpperCase().charAt(0);
System.out.print("Enter passenger #" + i
+ "’s class (1 = first, 2 = business, 3 = economy): ");
seatClass = RunAirLine.kb.nextInt();
RunAirLine.kb.nextLine();
Every time you do nextInt(), it doesn't take the new line off the Scanner's reading, which messes stuff up. If you want nextInt() to be on it's own line, you should throw a dummy RunAirLine.kb.nextLine(); after the nextInt() call. This needs to be done twice, once in the code I've fixed above, and once in your RunAirLine.class:
System.out.print("How many seats do you want to book? ");
int n = kb.nextInt();
kb.nextLine();
System.out.println();
Further reading on Why you shouldn't have multiple Scanners. the tl;dr of is that closing System.in closes the stream forever.

Related

Scanner cant read/incorrectly reading my input

I am writing a code whereby I have to input people's name and money value. However I have used a scanner but it does not read the name that I input into the console. Whatever name I put, the code will tell me that no such name is found. I have tried for many hours trying to resolve this, assistance would be appreciated! (there are 6 total cases but I only posted the first one since its the only one I'm having problems with)
The code is as such:
import java.util.Scanner;
public class Client {
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
Change[] changeArray = new Change [10];
int numNames = 0;
System.out.println("Please enter at least 10 records to test the program");
String name = "";
int change = 0;
int flag = 0;
int[] totalNumberOfCoinsOf = {0,0,0,0,0};
for (int i = 0;i < changeArray.length;i++)
{
System.out.println("Enter name: ");
name = reader.nextLine();
do {
System.out.println("Enter coin value for the person");
change = Integer.parseInt(reader.nextLine());
if (change % 5 ==0) {
break;
}else if (change % 5 <2.5) {//round down to nearest 5 if change is less than 2.5
change = change - change %5;
break;
}
else if (change %5>=2.5)
{
change = Math.round(change * 100)/100; //round up to nearest 5 if change is more than 2.5
}
}while (true);
changeArray[i] = new Change(name, change);
numNames++;
do {System.out.println("Do you have another person to enter? (Y/N) ");
String choice = reader.nextLine();
if (i!=changeArray.length - 1) {
if(choice.toUpperCase().equals("Y")) {
break;
}else if (choice.toUpperCase().equals("N")) {
flag = 1;
break;
}
}
}while(true);
if (flag==1) {
break;
}
}
do {
System.out.println("\n[1] Search by name ");
System.out.println("\n[2] Search largest coin");
System.out.println("\n[3] Search smallest coin");
System.out.println("\n[4] Total coins");
System.out.println("\n[5] Sum of coins");
System.out.println("\n[6] Exit the program");
System.out.print("Enter your choice: ");
int choice = Integer.parseInt(reader.nextLine());
switch (choice) {
case 1:
System.out.print("Enter name to search: ");
name = reader.nextLine();
flag = 0;
for (int i = 0;i < numNames; i++) {
if (name.equals(changeArray[i].getName())) {
System.out.println("Customer: ");
System.out.println(changeArray[i].getName() + " " + changeArray[i].getCoinChangeAmount());
int[] coins = changeArray[i].getChange();
System.out.println("Change: ");
if(coins[0]!=0) {
System.out.println("Dollar coins: "+coins[0]);
}
if(coins[1]!=0) {
System.out.println("50 cents: "+coins[1]);
}
if(coins[2]!=0) {
System.out.println("25 cents: "+coins[2]);
}
if(coins[3]!=0) {
System.out.println("10 cents: "+coins[3]);
}
if(coins[4]!=0) {
System.out.println("5 cents: "+coins[4]);
}
flag++;
}
}
if (flag==0) {
System.out.println("Name: "+name);
System.out.println("Not found! ");
}
break;
//Change class
import java.util.Scanner;
public class Change {
private String name;
private int coinChangeAmount;
public Change() {
this.name = "no name";
this.coinChangeAmount = 0;
}
public Change(String name, int coinChangeAmount) {
this.name = "name";
this.coinChangeAmount = coinChangeAmount;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCoinChangeAmount() {
return coinChangeAmount;
}

An ArrayList that should have objects from a created class but print as null

My first class that creates the ArrayList and populates it with the Skill objects
import java.util.ArrayList;
public class Skill {
String skillName;
int skillValue;
public static ArrayList<Skill> skillList = new ArrayList<Skill>();
public Skill(String newSkillName, int newSkillValue){
setSkillName(newSkillName);
setSkillValue(newSkillValue);
}
public static void initializeSkillList(){
//Creating the Skill obj's
Skill str = new Skill("Str", 1);
Skill dex = new Skill("Dex", 1);
Skill intl = new Skill("Int", 1);
Skill con = new Skill("Con", 3);
//Adding them to the Skill ArrayList
skillList.add(str);
skillList.add(dex);
skillList.add(intl);
skillList.add(con);
//Debug statement to check if the values are correct
System.out.println(skillList.get(1).skillValue);
}
}
I am attempting to access the objects skillValue and skillName from the ArrayList in my Main.java class. I have setters and getters.
This is the Main class
import java.util.Scanner;
public class Main {
static Character playerCharacter;
static boolean charCreationComplete = false;
static boolean endGame = false;
static Scanner scnr = new Scanner(System.in);
public static void main(String[] args){
System.out.println("\n\nWelcome to Garterra!\n\n");
Archetype.initializeArchetypeList();
Skill.initializeSkillList();
if (!charCreationComplete){
charCreation(scnr);
charCreationComplete = true;
}
charCreation(scnr); then accesses the skillList through getters and setters
charCreation() Method:
private static void charCreation(Scanner scnr){
System.out.println("\nEnter player name:\n\n");
String newName = scnr.nextLine();
System.out.println("You Entered " + newName + " for your name!\n");
System.out.println("Next, Choose your Archetype: 0 = Ranger\n");
int archetypeIndex = scnr.nextInt();
Character.currArchetype = Archetype.archetypeList.get(archetypeIndex);
Character.archetypeName = Archetype.archetypeList.get(archetypeIndex).getArchetypeName();
System.out.println("You have chosen " + Character.archetypeName + ".\nThis gives you " + Character.currArchetype.totalSpendableSkillPoints + " total spendable skill points!\n");
System.out.println("Now, spend those skill points.\n");
if (Character.currArchetype.getArchetypeName().contains("Ranger")){
Ranger.dexModifierCalc(1);
Ranger.conModifierCalc(1);
}
while (Character.currArchetype.totalSpendableSkillPoints > 0){
System.out.println("How many points would you like to put into Strength?\nThis impacts your melee weapon damage and carry weight.\n");
int strAddPoints = scnr.nextInt();
Skill.skillList.get(0).setSkillValue(Skill.skillList.get(0).getSkillValue() + strAddPoints);
Character.currArchetype.setTotalSpendableSkillPoints(Character.currArchetype.totalSpendableSkillPoints - strAddPoints);
strAddPoints = 0;
System.out.println("You now have " + Skill.skillList.get(0).getSkillValue() + "\n");
if (Character.currArchetype.totalSpendableSkillPoints == 0){
break;
}
if (Character.currArchetype.totalSpendableSkillPoints > 0){
System.out.println("How many points would you like to put into Dexterity?\nThis impacts your ranged weapon damage.\n");
int dexAddPoints = scnr.nextInt();
Skill.skillList.get(1).setSkillValue(Skill.skillList.get(1).getSkillValue() + dexAddPoints);
Character.currArchetype.setTotalSpendableSkillPoints(Character.currArchetype.totalSpendableSkillPoints - dexAddPoints);
dexAddPoints = 0;
System.out.println("You now have " + Skill.skillList.get(1).getSkillValue() + "\n");
}
if (Character.currArchetype.totalSpendableSkillPoints == 0){
break;
}
if (Character.currArchetype.totalSpendableSkillPoints > 0){
System.out.println("How many points would you like to put into Intelligence?\nThis impacts your magic damage.\n");
int intAddPoints = scnr.nextInt();
Skill.skillList.get(2).setSkillValue(Skill.skillList.get(2).getSkillValue() + intAddPoints);
Character.currArchetype.setTotalSpendableSkillPoints(Character.currArchetype.totalSpendableSkillPoints - intAddPoints);
intAddPoints = 0;
System.out.println("You now have " + Skill.skillList.get(2).getSkillValue() + "\n");
}
if (Character.currArchetype.totalSpendableSkillPoints == 0){
break;
}
if (Character.currArchetype.totalSpendableSkillPoints > 0){
System.out.println("How many points would you like to put into Strength?\nThis impacts your melee weapon damage and carry weight.\n");
int conAddPoints = scnr.nextInt();
Skill.skillList.get(3).setSkillValue(Skill.skillList.get(3).getSkillValue() + conAddPoints);
Character.currArchetype.setTotalSpendableSkillPoints(Character.currArchetype.totalSpendableSkillPoints - conAddPoints);
conAddPoints = 0;
System.out.println("You now have " + Skill.skillList.get(3).getSkillValue() + "\n");
}
}
int newHealthPoints = Character.healthPointsCalc();
System.out.println("You have " + newHealthPoints + " health points!\n\n");
int newTotalCarryWeight = Character.carryWeightCalc(Character.currArchetype.getLevel());
System.out.println("Your total carry weight is " + newTotalCarryWeight + ".\n");
int newExperince = 0;
playerCharacter = new Character(newName, newHealthPoints, newExperince, 1, Archetype.archetypeList.get(archetypeIndex), newTotalCarryWeight);
}
If you create a getter for "skillList" then you can just say
Skill.getSkillList();
And then do whatever you want with this list.
To make your code a little better, change this
public static ArrayList<Skill> skillList = new ArrayList<>();
to this
public static List<Skill> skillList = new ArrayList<>();

How can i write a code or that displays the most rented book type in my programming code of a library system?

I think i wrote a code wrong in my programming code because the outcome displays a number for the most rented book type instead of the name of the book type. For example a person borrows a Novel book type and another person borrows a Novel book type as well. So when i run the program, it displays a data list and contains 2 people borrowing the same book type which is Novel and later it display "The most rented book type is: 2". When it should display "The most rented book type is: Novel"
Here is my programming Java code:
import java.util.Scanner;
class BookRentalShop
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of data you want to see: ");
int inputnum = sc.nextInt();
sc.nextLine();
System.out.println("");
String[] Name = new String[inputnum];
String[] Bookname = new String[inputnum];
String[] AuthorName = new String[inputnum];
String[] Booktype = new String[inputnum];
int[] NumbersofDaysBorrowed = new int[inputnum];
int[] RentalCharges = new int[inputnum];
String[] Types = {"Cartoon","Magazine", "Short story", "Long story", "Journal", "Novel", "Encyclopedia"};
int[] count = new int[7];
for (int d = 0; d < inputnum; d = d + 1)
{
System.out.println("Enter the name of the person:");
Name[d] = sc.nextLine();
System.out.println("Enter the bookname:");
Bookname[d] = sc.nextLine();
System.out.println("Enter the author's name:");
AuthorName[d] = sc.nextLine();
System.out.println("Enter the book type:");
Booktype[d] = sc.nextLine();
for (int k = 0; k < 7; k++)
{
if (Booktype[d].equals(Types[k]))
{
count[k]++;
}
}
System.out.println("Enter the number of days that the book had been borrowed:");
NumbersofDaysBorrowed[d] = sc.nextInt();
sc.nextLine();
if (Booktype[d].equalsIgnoreCase("Cartoon"))
{
RentalCharges[d] = NumbersofDaysBorrowed[d] * 500;
} else if (Booktype[d].equalsIgnoreCase("Magazine"))
{
RentalCharges[d] = NumbersofDaysBorrowed[d] * 1000;
} else if (Booktype[d].equalsIgnoreCase("Short story"))
{
RentalCharges[d] = NumbersofDaysBorrowed[d] * 500;
} else if (Booktype[d].equalsIgnoreCase("Long story"))
{
RentalCharges[d] = NumbersofDaysBorrowed[d] * 1500;
} else if (Booktype[d].equalsIgnoreCase("Journal"))
{
RentalCharges[d] = NumbersofDaysBorrowed[d] * 350;
} else if (Booktype[d].equalsIgnoreCase("Novel"))
{
RentalCharges[d] = NumbersofDaysBorrowed[d] * 1500;
} else {
RentalCharges[d] = NumbersofDaysBorrowed[d] * 2500;
}
if(NumbersofDaysBorrowed[d] > 5)
{
int bookCost = RentalCharges[d] / NumbersofDaysBorrowed[d];
RentalCharges[d] = (5 * bookCost) + ((NumbersofDaysBorrowed[d] - 5) * (bookCost / 2));
}
}
System.out.printf("%s %20s %20s %20s %20s %20s %20s\n", "No", "Name", "Bookname", "AuthorName", "Booktype", "Numbers of Days Borrowed", "Rental Charges");
for (int d = 0; d < inputnum; d = d + 1)
{
int num = d + 1;
System.out.printf("%s %20s %20s %20s %20s %20d %20d\n", num, Name[d], Bookname[d], AuthorName[d], Booktype[d], NumbersofDaysBorrowed[d], RentalCharges[d]);
}
String again = "Yes";
String exist = "No";
while (again.equals("Yes")) {
exist = "No";
System.out.println("enter the search name");
String searchname = sc.nextLine();
for (int d = 0; d < inputnum; d = d + 1) {
if (searchname.equals(Name[d])) {
System.out.println("Name : " + Name[d]);
System.out.println("Bookname : " + Bookname[d]);
System.out.println("Number of Days Borrowed : " + NumbersofDaysBorrowed[d]);
exist = "Yes";
}
}
if (exist.equals("No")) {
System.out.println("The search name requested is not found");
}
System.out.println("Do you want to search again? (Yes,No) ");
again = sc.nextLine();
}
*int max = count[0];
for (int d = 0; d < 7; d = d + 1)
{
for (int k = d + 1; k < 7; k = k + 1)
{
if (count[k] > count[d])
{
max = count[k];
}
else {
max = count[d];
}
}
}
System.out.println("");
System.out.println("The most rented booktype is: " + max);
System.out.println("");
}
}*
Here is the outcome of the code:
No Name Bookname AuthorName Booktype Numbers of Days Borrowed Rental Charges
1 J Grere bvcnvnb Journal 3 1050
2 K wqerwr xczzzx Novel 6 8250
3 opoipo kkghjhjgh bvcbcvbc Cartoon 5 2500
4 Q erwytiu ghfghgfd Magazine 7 6000
5 D sdsafhgjhgjk vvbbnbn,nbvc Novel 6 8250
enter the search name
No
The search name requested is not found
Do you want to search again? (Yes,No)
No
The most rented booktype is: 2
I indicated where i think the code is wrong with a star (*) which is near the end of my code.
You are printing out the int value and not the String. Try printing out Booktype[max]
System.out.println("The most rented booktype is: " + Booktype[max]);

How to print out information from data used in arrays?

My code asks for a user to enter how many wins, losses, and ties 6 different sports teams have gotten throughout a season. How can I make it so that once all the information has been received, it will print out how many wins, ties, and losses each team have gotten, as well as displaying the total amount of each?
Code:
package SMKTeamStandings;
import java.util.Scanner;
public class SMKTeamStandings {
public static Scanner in = new Scanner(System.in);
public static int number(int max, int min) {
int teamchoice = 0;
for (boolean valid = false; valid == false;) {
teamchoice = in.nextInt();
if (teamchoice >= min && teamchoice <= max) {
valid = true;
} else {
System.out.println("Please enter a different value.");
}
}
return teamchoice;
}
public static boolean finished(boolean[] completedArray) {
int i = 0;
boolean done;
for (done = true; done == true;) {
if (completedArray[i++] == false) {
done = false;
}
}
return done;
}
public static void main(String[] args) {
int teamChoice = 0, gamesNum;
String[] sportteams = {"Basketball", "Football",
"Hockey", "Rugby",
"Soccer", "Volleyball"};
boolean[] completed = new boolean[sportteams.length];
int[][] Outcome = new int[64][sportteams.length];
for (boolean done = false; done == false;) {
for (int i = 0; i < sportteams.length; i++) {
System.out.print(i + 1 + " - " + sportteams[i]);
if (completed[i] == true) {
System.out.println(" - Finished");
} else {
System.out.println();
}
}
System.out.print("\nChoose a team from the list above:");
teamChoice = number(6, 1);
teamChoice--;
System.out.print("\nHow many games total did the " + sportteams[teamChoice]
+ " team play this season?: ");
gamesNum = in.nextInt();
System.out.format("\n %10s %10s %10s %10s %10s \n\n", "", "Possible Outcomes:",
"1 - Win",
"2 - Tie",
"3 - Loss");
for (int wintieloss = 0; wintieloss < gamesNum; wintieloss++) {
System.out.print("\nEnter the outcome for game "
+ (wintieloss + 1) + ": ");
Outcome[wintieloss][teamChoice] = number(3, 1);
}
System.out.println("\n");
completed[teamChoice] = true;
done = finished(completed);
If I understood you correctly, you just want to output the data you got from the user. To do that you could go through the data array using a for loop and accessing the data using indices.
for(int team = 0; team < sportteams.length; team++) { // for each team
System.out.println((team + 1) + " - " + sportteams[team]); // output the team
int game = 0; // index of the current game
while(Outcome[game][team] != 0) { // while there is data
System.out.print("Game " + (game + 1) ": " + Outcome[game][team] + " "); // print the data
game++; // increment the index
}
System.out.println("Total games: " + game); // print the last index == total number of games
System.out.println();
}

User inputted array is entirely the last input

When I execute this program, the canidateArray only stores the last user input as every variable in the array.
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class ArrayElection {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of candidates: ");
int loops = input.nextInt();
int loops2 = loops;
Canidate[] canidateArray = new Canidate[loops];
String canidatename;
int canidatevotes = 0;
int outputno = 1;
while (loops > 0){
System.out.println("Enter " + outputno +". name:");
canidatename = input.next();
System.out.println("Enter votes:");
canidatevotes = input.nextInt();
new Canidate(canidatename, canidatevotes);
loops = loops - 1;
outputno = outputno + 1;
}
if (canidateArray[0].getVotes() == canidateArray[1].getVotes()) {
System.out.println("The election is a tie between the following candidates: ");
System.out.println(canidateArray[0].getName() + " (" + canidateArray[0].getVotes() + " votes)");
System.out.println(canidateArray[1].getName() + " (" + canidateArray[1].getVotes() + " votes)");
System.out.println(canidateArray[2].getName() + " (" + canidateArray[2].getVotes() + " votes)");
}
else {
System.out.println("The winner is " + canidateArray[0].getName() + " with " + canidateArray[0].getVotes() + " votes!");
}
}
public static class Canidate {
private static String name;
private static int votes;
public Canidate(String name, int votes) {
this.name = name;
this.votes = votes;
}
public static String getName() {
return name;
}
public static int getVotes() {
return votes;
}
}
}
You don't store anything in the array. Fix :
while (loops > 0){
System.out.println("Enter " + outputno +". name:");
canidatename = input.next();
System.out.println("Enter votes:");
canidatevotes = input.nextInt();
loops = loops - 1;
canidateArray[outputno-1] = new Canidate(canidatename, canidatevotes);
outputno = outputno + 1;
}
But you'll have to fix your last part of code. You test canidateArray[0] vs canidateArray[1] while you can have
1 candidate, then it will trigger an IndexOutOfBoundsException
or more than 2 candidates

Categories