Why is my Buffered Reader skipping lines - java

I am working on a project for my programming class and I am having trouble with the BufferedReader. Here is the code. It will run fine but it is only reading every other line in my csv file. I think the issue is I have inFile = input.readLine in there twice but if I remove one of them the I get runtime errors.
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.io.BufferedReader;
public class CrimeData {
public static void main(String[] args) throws FileNotFoundException {
BufferedReader input = new BufferedReader(new FileReader("crime.csv"));
String inFile;
try {
inFile = input.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int n = 0;
int year[] = new int[50], population[] = new int[50], violentCrime[] = new int[50];
double violentCrimeRate[] = new double[50];
int murderAndNonnegligentManslaughter[] = new int[50];
double murderAndNonnegligentManslaughterRate[] = new double[50];
int rape[] = new int[50];
double rapeRate[] = new double[50];
int robbery[] = new int[50];
double robberyRate[] = new double[50];
int aggravatedAssault[] = new int[50];
double aggravatedAssaultRate[] = new double[50];
int propertyCrime[] = new int[50];
double propertyCrimeRate[] = new double[50];
int burglary[] = new int[50];
double burglaryRate[] = new double[50];
int larcenyTheft[] = new int[50];
double larcenyTheftRate[] = new double[50];
int motorVehicleTheft[] = new int[50];
double motorVehicleTheftRate[] = new double[50];
try {
while ((inFile = input.readLine()) != null) {
String words[] = input.readLine().split(",");
year[n] = Integer.parseInt(words[0]);
population[n] = Integer.parseInt(words[1]);
violentCrime[n] = Integer.parseInt(words[2]);
violentCrimeRate[n] = Double.parseDouble(words[3]);
murderAndNonnegligentManslaughter[n] = Integer.parseInt(words[4]);
murderAndNonnegligentManslaughterRate[n] = Double.parseDouble(words[5]);
rape[n] = Integer.parseInt(words[6]);
rapeRate[n] = Double.parseDouble(words[7]);
robbery[n] = Integer.parseInt(words[8]);
robberyRate[n] = Double.parseDouble(words[9]);
aggravatedAssault[n] = Integer.parseInt(words[10]);
aggravatedAssaultRate[n] = Double.parseDouble(words[11]);
propertyCrime[n] = Integer.parseInt(words[12]);
propertyCrimeRate[n] = Double.parseDouble(words[13]);
burglary[n] = Integer.parseInt(words[14]);
burglaryRate[n] = Double.parseDouble(words[15]);
larcenyTheft[n] = Integer.parseInt(words[16]);
larcenyTheftRate[n] = Double.parseDouble(words[17]);
motorVehicleTheft[n] = Integer.parseInt(words[18]);
motorVehicleTheftRate[n] = Double.parseDouble(words[19]);
n++;
}
} catch (NumberFormatException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("********** Welcome to the US Crime Statistical Application **************************");
System.out.println("Enter the number of the question you want answered. ");
System.out.println("1. What were the percentages in population growth for each consecutive year from 1994 – 2013?");
System.out.println("2. What year was the Murder rate the highest?");
System.out.println("3. What year was the Murder rate the lowest?");
System.out.println("4. What year was the Robbery rate the highest?");
System.out.println("5. What year was the Robbery rate the lowest?");
System.out.println("6. What was the total percentage change in Motor Vehicle Theft between 1998 and 2012?");
System.out.println("7. What was [enter your first unique statistic here]?");
System.out.println("8. What was [enter your second unique statistic here]?");
System.out.println("9. Quit the program");
System.out.println("Enter your selection: ");
int choice = scan.nextInt();
double low, high, percent;
int y;
switch (choice) {
case 1:
for (int i = 1; i < n; i++) {
percent = ((population[i] - population[i - 1]) / population[i - 1]) * 100;
System.out.println(
"Percentage of population growth during " + year[i - 1] + "-" + year[i] + " :" + percent);
}
break;
case 2:
high = murderAndNonnegligentManslaughter[0];
y = year[0];
for (int i = 1; i < n; i++) {
if (murderAndNonnegligentManslaughter[i] > high) {
high = murderAndNonnegligentManslaughter[i];
y = year[i];
}
}
System.out.println("The year has the highest Murder rate : " + y);
break;
case 3:
low = murderAndNonnegligentManslaughter[0];
y = year[0];
for (int i = 1; i < n; i++) {
if (murderAndNonnegligentManslaughter[i] < low) {
low = murderAndNonnegligentManslaughter[i];
y = year[i];
}
}
System.out.println("The year has the lowest Murder rate : " + y);
break;
case 4:
high = robberyRate[0];
y = year[0];
for (int i = 1; i < n; i++) {
if (robberyRate[i] > high) {
high = robberyRate[i];
y = year[i];
}
}
System.out.println("The year has the highest Robbery rate : " + y);
break;
case 5:
low = robberyRate[0];
y = year[0];
for (int i = 1; i < n; i++) {
if (robberyRate[i] < low) {
low = robberyRate[i];
y = year[i];
}
}
System.out.println("The year has the lowest Robbery rate : " + y);
break;
case 6:
double rateChange = 0;
rateChange = (motorVehicleTheft[19] - motorVehicleTheft[5]);
System.out.println(motorVehicleTheft);
case 7:
break;
case 8:
break;
case 9:
System.out.println("Thank you for using the Crime Database");
System.exit(0);
}
}
}
}

Your guess is absolutely correct.
This skips because the first condition in your while loop reads a line,
while((infile = input.readLine()) != null){
but you do not do anything with the read-in line.
Then the next time you call
input.readLine();
You read the next line, but you actually do something with the line.
For further reference, try looking at this thread:
Java: How to read a text file

Related

IDK why my code keeps crashing taking the input only one time in this loop

The question is taking input of names of students and their score and declaring the topper but the output ain't working.
Here, I am looking for the highest mark with always keeping the value of the current highest mark, please help me if I am missing something.
import java.util. * ;
public class Topper {
public static void main(String args[]) {
Scanner sc = new Scanner(System. in );
String studentTemp = "";
String students[] = new String[100];
int i = 0,
j = 0,
temp = 0,
n = 0;
int score[] = new int[100];
do {
System.out.println("Enter the Name");
students[i] = sc.nextLine();
System.out.println("Enter the Score");
score[i] = sc.nextInt();
n++;
i++;
} while ( students [ i ] != "Alldone");
for (i = 0; i < n; i++) {
for (j = i + 1; j < n - 1; j++) {
if (score[j] > score[i]) {
temp = score[j];
score[j] = score[i];
score[i] = temp;
studentTemp = students[i];
students[i] = students[j];
students[j] = studentTemp;
}
}
}
System.out.println("The Student with Highest score : " + students[n - 1] + " with score : " + score[n - 1]);
System.out.println("The Students performance list : ");
for (i = n - 1; i >= 0; i++)
System.out.println(students[i] + " " + score[i]);
}
}
Try this
import java.util.*;
public class Topper
{
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
String studentTemp="";
String students[]=new String[100];
int i=0,j=0,temp=0,n=0,stop=0;
int score[]=new int[100];
do
{
System.out.println("Enter the Name");
students[i]=sc.next();
System.out.println("Enter the Score");
score[i]=sc.nextInt();
System.out.println("Do you wish to continue \n Press 0 to continue \n Press 1 to exit");
stop=sc.nextInt();
n++;
i++;
}
while(stop!=1);
temp=score[0];
studentTemp=students[0];
for(i=1;i<n;i++)
{
if(score[i]>temp)
{
temp=score[i];
studentTemp=students[i];
}
}
System.out.println("The Student with Highest score : "+ studentTemp +" with score : "+temp);
System.out.println("The Students performance list : ");
for(i=0;i<n;i++)
System.out.println(students[i]+" "+score[i]);
}
}```

A discount program code is missing from my programming code

I think a code is missing or not working because there should be a discount if the number is more than 5. For example a novel is borrowed for 6 days and so the rental charge should 8250. But isn't working as the rental charge is 9000.
Here is my program 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;
}
}
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 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 : " + NumbersofDaysBorrowed[d]);
exist = "Yes";
}
}
if (exist.equals("No")) {
System.out.println("The search name 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 book is: " + max);
System.out.println("");
}
}
Outcome of the program:
No Name Bookname AuthorName Booktype Numbers of Days Borrowed Rental Charges
1 ghjghj hjghjgh hfghg Journal 3 1050
2 hghjhgjhgj uyiuyjghjg ghytghghjg Novel 6 9000
3 bcvnvn dasdasd weqwew Cartoon 5 2500*
I would appreciate it if anyone can help me find this problem.
I suggest solution like below:
This is the fragment of your code:
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;
}
And under this code try to add this. It replace in array calculated cost of book reduced by discount which is e.g 750 when number of days is more than 5. You can change it like you want
if(NumbersofDaysBorrowed[d] > 5)
{
int bookCost = RentalCharges[d] / NumbersofDaysBorrowed[d];
RentalCharges[d] = (5 * bookCost) + ((NumbersofDaysBorrowed[d] - 5) * (bookCost / 2)); // DISCOUNT
}

How can I define a variable so it can be used outside a while loop in java?

So the issue I'm having is that I'm declaring an array ability1 inside of a while loop, then trying to access that same array inside an if statement that is outside the loop. I can't figure out how to make that accessible as a global function. (BTW I'm new to Java so I don't know all the fancy stuff you can do).
Thanks!
import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
public class TextGame {
public static void main(String args[]) throws InterruptedException {
System.out.println('\u000C');
int x = 1;
int y = 1;
int money = 0;
int exp = 0;
int mobHealth = 50;
int playerHealth = 50;
int[] loot = new int[0];
String[] ability1;
//String ability2;
//String ability3;
String[] resp = new String[3];
resp[0] = "Left";
resp[1] = "Right";
resp[2] = "Straight";
String[] mob = new String[5];
mob[0] = "Kobold";
mob[1] = "Wolf";
mob[2] = "Orc";
mob[3] = "Pirate";
mob[4] = "Thief";
String[] clxss = new String[3];
clxss[0] = "Warrior";
clxss[1] = "Hunter";
clxss[2] = "Mage";
String[] mobloot = new String[5];
mobloot[0] = "13 Gold";
mobloot[1] = "28 Gold";
mobloot[2] = "Iron Shortsword";
mobloot[3] = "Linen Cloth";
mobloot[4] = "15 Gold";
System.out.println("Enter Character Name: ");
Scanner name = new Scanner(System.in);
String username = name.nextLine();
System.out.println("Character " + username + " created");
System.out.println("\n");
System.out.println("Enter A Class: ");
System.out.println("\n");
System.out.println(Arrays.toString(clxss));
Scanner se = new Scanner(System.in);
String clyss = se.nextLine();
System.out.println("Class " + clyss + " selected");
System.out.println("\n");
System.out.println("Loading Game...");
TimeUnit.SECONDS.sleep(3);
System.out.println('\u000C');
while (x == 1) {
System.out.println("You are in a forest");
System.out.println("\n");
System.out.println("Go Left, Right, or Straight.");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
Random rand = new Random();
int mobs = rand.nextInt(5);
System.out.println("You encounter a " + mob[rand.nextInt(4) + 1] + "!");
while (input.equals("Left") || input.equals("Right") || input.equals("Straight")) {
System.out.println("Fight or Run!");
Scanner sd = new Scanner(System.in);
String encounter = sd.nextLine();
if (encounter.equals("Fight")) {
System.out.println("Encounter Started!");
break;
} else if (encounter.equals("Run")) {
System.out.println("Encounter Started Anyway!");
break;
} else {
System.out.println("Enter A Valid Command!");
continue;
}
}
System.out.println("Enter Your Attack Method!");
while (clyss.equals("Warrior")) {
ability1 = new String[3];
ability1[0] = "Slam";
ability1[1] = "Charge";
ability1[2] = "Cleave";
System.out.println(Arrays.toString(ability1));
break;
}
while (clyss.equals("Hunter")) {
ability1 = new String[3];
ability1[0] = "Arcane Shot";
ability1[1] = "Steady Shot";
ability1[2] = "Aimed Shot";
System.out.println(Arrays.toString(ability1));
break;
}
while (clyss.equals("Mage")) {
ability1 = new String[3];
ability1[0] = "Firebolt";
ability1[1] = "Frostbolt";
ability1[2] = "Arcanebolt";
System.out.println(Arrays.toString(ability1));
break;
}
break;
}
Scanner tak = new Scanner(System.in);
String attack = tak.nextLine();
if (attack == ability1[0]) {
System.out.println("You cast " + ability1[0] + "!");
} else if (attack == ability1[1]) {
System.out.println("You cast " + ability1[1] + "!");
} else if (attack == ability1[2]) {
System.out.println("You cast " + ability1[2] + "!");
}
}
}
Initialize the ability1 array before the loop. I.e, delete all three ability1 = new String[3]; assignments and replace String[] ability1; with String[] ability1 = new String[3];.
Your code will at least start compiling. But it indeed has a lot of other issues (e.g., the never-ending while (x == 1) loop), so consider refactoring.

Problems with my for loops printing out my arrays

can anybody help me figure out why my for loops cant print the right answer out.
Its like its skipping the first array number [0]. but if i try to make it print out my Array nr [1] out it works fine.
It must be somthing with my counter ans answer at the top.
package assignment9.pkg1;
import java.util.Scanner;
/**
*
* #author Anders
*/
public class Assignment91 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
String studName = "Anders";
int counter = 1;// i think the problem is here
int answer = 1; // same
System.out.println(" welcome to student database, show informations about student" + studName);
Scanner courseScan = new Scanner(System.in);
Scanner gradeScan = new Scanner(System.in);
Scanner answerScan = new Scanner(System.in);
System.out.println(" Enter the name of courses");
String[] courseArray = new String[counter];
int[] gradeArray = new int[counter];
for (int k = 0; k <= counter; k++) {
if (counter < 20) {
while (answer != 0) { // what have i done here with that 0 answer??
System.out.println(" enter name");
courseArray[k] = courseScan.nextLine();
System.out.println(" Enter grade");
gradeArray[k] = gradeScan.nextInt();
System.out.println(" Do you want to add one more course enter 1, if not enter 0");
answer = answerScan.nextInt();
}
} else {
System.out.println("Sorry, there is no more memory");
}
}
int n = gradeArray.length;
int temp = 0;
for (int i = 0; i < n; i++) {
for (int j = 1; j < (n - i); j++) {
if (gradeArray[j - 1] > gradeArray[j]) {
//swap the elements!
temp = gradeArray[j - 1];
gradeArray[j - 1] = gradeArray[j];
gradeArray[j] = temp;
// Swap the course array
String gradeArrayTemp;
gradeArrayTemp = courseArray[j - 1];
courseArray[j - 1] = courseArray[j];
courseArray[j] = gradeArrayTemp;
}
}
}
for (int l = 0; l < courseArray.length; l++) {
System.out.println("grade " + gradeArray[l] + "name " + courseArray[l]); // why does it not print all the array out
}
Scanner request = new Scanner(System.in);
System.out.println(" what do you want to do. Enter 1 to rename a course");
System.out.println(" enter 2 to change a grade ");
int regNumber = request.nextInt();
switch (regNumber) {
case 1: // rename a course
Scanner search = new Scanner(System.in);
System.out.println("Enter the name of the course you want to rename");
String searchCourse = search.nextLine();
for (int i = 0; i < courseArray.length; i++) {
if (searchCourse.equals(courseArray[i])) {
System.out.println("Yes there is a course named " + courseArray[i]);
System.out.println(" to change coursename insert new name");
// here i change the coursename
Scanner newName = new Scanner(System.in);
courseArray[i] = newName.nextLine();
} else {
System.out.println(" no record of this course");
}
System.out.println(" you have chosen to rename course into " + courseArray[i]);
}
}
}
}
In this section of the code:
for (int k = 0; k <= counter; k++) {
if (counter < 20) {
while (answer != 0) { // what have i done here with that 0 answer??
System.out.println(" enter name");
courseArray[k] = courseScan.nextLine();
System.out.println(" Enter grade");
gradeArray[k] = gradeScan.nextInt();
System.out.println(" Do you want to add one more course enter 1, if not enter 0");
answer = answerScan.nextInt();
}
} else {
System.out.println("Sorry, there is no more memory");
}
}
note that you have used k to insert into array. the k do not update because is stuck in the inner while loop There for use another counter.
Also here
String[] courseArray = new String[counter];
you use counter to create the array. Which is 1. You are creating one eliment array.
You code will work like this:
int k = 0;
if (counter < 20) {
while (answer != 0) { // what have i done here with that 0 answer??
System.out.println(" enter name");
courseArray[k] = courseScan.nextLine();
System.out.println(" Enter grade");
gradeArray[k] = gradeScan.nextInt();
System.out.println(" Do you want to add one more course enter 1, if not enter 0");
answer = answerScan.nextInt();
k++;
}
} else {
System.out.println("Sorry, there is no more memory");
}
And
for (int i = 0; i < courseArray.length; i++) {
if(courseArray[i] != null)
System.out.println("grade " + gradeArray[i] + "name " + courseArray[i]); // why does it not print all the array out
}
I have assumed that you take only 20 records depending on the loop. So i initiated arrays
String[] courseArray = new String[20];
int[] gradeArray = new int[20];

this block of code going straight to break in java

I have this block in a switch case statement that when selected, just breaks and presents me with the main menu again.
System.out.println("Choose a competitor surname");
String competitorChoice2 = input.nextLine();
int lowestSpeed = Integer.MAX_VALUE;
int highestSpeed = 0;
for(int j = 0; j < clipArray.length; j++) {
if(clipArray[j] != null) {
if(competitorChoice2.equals(clipArray[j].getSurname())) {
if(clipArray[j].getSpeed() > clipArray[highestSpeed].getSpeed()) {
highestSpeed = j;
}
}
}
}
for(int i = 0; i < clipArray.length; i++) {
if(clipArray[i] != null) {
if(competitorChoice2.equals(clipArray[i].getSurname())) {
if(clipArray[i].getSpeed() < clipArray[lowestSpeed].getSpeed()) {
lowestSpeed = i;
}
}
}
}
for(int h = lowestSpeed; h < highestSpeed; h++ ) {
System.out.println(""+clipArray[h].getLength());
}
I have an array of objects and each object has a surname and a speed.
I want the user to choose a surname and display the speeds of all of their clips from lowest to highest.
when I select this option it just breaks and brings me back to the main menu
The speeds are also originally entered as floats.
here is the menu:
while (true) {
System.out.println("Please select one of the following options by entering a number: \n"
+ "1) Quit\n"
+ "2) Add a new clip to the records\n"
+ "3) View information about a clip via an index number\n"
+ "4) Change information about a clip via an index number\n"
+ "5) List all competitors which have a clip recorded\n"
+ "6) Choose a competitor and display their longest clip\n"
+ "7) Choose a competitor and display their clips arranged by speed\n"
+ "8) display elements of array in alphabetical order");
choice = input.nextInt();
input.nextLine();
switch (choice) {
switch (choice) {
case (1):
System.out.println("You have quit the program");
System.exit(0);
case (2):
Clip c = new Clip();
System.out.println("Set an index number between 1-1000");
int setIndexNumber = input.nextInt();
c.setIndexNumber(setIndexNumber);
System.out.println("What is the given name of the competitor?");
String givenName = input.next();
c.setGivenName(givenName);
System.out.println("What is the surname of the competitor?");
String surname = input.next();
c.setSurname(surname);
System.out.println("What is the length of the clip?");
float setLength = input.nextFloat();
c.setLength(setLength);
System.out.println("What is the speed of the competitor?");
float setSpeed = input.nextFloat();
c.setSpeed(setSpeed);
System.out.println("What what time was this recorded? (24 hour)");
System.out.println("Enter hour: ");
int setHour = input.nextInt();
System.out.println("Enter minute: ");
int setMin = input.nextInt();
c.setTime(setHour, setMin);
clipArray[firstAvailableIndex()] = c;
counter++;
break;
case (3):
System.out.println("Which clip do you want to view?\n"
+ "Select from index 0-1000:");
int indexNo = input.nextInt();
for (int j = 0; j < clipArray.length; j++) {
if (j == indexNo) {
System.out.println("Index Number: " + clipArray[j].getIndexNumber());
System.out.println("Given Name: " + clipArray[j].getGivenName());
System.out.println("Surname: " + clipArray[j].getSurname());
System.out.println("Length: " + clipArray[j].getLength());
System.out.println("Speed: " + clipArray[j].getSpeed());
System.out.println("Time: " + clipArray[j].getHour() + ":" + clipArray[j].getMinute());
break;
}
}
break;
case (4):
System.out.println("Which clip do you want to change? choose and index number: ");
int clipIndex = input.nextInt();
input.nextLine();
System.out.println("What do want to set this given name to?");
String editGivenName = input.nextLine();
clipArray[clipIndex].setGivenName(editGivenName);
System.out.println("What do you want to set this surname to?");
String editSurname = input.nextLine();
clipArray[clipIndex].setSurname(editSurname);
System.out.println("What do you want to set this length to?");
float editLength = input.nextFloat();
clipArray[clipIndex].setLength(editLength);
System.out.println("What do you want to set this speed to?");
float editSpeed = input.nextFloat();
clipArray[clipIndex].setSpeed(editSpeed);
System.out.println("What do you want to set this hour to?");
int editHour = input.nextInt();
System.out.println("What do you want to set this minute to?");
int editMin = input.nextInt();
clipArray[clipIndex].setTime(editHour, editMin);
break;
case (5):
for (int g = 0; g < clipArray.length; g++) {
if (clipArray[g] != null) {
System.out.println(""+clipArray[g].getSurname());
}
}
break;
case (6):
System.out.println("Choose a competitor by surname");
String competitorChoice = input.nextLine();
int longestClip = 0;
for(int i = 0; i < clipArray.length; i++) {
if(clipArray[i] != null) {
if (competitorChoice.equals(clipArray[i].getSurname())) {
if(clipArray[i].getLength() > clipArray[longestClip].getLength()) {
longestClip = i;
}
}
}
}
System.out.println(""+clipArray[longestClip].getLength()+", at Index: "+clipArray[longestClip].getIndexNumber());
break;
case (7):
System.out.println("Choose a competitor surname");
String competitorChoice2 = input.nextLine();
int lowestSpeed = Integer.MAX_VALUE;
int highestSpeed = 0;
for(int j = 0; j < clipArray.length; j++) {
if(clipArray[j] != null) {
if(competitorChoice2.equals(clipArray[j].getSurname())) {
if(clipArray[j].getSpeed() > clipArray[highestSpeed].getSpeed()) {
highestSpeed = j;
}
}
}
}
for(int i = 0; i < clipArray.length; i++) {
if(clipArray[i] != null) {
if(competitorChoice2.equals(clipArray[i].getSurname())) {
if(clipArray[i].getSpeed() < clipArray[lowestSpeed].getSpeed()) {
lowestSpeed = i;
}
}
}
}
for(int h = lowestSpeed; h < highestSpeed; h++ ) {
System.out.println(""+clipArray[h].getLength());
}
break;
case (8):
for(int i = 1; i < counter; i++) {
for(int j = 0; j < counter - 1; j++) {
if(((clipArray[j].getSurname()).compareToIgnoreCase((clipArray[j+1].getSurname()))) > 0) {
Clip temp = clipArray[j];
clipArray[j] = clipArray[j+1];
clipArray[j+1] = temp;
}
}
}
for(int g = 0; g < counter; g++) {
System.out.println(clipArray[g].getSurname());
}
break;
default:
System.out.println("you have not selected a valid option");
break;
}//end of switch case
EDIT: out of bounds exceptions at the if statement for the lowest speed
When lowestSpeed is Integer.MAX_VALUE, that's out of bounds for the clipArray. Change the initialization for lowestSpeed to:
int lowestSpeed = 0;
Initializing a variable to the maximum before doing a loop that searches for the minimum is appropriate when the variable is used to store the minimum value itself, but in this case it's not: it's storing the index of the element having the minimum value, so start with the first valid index (Er, I think).

Categories

Resources