Edit: Yes, I know that ScanObj is not a proper name. And here is all of my code. It is not complete so there are some errors and some glitches, but you asked for it:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Scanner;
import javax.swing.JFrame;
public class MainClass {
static String[][] medium = new String[5][4];
static String names[] = { "Gatz ", "Cat ", "Women", "Mice ", "Robin",
"Romeo ", "Summer ", "Tempest", "Hamlet ", "Lear ",
"Titanic ", "StarWars", "DieHard ", "Elf ", "RED " };
static String lexile[] = { "L:1070", "L:123 ", "L:9254", "L:1234",
"L:534 ", "L:349", "L:632 ", "L:1097", "L:6453", "L:812 ",
"L:NA ", "L:NA ", "L:NA ", "L:NA ", "L:NA ", "L:NA " };
static String grade[] = { "G:9", "G:10", "G:12", "G:11", "G:9", "G:12",
"G:11", "G:9", "G:9", "G:12", "G:12", "G:11", "G:11", "G9", "G:9" };
static int max = 4;
char sorttype = (Character) null;
public static void main(String args[]) throws InterruptedException {
System.
out.println("Would you like to browse:\nB)Book C)CD or D)DVD");
Scanner ScanObj =
new Scanner(System.in);
String[] input =
new String[10];
input[1] = ScanObj.nextLine();
int store[] = { 6, 6, 6, 6, 6, 6 };
if (input[1].equals("B") || input[1].equals("Book")
|| input[1].equals(
"book") || input[1].equals("b")) {
String temp[][] =
new String[5][4];
int random[] = new int[5];
boolean repeat = true;
int nums[] = new int[5];
for (int i = 0; i < 5; i++) {
temp[i][0] =
"book";
temp[i][1] =
names[i];
temp[i][2] =
lexile[i];
temp[i][3] =
grade[i];
for (int qwop = 0; qwop < 4; qwop++) {
medium[i][qwop] = temp[i][qwop];
if (qwop == 0)
System.
out.printf(i + 1 + " | ");
else if (qwop < 3)
System.
out.printf(medium[i][qwop] + " | ");
else
System.
out.println(medium[i][qwop] + "\n");
Thread.sleep(150);
}
// System.out.println(temp[i][i]);
// random = store[i];
if (i == 4)
System.
out.printf("\n\n\n");
}
}
if (input[1].equals("C") || input[1].equals("c")
|| input[1].equalsIgnoreCase(
"CD") || input[1].equals("cd")) {
String temp[][] =
new String[5][4];
for (int i = 0; i < 5; i++) {
int random1 = (int) (Math.random() * max) + 6;
for (int p = 0; p < 4; p++) {
for (random1 = (int) (Math.random() * max) + 6; random1 == store[p];) {
random1 = (
int) (Math.random() * max) + 6;
}
}
temp[i][0] =
"CD";
temp[i][1] =
names[i + 5];
temp[i][2] =
lexile[i + 5];
temp[i][3] =
grade[i + 5];
for (int qwop = 0; qwop < 4; qwop++) {
medium[i][qwop] = temp[i][qwop];
if (qwop < 3)
System.
out.printf(medium[i][qwop] + " | ");
else
System.
out.println(medium[i][qwop] + "\n");
Thread.sleep(150);
}
// System.out.println(temp[i][i]);
random1 = store[i];
if (i == 4)
System.
out.printf("\n\n\n");
}
}
if (input[1].equalsIgnoreCase("D") || input[1].equalsIgnoreCase("DVD")) {
String temp[][] =
new String[5][4];
for (int i = 0; i < 5; i++) {
int random1 = (int) (Math.random() * max) + 6;
for (int p = 0; p < 4; p++) {
for (random1 = (int) (Math.random() * max) + 6; random1 == store[p];) {
random1 = (
int) (Math.random() * max) + 6;
}
}
temp[i][0] =
"DVD";
temp[i][1] =
names[i + 10];
temp[i][2] =
lexile[i + 10];
temp[i][3] =
grade[i + 10];
for (int qwop = 0; qwop < 4; qwop++) {
medium[i][qwop] = temp[i][qwop];
if (qwop < 3)
System.
out.printf(medium[i][qwop] + " | ");
else
System.
out.println(medium[i][qwop] + "\n");
Thread.sleep(150);
}
// System.out.println(temp[i][i]);
random1 = store[i];
if (i == 4)
System.
out.printf("\n\n\n");
}
}
// for loop goes through each of the parameters and compltes the
// instructions
System.
out
.println(
"How would you like to sort?\nA)Name B) Lexile C) Grade Level");
input[2] = ScanObj.nextLine();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
// if statement checks if the condition is true, then does the
// instructions if it is
// compareTo checks the ascii values of two strings. If the
// second
// is smaller (would come first in alphabetic order) it returns
// -1
boolean mismatch = true;
while (mismatch) {
if (input[2].equalsIgnoreCase("a")) {
mismatch =
false;
// makes a new temporary String
String temp1 =
null;
// Switches the String at position i with the one above
// it
for (int z = 0; z < medium.length - 1; z++) {
for (int x = z + 1; x < medium.length; x++) {
if (medium[z][1].compareTo(medium[x][1]) > 0) {
for (int qwop = 0; qwop < 4; qwop++) {
temp1 =
medium[z][qwop];
medium[z][qwop] = medium[x][qwop];
medium[x][qwop] = temp1;
}
mismatch =
true;
}
}
}
}
if (input[2].equalsIgnoreCase("b")) {
mismatch =
false;
// makes a new temporary String
String temp3 =
null;
// Switches the String at position i with the one above
// it
for (int z = 0; z < medium.length - 1; z++) {
for (int x = z + 1; x < medium.length; x++) {
if (medium[z][2].compareTo(medium[x][2]) > 0) {
for (int qwop = 0; qwop < 4; qwop++) {
temp3 =
medium[z][qwop];
medium[z][qwop] = medium[x][qwop];
medium[x][qwop] = temp3;
}
mismatch =
true;
}
}
}
}
if (input[2].equalsIgnoreCase("c")) {
mismatch =
false;
// makes a new temporary String
String temp3 =
null;
// Switches the String at position i with the one above
// it
for (int z = 0; z < medium.length - 1; z++) {
for (int x = z + 1; x < medium.length; x++) {
// Here I encounter an error because compareTo()
// sorts Lexicographically, meaning anything
// starting with a 1 comes before anything
// starting with a 2, 3, etc. This means that
// Grade 11 comes before grade 9, and simply
// comparing greater than does not work due to
// the fact that they are Strings.
if (medium[z][3].compareTo(medium[x][3]) > 0) {
for (int qwop = 0; qwop < 4; qwop++) {
temp3 =
medium[z][qwop];
medium[z][qwop] = medium[x][qwop];
medium[x][qwop] = temp3;
}
mismatch =
true;
}
}
}
}
}
}
}
Thread.sleep(1000);
for (int prt = 0; prt < medium.length; prt++) {
for (int qwop = 0; qwop < 4; qwop++) {
if (qwop < 3)
System.
out.printf(medium[prt][qwop] + " | ");
else
System.
out.println(medium[prt][qwop]);
}
}
Thread.sleep(1800);
for (int t = 0; t < 1000; t++) {
System.
out.println("\n\n\nTo edit a selection, type edit\n"
+
"To sort by a different category, type sort\n"
+
"To browse another medium, type browse\n"
+
"To search the " + medium[1][0] + "s, type search");
input[3] = ScanObj.nextLine();
if (input[3].equalsIgnoreCase("browse")) {
Thread.sleep(500);
System.out.println("What would you like to browse?:");
if (medium[1][0].equals("book")) {
System.out.println("CDs, or DVDs");
input[4] = ScanObj.nextLine();
}
if (medium[1][0].equals("CD")) {
System.out.println("Books, or DVDs");
input[4] = ScanObj.nextLine();
}
if (medium[1][0].equals("DVD")) {
System.out.println("Books, or CDs");
input[4] = ScanObj.nextLine();
}
if (input[4].equalsIgnoreCase("CD")
|| input[4].equalsIgnoreCase("DVD")
|| input[4].equalsIgnoreCase("Book")) {
for (int i = 0; i < 5; i++) {
String temp[][] = new String[5][4];
temp[i][0] =
"CD";
int addon = 0;
if (input[4].equals("CD"))
addon = 5;
if (input[4].equals("DVD"))
addon = 10;
temp[i][1] =
names[i + addon];
temp[i][2] =
lexile[i + addon];
temp[i][3] =
grade[i + addon];
for (int qwop = 0; qwop < 4; qwop++) {
medium[i][qwop] = temp[i][qwop];
if (qwop < 3)
System.
out.printf(medium[i][qwop] + " | ");
else
System.
out.println(medium[i][qwop] + "\n");
Thread.sleep(150);
}
}
}
}
if (input[3].equalsIgnoreCase("edit")) {
Thread.sleep(500);
System.
out.println("First type the number of the " + medium[1][0]
+
"\nyou want to edit");
int input4 = ScanObj.nextInt();
System.
out
.println(
"Now, type the number of the column you want to edit");
int input5 = ScanObj.nextInt();
System.
out.println("Now, make your edit:\n");
input[6] = ScanObj.nextLine();
medium[input4][input5] = input[6];
System.
out.println(medium[input4][input5]);
}
for (int y = 0; y < 10; y++) {
if (input[3].equalsIgnoreCase("search")) {
Thread.sleep(2000);
System.
out.println("Would you like to search by 1) Title or 2) Lexile #");
int input4 = 1;
input4 = ScanObj.nextInt();
Thread.sleep(2500);
System.
out.println("Now Type your search");
input[5] = ScanObj.nextLine();
Thread.sleep(2500);
for (int i = 0; i < 5; i++) {
System.
out.println(input4 + ", " + input[5]);
System.
out.println(medium[i][input4]);
if (input[5].compareToIgnoreCase(medium[i][input4]) == 0) {
y = 10;
System.
out.println("why do you lie");
for (int r = 0; r < 4; r++) {
if (r < 3)
System.
out.printf(medium[i][r] + " | ");
else
System.
out.println(medium[i][r]);
Thread.sleep(150);
}
}
else
System.
out.println(medium[i][input4]);
System.
out.println("Couldn't find item, please search again:");
}
}
}
}
}
}
Where ScanObj is my scanner. I'm just trying to get an Integer input, but it always returns null. What's wrong?
Here's an example using Scanner-
int choice;
do {
System.out.print("Would you like to search by (1) Title or (2) Lexile # : ");
choice = scan.nextInt();
} while(choice < 1 || choice > 2); // Keep asking the user for a valid integer
Above code will break, if the input is not an integer.
You can use nextLine-
Scanner scan = new Scanner(System.in);
String response;
do {
System.out.print("Would you like to search by (1) Title or (2) Lexile # : ");
response = scan.nextLine();
} while(!response.equals("1") && !response.equals("2"));
int choice = Integer.parseInt(response);
try {
System.out.print("Say cheeseeeee...");
Thread.sleep(3000);
} catch(InterruptedException e){
// handle exception
e.printStackTrace();
}
System.out.println("\nChoice : " + choice);
Related
import java.util.Scanner;
public class Note {
public static void main(String[] args) {
String etudiants[][] = new String[1][4];
Scanner saisie = new Scanner(System.in);
for (int i = 0; i < 1; i++) {
System.out.print("\n\nEtudiant BTI00" + (i + 1) + "\n\n");
for (int j = 0; j < 4; j++) {
if (j == 0) {
System.out.print("\n\tCode de l'etudiant : ");
} else if (j == 1) {
System.out.print("\n\tNom etudiant : ");
} else if (j == 2) {
System.out.print("\n\tNote Maths : ");
} else if (j == 3) {
System.out.print("\n\tNote Francais : ");
} else {
System.out.print("\n\tChamps inexistant!");
}
etudiants[i][j] = saisie.nextLine();
}
}
System.out.print("\n\tEtudiants Enregistres : \n\n");
// System.out.print("\tCode\tNom\t\tMaths\tFrancais\n\n");
for (int i = 0; i < 1; i++) {
for (int j = 0; j < 4; j++) {
System.out.print("\t" + etudiants[i][j] + " ");
}
}
System.out.println();
System.out.print("\n\tEntrez code etudiant : ");
String recherche = saisie.nextLine();
boolean trouve = false;
for (int i = 0; i < 1; i++) {
for (int j = 0; j < 4; j++) {
if (recherche.equals(etudiants[i][0])) {
trouve = true;
System.out.print("\n\tCode etudiant correct!");
String math = etudiants[i][2];
String francais = etudiants[i][3];
Double m = new Double(math);
double mathConv = m.doubleValue();
Double f = new Double(francais);
double francaisConv = f.doubleValue();
double moyenne = (mathConv + francaisConv) / 2;
System.out.print("\n\tMoyenne de l'etudiant : " + moyenne);
System.out.print("\n\tEtudiant : " + etudiants[i][j]);
if (moyenne <= 40) {
System.out.print("\n\tEchec!");
} else if (moyenne > 40 && moyenne < 70) {
System.out.print("\n\tReprise!");
} else {
System.out.print("\n\tSucces!");
}
} if (!trouve) {
System.out.print("\n\tCode etudiant incorrect!");
}
}
}
}
}
I need to display only one message after entering the code etudiant but istead it displays the message 4 times. The loop should only iterate through the first column of each line and compares it to what the user entered.
The indexing variable (j) of the last inner for-loop (for (int j = 0; j < 4; j++) {) is never used inside the loop, so you actually do not need that loop at all. General styling issues of your sample aside, you should rewrite the last loop like this:
for (int i = 0; i < 1; i++) {
// throw away this
//for (int j = 0; j < 4; j++) {
if (recherche.equals(etudiants[i][0])) {
trouve = true;
// ... rest of the code like you currently have it
// You probably do not need this line too,
// because you have almost the same in your second loop
//System.out.print("\n\tEtudiant : " + etudiants[i][j]);
}
}
The userGuess[y, x] gets compared to ship1[][] but only updates the board[][] at the matching index with "!" when I enter a user guess that equals (ship1[2][0], ship1[2][1]).
In other words, I feed updateBoard() the int[] userGuess, updateBoard loops through the int[][] ship1 and compares userGuess[0] to ship1[0][0] and userGuess[1] to ship1[0][1]...ship1[2][0], ship1[2][1], looking to see if they match, but the only match I am getting is if I enter a userGuess that equals the values at (ship1[2][0], ship1[2][1]).
Does anyone see what I have done wrong?
package assignment_8;
import java.util.Scanner;
public class Battleship {
private String[][] board = new String[8][8];
private int[][] ship1 = new int[3][2];
private int[][] ship2 = new int[4][2];
private int counter;
Battleship() {
}
public void makeBoard() {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
board[i][j] = "| ";
board[i][7] = "| |";
}
}
}
public void makeShips() {
int ship1Y = (int)Math.floor(Math.random() * 7);
int ship1X = (int)Math.floor(Math.random() * 7);
int ship2Y = (int)Math.floor(Math.random() * 7);
int ship2X = (int)Math.floor(Math.random() * 7);
int coinToss = (int)Math.floor(Math.random() * 2) + 1;
if(coinToss == 1) {
if(ship1X >= 5) {
ship1X = 4;
}
for(int i = 0; i < ship1.length; i++) {
ship1[i][0] = ship1Y;
ship1[i][1] = ship1X + i;
}
if(ship2Y >= 4) {
ship2Y = 3;
}
for(int i = 0; i < ship2.length; i++) {
ship2[i][0] = ship2Y + i;
ship2[i][1] = ship2X;
}
} else {
if(ship1Y >= 5) {
ship1Y = 4;
}
for(int i = 0; i < ship1.length; i++) {
ship1[i][0] = ship1Y + i;
ship1[i][1] = ship1X;
}
if(ship2X >= 4) {
ship2X = 3;
}
for(int i = 0; i < ship2.length; i++) {
ship2[i][0] = ship2Y;
ship2[i][1] = ship2X + i;
}
}
}
public String[][] getBoard() {
return board;
}
public int[][] getShip1() {
return ship1;
}
public int[][] getShip2() {
return ship2;
}
public int getCounter() {
return counter;
}
public void updateBoard(int[] userInput) {
for(int i = 0; i < ship1.length; i++) {
if(ship1[i][0] == userInput[0] && ship1[i][1] == userInput[1] && board[userInput[0]][userInput[1]] == "| ") {
counter++;
} else if (ship1[i][0] == userInput[0] && ship1[i][1] == userInput[1] && board[userInput[0]][userInput[1]] == "| |") {
counter++;
}
if(ship1[i][0] == userInput[0] && ship1[i][1] == userInput[1]) {
board[userInput[0]][userInput[1]] = "|!";
if(userInput[1] == 7) {
board[userInput[0]][userInput[1]] = "|!|";
}
} else {
board[userInput[0]][userInput[1]] = "|X";
if(userInput[1] == 7) {
board[userInput[0]][userInput[1]] = "|X|";
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Battleship bs = new Battleship();
bs.makeBoard();
bs.makeShips();
int[][] ship1 = bs.getShip1();
//int[][] ship2 = bs.getShip2();
for(int i = 0; i < ship1.length; i++) {
System.out.println("[" + ship1[i][0] + "][" + ship1[i][1] + "]");
}
System.out.println();
/*for(int i = 0; i < ship2.length; i++) {
System.out.println("[" + ship2[i][0] + "][" + ship2[i][1] + "]");
}*/
System.out.println();
int[] userGuess = new int[2];
int counter = 0;
String[][] board = bs.getBoard();
do {
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[i].length; j++) {
System.out.print(board[i][j]);
}
System.out.println();
}
counter = bs.getCounter();
System.out.println("Counter = " + counter);
System.out.print("Enter Y coordinate (0 - 7): ");
userGuess[0] = sc.nextInt();
System.out.print("Enter X coordinate(0 - 7): ");
userGuess[1] = sc.nextInt();
bs.updateBoard(userGuess);
System.out.println();
} while(counter < 8);
}
}
The issue is with the following condition check:
if(ship1[i][0] == userInput[0] && ship1[i][1] == userInput[1]) {
board[userInput[0]][userInput[1]] = "|!";
if(userInput[1] == 7) {
board[userInput[0]][userInput[1]] = "|!|";
}
} else {
board[userInput[0]][userInput[1]] = "|X";
if(userInput[1] == 7) {
board[userInput[0]][userInput[1]] = "|X|";
}
}
If the user gives co-ordinates that match with ship1[0][0] and ship[0][1], it assigns ! to it but the loop continues and next two ship1 array values won't match thereby resetting it to X
The fix is to break out of the loop when the match is found.
if(ship1[i][0] == userInput[0] && ship1[i][1] == userInput[1]) {
board[userInput[0]][userInput[1]] = "|!";
if(userInput[1] == 7) {
board[userInput[0]][userInput[1]] = "|!|";
}
break; // that's it!
}
The code shows:
java.util.IllegalFormatConversionException: d != java.lang.String at
java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302)
at
java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2793)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2747) a
public class addiePorterMod10Sieve {
void sieveOfEratosthenes(int n) {
boolean prime[] = new boolean[n + 1];
for (int i = 0; i < n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
for (int i = 2; i <= n; i++) {
if (prime[i] == true)
System.out.printf(i + "%-1s %-15n", " ");
}
}
public static void main(String args[]) {
int n = 1000;
addiePorterMod10Sieve g = new addiePorterMod10Sieve();
g.sieveOfEratosthenes(n);
}
}
Not very familiar with formatter myself, but the below workaround should achieve what you need as ouput :
int linecount = 0;
for(int i = 2; i <= n; i++)
{
if(prime[i] == true) {
// System.out.printf(i + "%-1s %-15d", " ");
linecount++;
System.out.print(i + " ");
if (linecount == 15) {
linecount =0;
System.out.println();
}
}
}
The error derives from %'s matching with a parameter.
int p = 0;
String nl = "\r\n";
for (int i = 2; i <= n; i++) {
if (prime[i]) {
++p;
System.out.printf("%-15d ", i);
if (p % 10 == 0) {
System.out.println();
}
//System.out.printf("%-15d%s", i, (p % 10 == 0 ? nl : " "));
}
}
Now %n would indeed yield a newline ("\r\n" on Windows, "\n" on Linux) with a flush of the line. However you must place it in the format string,
My out-commented alternative misses immediate flushing to the console.
I'm trying to write a code that multiplies two strings of integers. I'm not too sure where it's going wrong... It works for some numbers, but is horribly wrong for others. I'm not asking for a full solution, but just a hint (I seriously appreciate any help possible) as to where I'm making the obviously silly mistake. Thanks in advance.
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Please enter a big integer. ");
String t = scan.nextLine();
System.out.print("And another. ");
String s = scan.nextLine();
BigInt a = new BigInt(t);
BigInt b = new BigInt(s);
System.out.println(a + " + " + b + " = " + a.add(b));
System.out.println(a + " - " + b + " = " + a.sub(b));
System.out.println(a + " * " + b + " = " + a.mul(b));
System.out.println(a + " / " + b + " = " + a.div(b));
}
}
class BigInt {
public BigInt() {
n = new int[1];
}
public BigInt(String s) {
n = new int[s.length()];
for (int i = 0; i < n.length; ++i) {
n[n.length - i - 1] = s.charAt(i) - '0' ;
}
}
private BigInt(int[] n) {
this.n = new int[n.length];
for (int i = 0; i < n.length; ++i) {
this.n[i] = n[i];
}
}
public String toString() {
String s = "";
for (int i : n) {
s = i + s;
}
return s;
}
public BigInt mul(BigInt o) {
int carry = 0;
int s = 0;
int digit;
int subtotal = 0;
int total = 0;
int max = n.length > o.n.length ? n.length : o.n.length;
int[] result = new int[n.length + o.n.length];
for (int i = 0; i < o.n.length; ++i) {
int bottom = i <= o.n.length ? o.n[i] : 0;
for (s = 0; s <= n.length; ++s){
int top = s < n.length ? n[s] : 0;
int prod = (top * bottom + carry);
if (s == (max-1)) {
total = Integer.valueOf((String.valueOf(prod) + String.valueOf(subtotal)));
carry = 0;
digit = 0;
subtotal = 0;
break;
}
if (prod < 10) {
digit = prod;
subtotal += digit;
carry = 0;
}
if (prod >= 10); {
digit = prod % 10;
carry = prod / 10;
subtotal += digit;
}
}
result[i] = total;
}
return new BigInt(trim(result));
}
private int[] trim(int[] nums) {
int size = nums.length;
for (int i = nums.length - 1; i > 0; --i) {
if (nums[i] != 0) {
break;
}
--size;
}
int[] res = new int[size];
for (int i = 0; i < size; ++i) {
res[i] = nums[i];
}
return res;
}
private int[] n;
}
A quick test using:
for (int x = 0; x < 10; x++) {
for (int y = 0; y < 10; y++) {
System.out.println(x + " * " + y + " = " + new BigInt(Integer.toString(x)).mul(new BigInt(Integer.toString(y))));
}
}
demonstrates that somehow your multiply of x * y is actually multiplying by 10x * y. That should give you a clear hint to the problem.
I need some help in this program. I have the whole program done and set. I am having trouble in the printf statement as everytime I run the program, I get an IllegalFormatConversionException error even though the program compiles flawlessly.
This is the printf in question:
System.out.printf("%s%20d%10d%10.1f", "Average", categoryAvg, pressureAvg, speedMPHAvg);
Code:
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class Hurricanes2
{
public static void main(String[] args) throws IOException
{
int arrayLength = 59;
int [] year = new int[arrayLength];
String [] month = new String[arrayLength];
int [] pressure = new int[arrayLength];
int [] speed = new int[arrayLength];
String [] name = new String[arrayLength];
double [] speedMPH = new double[arrayLength];
int [] category = new int[arrayLength];
int c1 = 0;
int c2 = 0;
int c3 = 0;
int c4 = 0;
int c5 = 0;
int categoryMin = 0;
int categoryMax = 0;
int speedMin = 0;
int speedMax = 0;
int pressureMin = 0;
int pressureMax = 0;
int categorySum = 0;
int speedMPHSum = 0;
int pressureSum = 0;
double categoryAvg = 0;
double speedMPHAvg = 0;
double pressureAvg = 0;
File fileName = new File("hurcdata2.txt");
Scanner inFile = new Scanner(fileName);
int index = 0;
while (inFile.hasNext())
{
year[index] = inFile.nextInt();
month[index] = inFile.next();
pressure[index] = inFile.nextInt();
speed[index] = inFile.nextInt();
name[index] = inFile.next();
index++;
}
inFile.close();
for(index = 0; index < arrayLength; index++)
{
speedMPH [index] = (speed[index]) * 1.15;
if(speedMPH[index] >= 74 && speedMPH[index] <= 95)
{
category[index] = 1;
c1++;
}
else if(speedMPH[index] >= 96 && speedMPH[index] <= 110)
{
category[index] = 2;
c2++;
}
else if(speedMPH[index] >= 111 && speedMPH[index] <= 130)
{
category[index] = 3;
c3++;
}
else if(speedMPH[index] >= 131 && speedMPH[index] <= 155)
{
category[index] = 4;
c4++;
}
else
{
category[index] = 5;
c5++;
}
}
for(index = 0; index < arrayLength; index++)
{
categoryMin = category[index];
if(category[index] < categoryMin)
{
category[index] = categoryMin;
}
categoryMax = category[index];
if(category[index] > categoryMax)
{
category[index] = categoryMax;
}
speedMin = speed[index];
if(speed[index] < speedMin)
{
speed[index] = speedMin;
}
speedMax = speed[index];
if(speed[index] > speedMax)
{
speed[index] = speedMax;
}
pressureMin = pressure[index];
if(pressure[index] < pressureMin)
{
pressure[index] = pressureMin;
}
pressureMax = pressure[index];
if(pressure[index] > pressureMax)
{
pressure[index] = pressureMax;
}
categorySum += category[index];
speedMPHSum += speedMPH[index];
pressureSum += pressure[index];
}
categoryAvg = (double)categorySum / arrayLength;
speedMPHAvg = (double)speedMPHSum / arrayLength;
pressureAvg = (double)pressureSum / arrayLength;
System.out.printf("%40s", "Hurricanes 1980 - 2006");
System.out.println();
System.out.println();
System.out.printf("%s%10s%10s%10s%10s", "Year", "Hurricane", "Category", "Pressure (mb)", "Wind Speed (mph)");
System.out.println();
System.out.println("=====================================================================");
for(index = 0; index < arrayLength; index++)
{
System.out.printf("%d%10s%10d%10d%10f", year[index], name[index], category[index], pressure[index], speedMPH[index]);
System.out.println();
}
System.out.println("=====================================================================");
System.out.println();
System.out.printf("%s%20d%10d%10.1f", "Average", categoryAvg, pressureAvg, speedMPHAvg);
System.out.println();
System.out.printf("%s%20d%10d%10.1f", "Maximum", categoryMax, pressureMax, speedMax);
System.out.println();
System.out.printf("%s%20d%10d%10.1f", "Minimum", categoryMin, pressureMin, speedMin);
System.out.println();
System.out.println("Number of Category 1 Hurricanes: " + c1);
System.out.println("Number of Category 2 Hurricanes: " + c2);
System.out.println("Number of Category 3 Hurricanes: " + c3);
System.out.println("Number of Category 4 Hurricanes: " + c4);
System.out.println("Number of Category 5 Hurricanes: " + c5);
}//end main()
}//end Hurricanes2
In printf, d is for integral types only, i.e. byte, short, int and long, not double.
Since categoryAvg and pressureAvg are double's, use f for them instead.
System.out.printf("%s%20f%10f%10.1f", "Average", categoryAvg, pressureAvg, speedMPHAvg);
And similarly f is not for int.
So, since speedMax and speedMin are int's, you should use d there instead:
System.out.printf("%s%20d%10d%10d", "Maximum", categoryMax, pressureMax, speedMax);
System.out.printf("%s%20d%10d%10d", "Minimum", categoryMin, pressureMin, speedMin);
(or change the types of your variables appropriately).