mardi 24 février 2015

Error: Variable might not have been initialized?

Trying to create a code that converts RGB color code to its equivalent hexadecimal code. I've only gotten through the if statements for the first two characters so far but I get the error:


ToHex.java:62: error: variable digitOne might not have been initialized System.out.println("The first two characters are: "+digitOne+digitTwo); ^ ToHex.java:62: error: variable digitTwo might not have been initialized System.out.println("The first two characters are: "+digitOne+digitTwo);


This is the main part of my code so far:



try{

int red, green, blue;
System.out.print("Please enter three numbers representing RGB values: ");
red = myScanner.nextInt();
green = myScanner.nextInt();
blue = myScanner.nextInt();

if(red>=0 && red<=255 && green>=0 && green<=255 && blue>=0 && blue<=255){
int firsthexRed = (int)(red/16);
int redremainder = red%16;
int firsthexGreen = (int)(green/16);
int greenremainder = green%16;
int firsthexBlue = (int)(blue/16);
int blueremainder = blue%16;
String digitOne, digitTwo, digitThree, digitFour, digitFive, digitSix;

if(firsthexRed>=0 && firsthexRed<=9){ //if statement for first hex character
digitOne = Integer.toString(firsthexRed);
}else if (firsthexRed==10){
digitOne = "A";
}else if (firsthexRed==11){
digitOne = "B";
}else if (firsthexRed==12){
digitOne = "C";
}else if (firsthexRed==13){
digitOne = "D";
}else if (firsthexRed==14){
digitOne = "E";
}else if (firsthexRed==15){
digitOne = "F";
}

if(redremainder>=0 && redremainder<=9){ //if statement for second hex character
digitTwo = Integer.toString(redremainder);
}else if (redremainder==10){
digitTwo = "A";
}else if (redremainder==11){
digitTwo = "B";
}else if (redremainder==12){
digitTwo = "C";
}else if (redremainder==13){
digitTwo = "D";
}else if (redremainder==14){
digitTwo = "E";
}else if (redremainder==15){
digitTwo = "F";
}

System.out.println("The first two characters are: "+digitOne+digitTwo);

}else{
System.out.println("You did not enter integers between 0-255 (inclusive).");
}

}catch(RuntimeException ex){
System.out.println("Sorry, your input must consist of integers.");
}

}


} ^ ^


Aucun commentaire:

Enregistrer un commentaire