Scanner issue: No such element exception - java

I am a novice with Java, and I am experiencing an error in method userInput, line 16, which throws a no such element exception. I need to be able to pass the values of both 'before' and 'after.' Thanks! Here is the code:
import java.util.Scanner;
public class BasketballGame {
public static String nameFirstTeam = "Bulldogs";
public static String nameSecondTeam = "Wildcats";
private static String before, character1, character2, after;
public static int scoreFirstTeam = 0;
public static int scoreSecondTeam = 0;
public int scoreOne, scoreTwo, scoreThree;
//determine end of game
//System.out.println("Enter the team that scored ('a' for Bulldogs, 'b' for Wildcats), then enter the number of points scored.");
public static void userInput(){
Scanner keyboard = new Scanner(System.in);
//keyboard.useDelimiter(" ");
character1 = keyboard.next();
before = String.valueOf(character1.charAt(0));
//System.out.println(before);
character2 = keyboard.next();
after = String.valueOf(character1.charAt(1));
//System.out.println(after);
}
public static boolean finished(){
if (scoreFirstTeam >= 50){
return true;
}
else if (scoreSecondTeam >= 50){
return true;
}
else{
return false;}
}
public static void playGame(){
while(!finished()){
if (before== "a"){
System.out.println("a");
}
if (after=="1"){
System.out.println("1");
}
System.out.println("Enter a score.");
userInput();
if (before=="a" && after=="1"){
scoreFirstTeam = (scoreFirstTeam +1);
if (scoreFirstTeam > scoreSecondTeam){
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameFirstTeam + " are winning.");
}
else if (scoreFirstTeam < scoreSecondTeam){
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameSecondTeam + " are winning.");
}
else{
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + "; The game is tied.");
}
}
else if (before=="a" && after=="2"){
scoreFirstTeam = (scoreFirstTeam +2);
if (scoreFirstTeam > scoreSecondTeam){
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameFirstTeam + " are winning.");
}
else if (scoreFirstTeam < scoreSecondTeam){
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameSecondTeam + " are winning.");
}
else{
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + "; The game is tied.");
}
}
else if (before=="a" && after=="3"){
scoreFirstTeam = (scoreFirstTeam +3);
if (scoreFirstTeam > scoreSecondTeam){
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameFirstTeam + " are winning.");
}
else if (scoreFirstTeam < scoreSecondTeam){
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameSecondTeam + " are winning.");
}
else{
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + "; The game is tied.");
}
}
else if (before=="b" && after=="1"){
scoreSecondTeam = (scoreSecondTeam +1);
if (scoreFirstTeam > scoreSecondTeam){
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameFirstTeam + " are winning.");
}
else if (scoreFirstTeam < scoreSecondTeam){
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameSecondTeam + " are winning.");
}
else{
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + "; The game is tied.");
}
}
else if (before=="b" && after=="2"){
scoreSecondTeam = (scoreSecondTeam +2);
if (scoreFirstTeam > scoreSecondTeam){
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameFirstTeam + " are winning.");
}
else if (scoreFirstTeam < scoreSecondTeam){
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameSecondTeam + " are winning.");
}
else{
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + "; The game is tied.");
}
}
else {
scoreSecondTeam = (scoreSecondTeam +3);
if (scoreFirstTeam > scoreSecondTeam){
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameFirstTeam + " are winning.");
}
else if (scoreFirstTeam < scoreSecondTeam){
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameSecondTeam + " are winning.");
}
else{
System.out.println(nameFirstTeam + " " + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + "; The game is tied.");
}
}
}
System.out.println("");
}
public static void main(String[] args){
BasketballGame game = new BasketballGame();
game.playGame();
while (finished()){
if (scoreFirstTeam > scoreSecondTeam){
System.out.println(nameFirstTeam + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameFirstTeam + " win!");
}
else if (scoreFirstTeam < scoreSecondTeam){
System.out.println(nameFirstTeam + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + ";" + nameSecondTeam + " win!");
}
else{
System.out.println(nameFirstTeam + scoreFirstTeam + "," + nameSecondTeam + scoreSecondTeam + "; The game is a draw.");
}
}
}
}

Related

Cannot Access Variable inside the for loop

I'm trying to concatenate string values from clientGroupList to make a string variable. I cannot access to the returned variable. I'm working on shareString_8 and shareString_9.
if (uib.getClientProviderType().equals(ParamConstant.CLIENT_TYPE)) {
// ClientUserの場合の処理
//SM4|
String shareString_8 = shareStringList + ":SM" + Constant.SHARE_MODE_CLIENT_GROUP + "|";
for (int idx = 0; idx < clientGroupList.size(); idx++) {
//SM4|SHARE_CLIENT_GROUP
shareString_8 = shareString_8 + clientGroupList.get(idx);
}
return shareString_8;
} else {
// ProviderUserの場合の処理
//SM-1|
String shareString_9 = shareStringList + ":SM" + Constant.SHARE_MODE_PUBLISH + "|";
for (int idx = 0; idx < entityListClientGroup.size(); idx++) {
//SM-1|CLIENT_GROUP_ID
shareString_9 = shareString_9 + clientGroupList.get(idx);
}
return shareString_9;
}
String shareString = providerId + " AND " + "(" + shareString_1 + " OR " + shareString_2 + " OR " + shareString_3
+ " OR " + shareString_4 + " OR " + shareString_5 + " OR " + shareString_6 + " OR " + shareString_7
+ " OR " + shareString_8 + " OR " + shareString_9 + ")";

random walk across 10x10 grid array

The purpose of this program is to build a 10x10 array grid made out of periods (.), allow the user to designate a starting point, and then the program will randomly choose numbers which are assigned to directions for the 'walker' to go. Each time it moves it marks its spot with the next letter in the alphabet (the starting point is marked by A). I haven't done this part yet but I know how I will; if the walker crosses out of the bounds of the array (AKA > 10 or < 0) it will say "you were arrested" and if the variable alpha == 'Z' it way say "You made it home".
The main problem as far as I can tell is with the method processing(), where I draw numbers from getRand (which I have confirmed is working) and then assign them directions which are given to the array. But for some reason when it prints the grid it only shows the starting point, and not the movement of the walker.
Another question I have of lesser importance is if there is a more brief way to display my grid than how I have been (A massive brick of text in a System.out.print)
package walktester;
import java.lang.Math;
import java.util.Random;
import java.util.Scanner;
class DrunkWalker {
private char[][] walkgrid = new char[10][10];
private static int randNSEW;
private int randomnum;
private int startrow;
private int startcol;
private char alpha = 'A';
private int nextrow;
private int nextcol;
public DrunkWalker(int r, int c) {
startrow = r;
startcol = c;
nextrow = startrow;
nextcol = startcol;
for (int i = 0; i < 10; i ++) {
for (int j = 0; j < 10; j++)
walkgrid[i][j] = '.';
}
walkgrid[r][c] = alpha;
}
public static void getRand(){
int x100 = 0;
double randomNum = 0.0;
randomNum = Math.random();
x100 = (int) (randomNum * 100);
randNSEW = x100 % 4;
}
public int getNextRow(){
return nextrow;
}
public int getNextCol(){
return nextcol;
}
public void processing(){
for(int i = 0; i < 26; i ++){
getRand();
if(randNSEW == 0){
nextcol--;
walkgrid[nextrow][nextcol] = alpha++;
}
if(randNSEW == 1){
nextrow++;
walkgrid[nextrow][nextcol] = alpha++;
}
if(randNSEW == 2){
nextcol++;
walkgrid[nextrow][nextcol] = alpha++;
}
if(randNSEW == 3){
nextrow--;
walkgrid[nextrow][nextcol] = alpha++;
}
}
}
public char[][] DisplayGrid() {
System.out.print(
walkgrid[0][0] + " " + walkgrid[0][1] + " " + walkgrid[0][2] + " " + walkgrid[0][3] + " " + walkgrid[0][4] + " " + walkgrid[0][5] + " " + walkgrid[0][6] + " " + walkgrid[0][7] + " " + walkgrid[0][8] + " " + walkgrid[0][9] + "\n" +
walkgrid[1][0] + " " + walkgrid[1][1] + " " + walkgrid[1][2] + " " + walkgrid[1][3] + " " + walkgrid[1][4] + " " + walkgrid[1][5] + " " + walkgrid[1][6] + " " + walkgrid[1][7] + " " + walkgrid[1][8] + " " + walkgrid[1][9] + "\n" +
walkgrid[2][0] + " " + walkgrid[2][1] + " " + walkgrid[2][2] + " " + walkgrid[2][3] + " " + walkgrid[2][4] + " " + walkgrid[2][5] + " " + walkgrid[2][6] + " " + walkgrid[2][7] + " " + walkgrid[2][8] + " " + walkgrid[2][9] + "\n" +
walkgrid[3][0] + " " + walkgrid[3][1] + " " + walkgrid[3][2] + " " + walkgrid[3][3] + " " + walkgrid[3][4] + " " + walkgrid[3][5] + " " + walkgrid[3][6] + " " + walkgrid[3][7] + " " + walkgrid[3][8] + " " + walkgrid[3][9] + "\n" +
walkgrid[4][0] + " " + walkgrid[4][1] + " " + walkgrid[4][2] + " " + walkgrid[4][3] + " " + walkgrid[4][4] + " " + walkgrid[4][5] + " " + walkgrid[4][6] + " " + walkgrid[4][7] + " " + walkgrid[4][8] + " " + walkgrid[4][9] + "\n" +
walkgrid[5][0] + " " + walkgrid[5][1] + " " + walkgrid[5][2] + " " + walkgrid[5][3] + " " + walkgrid[5][4] + " " + walkgrid[5][5] + " " + walkgrid[5][6] + " " + walkgrid[5][7] + " " + walkgrid[5][8] + " " + walkgrid[5][9] + "\n" +
walkgrid[6][0] + " " + walkgrid[6][1] + " " + walkgrid[6][2] + " " + walkgrid[6][3] + " " + walkgrid[6][4] + " " + walkgrid[6][5] + " " + walkgrid[6][6] + " " + walkgrid[6][7] + " " + walkgrid[6][8] + " " + walkgrid[6][9] + "\n" +
walkgrid[7][0] + " " + walkgrid[7][1] + " " + walkgrid[7][2] + " " + walkgrid[7][3] + " " + walkgrid[7][4] + " " + walkgrid[7][5] + " " + walkgrid[7][6] + " " + walkgrid[7][7] + " " + walkgrid[7][8] + " " + walkgrid[7][9] + "\n" +
walkgrid[8][0] + " " + walkgrid[8][1] + " " + walkgrid[8][2] + " " + walkgrid[8][3] + " " + walkgrid[8][4] + " " + walkgrid[8][5] + " " + walkgrid[8][6] + " " + walkgrid[8][7] + " " + walkgrid[8][8] + " " + walkgrid[8][9] + "\n" +
walkgrid[9][0] + " " + walkgrid[9][1] + " " + walkgrid[9][2] + " " + walkgrid[9][3] + " " + walkgrid[9][4] + " " + walkgrid[9][5] + " " + walkgrid[9][6] + " " + walkgrid[9][7] + " " + walkgrid[9][8] + " " + walkgrid[9][9] + "\n"
);
return walkgrid;
}
}
public class WalkTester {
public static void main(String[] args) {
Scanner inpr = new Scanner(System.in);
Scanner inpc = new Scanner(System.in);
Scanner inpchoice = new Scanner(System.in);
int r = 0;
int c = 0;
char choice = 'y';
while(choice == 'y' || choice == 'Y') {
System.out.println("Please enter x coordinate between 1 and 10.");
r = inpr.nextInt();
r = r - 1;
System.out.println("Please enter y coordinate between 1 and 10");
c = inpr.nextInt();
c = c - 1;
if(r < 0 || r > 9 || c < 0 || c > 9){
System.out.println("Invalid Entry. Restart? y/n");
choice = inpchoice.next().charAt(0);
if(choice == 'y' || choice == 'Y'){
continue;
}
else if(choice == 'n' || choice == 'N'){
return;
}
else{
System.out.println("Invalid Entry. Restart? y/n");
choice = inpchoice.next().charAt(0);
}
}
DrunkWalker drunkwalker = new DrunkWalker(r, c);
drunkwalker.DisplayGrid();
System.out.println("Restart? y/n");
choice = inpchoice.next().charAt(0);
if(choice == 'y' || choice == 'Y'){
continue;
}
else if(choice == 'n' || choice == 'N'){
return;
}
else{
System.out.println("Invalid Entry. Restart? y/n");
choice = inpchoice.next().charAt(0);
}
}
}
}
The code that moves the walker is in a method called processing(), but you never call it.
You can make your DisplayGrid() method simpler (and it will still print the same stuff):
public char[][] DisplayGrid() {
for(int y = 0; y < 10; y++) {
for(int x = 0; x < 10; x++) {
System.out.print(walkgrid[x][y] + " ");
}
System.out.println();
}
return walkgrid;
}
Your processing() method needs to check if the walker leaves the bounds of the area. If you don't, you'll get an ArrayIndexOutOfBoundsException.
public boolean processing(){
for(int i = 0; i < 26; i ++){
getRand();
if(randNSEW == 0){
nextcol--;
}
if(randNSEW == 1){
nextrow++;
}
if(randNSEW == 2){
nextcol++;
}
if(randNSEW == 3){
nextrow--;
}
if(nextrow < 0 || nextrow >= 10 || nextcol < 0 || nextcol >= 10) {
return false;
}
walkgrid[nextrow][nextcol] = alpha++;
}
return true;
}
Now you need to call processing() and check the return value to see if the walker succeeded:
DrunkWalker drunkwalker = new DrunkWalker(r, c);
boolean walkerSucceeded = drunkwalker.processing();
drunkwalker.DisplayGrid();
if(walkerSucceeded) {
System.out.println("You made it home");
} else {
System.out.println("You were arrested");
}
And keep in mind (as you will see when you test this), that the walker can cross their own tracks (so you might see some letters missing).

Convert numbers in an array to another array

I'm trying to create another array but I want it to be the array double gainT[], which is the temperature in Fahrenheit, converted to Celcius to make another array called double gainTC[]. I am also trying to do the same thing but with the precipitation. I just can't seem to find a way to do it though.
import java.io.IOException;
import java.util.Scanner;
public class AnnualClimate1 {
public static void main(String [] args) throws IOException
{
Scanner in = new Scanner(System.in);
System.out.print("Choose the temperature Scale (F = Fahrenheit, C = Celsius): ");
String tFC = in.nextLine();
System.out.print("Choose the precipitation Scale (I = Inches, C = Centimeters): ");
String pIC = in.nextLine();
System.out.println("");
System.out.println("");
System.out.println(" Climate Data");
System.out.println(" Location: Gainesville, Florida");
System.out.println(" Temperature " + tFC + " Precipitation " + pIC);
System.out.println("=================================================");
double gainT[]={54.3, 57.0, 62.5, 67.6, 74.3, 79.2, 80.9, 80.4, 77.8, 70.1, 62.8, 56.3};
double gainTC[] = {(gainT[] - 32) / 1.8};
double gainP[]={3.5, 3.4, 4.3, 2.9, 3.2, 6.8, 6.1, 6.6, 4.4, 2.5, 2.2, 2.6};
double gainPC[] = {gainP[] / .3937};
if(tFC.equalsIgnoreCase("F") && pIC.equalsIgnoreCase("I")){
System.out.println("Jan. " + gainT[1] + " " + gainP[1]);
System.out.println("Feb. " + gainT[2] + " " + gainP[2]);
System.out.println("Mar. " + gainT[3] + " " + gainP[3]);
System.out.println("Apr. " + gainT[4] + " " + gainP[4]);
System.out.println("May " + gainT[5] + " " + gainP[5]);
System.out.println("Jun. " + gainT[6] + " " + gainP[6]);
System.out.println("Jul. " + gainT[7] + " " + gainP[7]);
System.out.println("Aug. " + gainT[8] + " " + gainP[8]);
System.out.println("Sep. " + gainT[9] + " " + gainP[9]);
System.out.println("Oct. " + gainT[10] + " " + gainP[10]);
System.out.println("Nov. " + gainT[11] + " " + gainP[11]);
System.out.println("Dec. " + gainT[12] + " " + gainP[12]);
}
else if(tFC.equalsIgnoreCase("C") && pIC.equalsIgnoreCase("C")){
System.out.println("Jan. " + gainTC[1] + " " + gainPC[1]);
System.out.println("Feb. " + gainTC[2] + " " + gainPC[2]);
System.out.println("Mar. " + gainTC[3] + " " + gainPC[3]);
System.out.println("Apr. " + gainTC[4] + " " + gainPC[4]);
System.out.println("May " + gainTC[5] + " " + gainPC[5]);
System.out.println("Jun. " + gainTC[6] + " " + gainPC[6]);
System.out.println("Jul. " + gainTC[7] + " " + gainPC[7]);
System.out.println("Aug. " + gainTC[8] + " " + gainPC[8]);
System.out.println("Sep. " + gainTC[9] + " " + gainPC[9]);
System.out.println("Oct. " + gainTC[10] + " " + gainPC[10]);
System.out.println("Nov. " + gainTC[11] + " " + gainPC[11]);
System.out.println("Dec. " + gainTC[12] + " " + gainPC[12]);
}
}
}
Here is an incomplete example so you can fill in the blanks yourself:
double gainT[]={54.3, 57.0, 62.5, 67.6, 74.3, 79.2, 80.9, 80.4, 77.8, 70.1, 62.8, 56.3};
double gainTC[] = new double[gainT.length]; //create array which matches the size of gainT
//array.length is a property value returning the 'size' or length or the array
//Now just iterate through all the gainT[] values you populated above and read each one
for(int i = 0; i < gainT.length; i++){
double math = //use your conversion math here, and store the value
gainTC[i] = math; //assign the results of your math to the same spot in the new array
}
If you'd like more information on Loops and Arrays, check here:
For Loops: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
Arrays: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Adding random arrays to random arrays in java to make it equal another array

I am learning java, and from what I can tell, what I am looking to do is a rare situation.
I am trying to use an API (kindof) to randomly generate musical notes. I want it to generate 20 times so i have it in a for loop. I realize that i could have used a list for this I just dont know how I could have implemented it. The question I have is, when I try to compile this code, the first part runs. It lets me make the seed. However after that it gives me
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at co.kbcomp.music.Main.main(Main.java:64)
What could I do to prevent this? I know that what I am doing is wrong. That much I dont need to be told. What I want to know is where am I going wrong.
package co.kbcomp.music;
import java.util.*;
import org.jfugue.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Please enter a number for the seed of the song");
long seed = in.nextLong();
Calendar cal = Calendar.getInstance();
Random rand;
//rand = new Random(cal.getTime());
rand = new Random(seed);
int NoteNumber = 0;
int NoteLength = 0;
int OctiveNumber = 0;
int ChordNumber = 0;
int InversionNumber = 0;
//int Duration = rand.nextInt(100 - 5) + 5;
//This keeps track of the iteration of the for loop.
String[] NN = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
String[] NL = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
String[] IN = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
String[] CN = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
String[] ON = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
//This is what is being played
String[] note = { "A", "B", "C", "D", "E", "F", "G" };
String[] noteLength = {"", "w", "", "h", "", "q","", "i"};
String[] octive = { "","1","", "2", "", "3", "", "4", "", "5", "", "6", "", "7", "", "8", "", "9"};
String[] chord = { "", "maj", "", "min"};
String[] inversion = {"", "^", "", "^^", "", "^^^", "", "^^^^", "", "^^^^^"};
String[] key = {"",""};
String keys= " ";
String randstr = " ";
//this is the loop that defines the music legnth
for (int i = 0; i < 21; i++) {
NoteNumber = rand.nextInt(7);
NoteLength = rand.nextInt(8);
OctiveNumber = rand.nextInt(18);
ChordNumber = rand.nextInt(4);
InversionNumber = rand.nextInt(10);
NN[i] = note[NoteNumber]; // This randomly generates the note to be played.
NL[i] = noteLength[NoteLength]; // This randomly generates the length of the note.
ON[i] = octive[OctiveNumber]; // This defines the octive to be played in.
CN[i] = chord[ChordNumber]; // This is defines the major or the minor
IN[i] = inversion[InversionNumber]; // IN[i] = inversion[InversionNumber];
key[i] = NN[i] + NL[i] + ON[i] + CN[i] + IN[i];
//randstr = c[0] + " " + c[1] + " " + c[2] + " " + c[3] + " " + c[4] + " " + c[5] + " " + c[6] + " " + c[7] + " " + c[8] + " " + c[9] + " " + c[10] + " " + c[11] + " " + c[12] + " " + c[13] + " " + c[14] + " " + c[15] + " " + c[16];
keys = (key[0] + " " + key[1] + " " + key[2] + " " + key[3] + " " + key[4] + " " + key[5] + " " + key[6] + " " + key[7] + " " + key[8] + " " + key[9] + " " + key[10] + " " + key[11] + " " + key[12] + " " + key[13] + " " + key[14] + " " + key[15] + " " + key[16] + " " + key[17] + " " + key[18] + " " + key[19] + " " + key[20]);
}
System.out.println(key);
Player player = new Player();
Pattern pattern = new Pattern(key[0]);
player.play(pattern);
}
}
You declare your key array with a length of two:
String[] key = {"",""};
But then later in your for loop, you try to access elements beyond the length of your array:
keys = (key[0] + " " + key[1] + " " + key[2] + " " + key[3] + " " + key[4] +
" " + key[5] + " " + key[6] + " " + key[7] + " " + key[8] + " " + key[9] +
" " + key[10] + " " + key[11] + " " + key[12] + " " + key[13] + " " +
key[14] + " " + key[15] + " " + key[16] + " " + key[17] + " " + key[18] +
" " + key[19] + " " + key[20]);
Since your array has only a length of two, when you try to access the third element (at array index 2), you get an ArrayIndexOutOfBoundsException.
String[] key = {"",""};
...
for (int i = 0; i < 21; i++) {
...
key[i] = ....
Do you see the problem?
As you array in only 20 in length
then this
for (int i = 0; i < 21; i++) {
is going to cause an overflow
It should be < 20
Plus this code is meaningless as your key is only an array of 2
keys = (key[0] + " " + key[1] + " " + key[2] + " " + key[3] + " " + key[4] + " " + key[5] + " " + key[6] + " " + key[7] + " " + key[8] + " " + key[9] + " " + key[10] + " " + key[11] + " " + key[12] + " " + key[13] + " " + key[14] + " " + key[15] + " " + key[16] + " " + key[17] + " " + key[18] + " " + key[19] + " " + key[20]);

Programming database with java

I notice that when i sent data to the database from a GUI I have created it is save there,but when I try to recall it nothing is happening.
I also notice that the result set which was created only retrieve records that were save when java made connection to the database,e.g. if say 500 records are in a particular database table when the result set was made only those 500 records can be retrieved no additional records when added can be retrieved.
I have try everything I am desperate need some help or point me in the right direction
Here is the code snippet for the button that is responsible to retrieved records
JButton btncanel = new JButton("Submit");
btncanel.setBounds(820, 300, 80, 30);
btncanel.setFont(new Font("Times New Roman", Font.BOLD,12));
panel.add(btncanel);
btncanel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (scat3.getSelectedIndex() == 1) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String FILE = "jdbc:odbc:Usermbj";
Connection pcon = DriverManager.getConnection(FILE);
java.sql.Statement pt = pcon.createStatement();
ResultSet result = pt.executeQuery("SELECT * FROM Airlines2");
while (result.next()) {
String LN = result.getString(3);
String FN = result.getString(4);
String COMP = result.getString(12);
int RAPnum = result.getInt(1);
String EH = result.getString(2);
String FRP = result.getString(5);
String ROD = result.getString(6);
int RS = result.getInt(7);
String OD = result.getString(8);
int OS = result.getInt(9);
String COOD =result.getString(10);
String AS = result.getString(11);
if ((scat2.getSelectedIndex() == 1 && crit.getText().equals(LN))
&& (scat4.getSelectedIndex() == 1 && c.getText().equals(FN))) {
JOptionPane.showMessageDialog(null,
"Employment History----------" + " " + " " + EH + "\n\n"
+ "Rap Code----------" + " " + " " + RAPnum + "\n\n"
+ "First Name----------" + " " + " " + FN + "\n\n"
+ "Last Name----------" + " " + " " + LN + "\n\n"
+ "Finger Print Record----------" + " " + " " + FRP + "\n\n"
+ "Re-Orientation Date----------" + " " + " " + ROD + "\n\n"
+ "Re-Orientation Score----------" + " " + " " + RS + "\n\n"
+ "Oreintation Date----------" + " " + " " + OD + "\n\n"
+ "Orientation Score----------" + " " + " " + OS + "\n\n"
+ "Coodintors----------" + " " + " " + COOD + "\n\n"
+ "Applicant Gender----------" + " " + " " + AS + "\n\n"
+ "Company----------" + " " + " " + COMP);
lnam.setText(LN);
fnam.setText(FN);
comp.setText(COMP);
empn.setText(String.valueOf(RAPnum));
scat.setText(EH);
fpr1.setText(FRP);
reod.setText(ROD);
reos.setText(String.valueOf(RS));
od.setText(OD);
os.setText(String.valueOf(OS));
cood.setText(COOD);
scat1.setText(AS);
}
if ((scat2.getSelectedIndex() == 1 && crit.getText().equals(LN))
&& (c.getText().equals(""))) {
JOptionPane.showMessageDialog(null,
"Employment History----------" + " " + " " + EH + "\n\n"
+ "Rap Code----------" + " " + " " + RAPnum + "\n\n"
+ "First Name----------" + " " + " " + FN + "\n\n"
+ "Last Name----------" + " " + " " + LN + "\n\n"
+ "Finger Print Record----------" + " " + " " + FRP + "\n\n"
+ "Re-Orientation Date----------" + " " + " " + ROD + "\n\n"
+ "Re-Orientation Score----------" + " " + " " + RS + "\n\n"
+ "Oreintation Date----------" + " " + " " + OD + "\n\n"
+ "Orientation Score----------" + " " + " " + OS + "\n\n"
+ "Coodintors----------" + " " + " " + COOD + "\n\n"
+ "Applicant Gender-----------" + " " + " " + AS + "\n\n"
+ "Company----------" + " " + " " + COMP);
lnam.setText(LN);
fnam.setText(FN);
comp.setText(COMP);
empn.setText(String.valueOf(RAPnum));
scat.setText(EH);
fpr1.setText(FRP);
reod.setText(ROD);
reos.setText(String.valueOf(RS));
od.setText(OD);
os.setText(String.valueOf(OS));
cood.setText(COOD);
scat1.setText(AS);
}
if ((scat4.getSelectedIndex() == 1 && c.getText().equals(FN))
&&(crit.getText().equals(""))) {
JOptionPane.showMessageDialog(null,
"Employment History----------" + " " + " " + EH + "\n\n"
+ "Rap Code----------" + " " + " " + RAPnum + "\n\n"
+ "First Name----------" + " " + " " + FN + "\n\n"
+ "Last Name----------" + " " + " " + LN + "\n\n"
+ "Finger Print Record----------" + " " + " " + FRP + "\n\n"
+ "Re-Orientation Date----------" + " " + " " + ROD + "\n\n"
+ "Re-Orientation Score----------" + " " + " " + RS + "\n\n"
+ "Oreintation Date----------" + " " + " " + OD + "\n\n"
+ "Orientation Score----------" + " " + " " + OS + "\n\n"
+ "Coodintors-----------" + " " + " " + COOD + "\n\n"
+ "Applicant Gender----------" + " " + " " + AS + "\n\n"
+ "Company----------" + " " + " " + COMP);
lnam.setText(LN);
fnam.setText(FN);
comp.setText(COMP);
empn.setText(String.valueOf(RAPnum));
scat.setText(EH);
fpr1.setText(FRP);
reod.setText(ROD);
reos.setText(String.valueOf(RS));
od.setText(OD);
os.setText(String.valueOf(OS));
cood.setText(COOD);
scat1.setText(AS);
}
}
} catch(Exception g) {
g.printStackTrace();
}
} else if (scat3.getSelectedIndex() == 2) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String FILE = "jdbc:odbc:Usermbj";
Connection pcon = DriverManager.getConnection(FILE);
java.sql.Statement qt = pcon.createStatement();
ResultSet result = qt.executeQuery("SELECT * FROM Concessionaire1");
while (result.next()) {
String LN = result.getString(3);
String FN = result.getString(4);
String COMP = result.getString(12);
int RAPnum = result.getInt(2);
String EH = result.getString(1);
String FRP = result.getString(5);
String ROD = result.getString(6);
int RS = result.getInt(7);
String OD = result.getString(8);
int OS = result.getInt(9);
String COOD =result.getString(10);
String AS = result.getString(11);
if ((scat2.getSelectedIndex() == 1 && crit.getText().equals(LN))
&& (scat4.getSelectedIndex() == 1 && c.getText().equals(FN))) {
JOptionPane.showMessageDialog(null,
"Employment History----------" + " " + " " + EH + "\n\n"
+ "Rap Code----------" + " " + " " + RAPnum + "\n\n"
+ "First Name----------" + " " + " " + FN + "\n\n"
+ "Last Name----------" + " " + " " + LN + "\n\n"
+ "Finger Print Record----------" + " " + " " + FRP + "\n\n"
+ "Re-Orientation Date----------" + " " + " " + ROD + "\n\n"
+ "Re-Orientation Score----------" + " " + " " + RS + "\n\n"
+ "Oreintation Date----------" + " " + " " + OD + "\n\n"
+ "Orientation Score----------" + " " + " " + OS + "\n\n"
+ "Coodintors----------" + " " + " " + COOD + "\n\n"
+ "Applicant Gender----------" + " " + " " + AS + "\n\n"
+ "Company----------" + " " + " " + COMP);
lnam.setText(LN);
fnam.setText(FN);
comp.setText(COMP);
empn.setText(String.valueOf(RAPnum));
scat.setText(EH);
fpr1.setText(FRP);
reod.setText(ROD);
reos.setText(String.valueOf(RS));
od.setText(OD);
os.setText(String.valueOf(OS));
cood.setText(COOD);
scat1.setText(AS);
}
if ((scat2.getSelectedIndex() == 1 && crit.getText().equals(LN))
&&(c.getText().equals(""))){
JOptionPane.showMessageDialog(null,
"Employment History----------" + " " + " " + EH + "\n\n"
+ "Rap Code----------" + " " + " " + RAPnum + "\n\n"
+ "First Name----------" + " " + " " + FN + "\n\n"
+ "Last Name----------" + " " + " " + LN + "\n\n"
+ "Finger Print Record----------" + " " + " " + FRP + "\n\n"
+ "Re-Orientation Date----------" + " " + " " + ROD + "\n\n"
+ "Re-Orientation Score----------" + " " + " " + RS + "\n\n"
+ "Oreintation Date----------" + " " + " " + OD + "\n\n"
+ "Orientation Score----------" + " " + " " + OS + "\n\n"
+ "Coodintors----------" + " " + " " + COOD + "\n\n"
+ "Applicant Gender----------" + " " + " " + AS + "\n\n"
+ "Company----------" + " " + " " + COMP);
lnam.setText(LN);
fnam.setText(FN);
comp.setText(COMP);
empn.setText(String.valueOf(RAPnum));
scat.setText(EH);
fpr1.setText(FRP);
reod.setText(ROD);
reos.setText(String.valueOf(RS));
od.setText(OD);
os.setText(String.valueOf(OS));
cood.setText(COOD);
scat1.setText(AS);
}
if ((scat4.getSelectedIndex() == 1 && c.getText().equals(FN))
&&(crit.getText().equals(""))) {
JOptionPane.showMessageDialog(null,
"Employment History----------" + " " + " " + EH + "\n\n"
+ "Rap Code----------" + " " + " " + RAPnum + "\n\n"
+ "First Name----------" + " " + " " + FN + "\n\n"
+ "Last Name----------" + " " + " " + LN + "\n\n"
+ "Finger Print Record----------" + " " + " " + FRP + "\n\n"
+ "Re-Orientation Date----------" + " " + " " + ROD + "\n\n"
+ "Re-Orientation Score----------" + " " + " " + RS + "\n\n"
+ "Oreintation Date----------" + " " + " " + OD + "\n\n"
+ "Orientation Score----------" + " " + " " + OS + "\n\n"
+ "Coodintors----------" + " " + " " + COOD + "\n\n"
+ "Applicant Gender----------" + " " + " " + AS + "\n\n"
+ "Company----------" + " " + " " + COMP);
lnam.setText(LN);
fnam.setText(FN);
comp.setText(COMP);
empn.setText(String.valueOf(RAPnum));
scat.setText(EH);
fpr1.setText(FRP);
reod.setText(ROD);
reos.setText(String.valueOf(RS));
od.setText(OD);
os.setText(String.valueOf(OS));
cood.setText(COOD);
scat1.setText(AS);
}
}
} catch (Exception g) {
g.printStackTrace();
}
}
}
});
First of all, if you open a connection or create a statement, you should close it after using it, otherwise you will run out of resources soon enough.
Second, depending on the database you might have to commit the changes, otherwise the transaction might be rolled back and the data is lost.

Categories