java - Cannot refer to non variable inside Action listener (Jframe) -


the following program's goal ask user input resistor value, of program output corresponding colors each digit. not include digits. however, program done, i've made attempt incorporate jframe thing, except hung on how print corresponding colors in action listener.

this line calls specific methods, 3 digits proceeds print colors, except how incorporate , rest of code jframe, , or actionlistener. line system.out.println(array3[i]);/

i receive errors when trying print color values in action listener "cannot refer non-final variable"....

i have tried viewing various tutorials online, , java api , guidelines, none of help. in general seem unaware of how incorporate code written jframe, whether it's tedious process, willing corporate , grateful insight on how tackle predicament.

import java.io.*; import javax.swing.*; //import javax.swing.jframe; //import javax.swing.jlabel; //import javax.swing.jbutton; //import javax.swing.jpanel; //import javax.swing.jtextfield; import java.awt.event.*;   public class test extends jframe {   public static void main (string [] args) throws ioexception   {     bufferedreader myinput = new bufferedreader (new inputstreamreader (system.in));       //calling variables     string input;     int numinput;      jlabel l = new jlabel("hello , welcome program (press button start instructions");     //l.setalignmentx(0);     // l.setalignmenty(0);      //calling arrays     int [] array = new int [5];     int [] array2 = new int [3];     string [] array3 = new string [3];     string[] colours = {"black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "gray", "white"};       jframe f = new jframe("hello jframe");     f.setsize(500,500);     //f.getcontentpane().setbackground(color.cyan);     f.add(l);     f.setdefaultcloseoperation(jframe.exit_on_close);     f.setvisible(true);      //jtextfield t = new jtextfield(16);       jpanel p = new jpanel ();     jbutton b = new jbutton("press me") ;     // b.setalignmentx(0);     // b.setalignmenty(0);      b.addactionlistener(new actionlistener(){       public void actionperformed (actionevent e) {         joptionpane.showmessagedialog(null,"in following program (the user!) input number of resistor value \nthe program pass information methods , proceed print out \nthe coorelating colors (press button asked input)");         int number = integer.parseint(joptionpane.showinputdialog("please enter resistor value"));           joptionpane.showmessagedialog(null, "the colors : " + (array3[i] + "\n" ));          }       });      p.add(b);     p.add(l);     //p.add(t);     f.add(p);       system.out.println("hello , welcome program (press key con't)");     input = myinput.readline ();      system.out.println("in following program (the user!) input number of resistor value");     system.out.println("the program pass information methods , proceed print out");     system.out.println("the coorelating colors (press key asked input)");     input = myinput.readline();      system.out.println("enter resistor value (note resistors can acount 4 decimal places");     input = myinput.readline ();     numinput = integer.parseint (input);      //colours values     array2 = values(array, input, colours);     for(int = 0 ; < 3; i++){       array3[i] = digitcolours(array2[i], colours);       system.out.println(array3[i]);// prints colours values     }       //prints 4th colour multiplier     system.out.println(decimalplaces(input, colours));    }     public static int[] values (int [] digit, string num, string[] colours)   {      string holder;     double numholder;     int lengthofinput;     int holder2;      //tollerance     holder = num.substring(3,4);     digit[3] = integer.parseint(holder);     holder2 = integer.parseint(num);     // checks see if above 5     if(digit[3] < 5){       digit[3] = digit[3]/holder2 * 100;     }     else if(digit[3] > 5){       digit[3] = 10 - digit[3];       digit[3] = digit[3]/holder2 * 100;     }     system.out.println(digit[3]);      //rounding of input     lengthofinput = num.length() - 3;     numholder = double.parsedouble(num);     numholder = numholder/(math.pow(10,lengthofinput));     numholder = (int)(math.round(numholder)+0.5);      // first 3 digits     for(int = 0; < 3; i++){       holder = num.substring(i,i+1);       digit[i] = integer.parseint(holder);     }      //print out information     /*system.out.println("the first digit rounded to:" + (int)digit[0]);      system.out.println("the second digit roudned to:" + (int)digit[1]);                         system.out.println("the third digit roudned to:" + (int)digit[2]);  */     /* */     return new int[] {digit[0], digit[1],digit[2],digit[3]} ;// return   }     public static string digitcolours(int decimalplace, string[] colours){     //calling additional variables     string answer;     answer = colours[decimalplace];     return answer;   }     //method find multiplier   public static string decimalplaces(string input, string[] colours){     //calling additional variables     int length = input.length();     string answer;      length = length - 3;     answer = colours[length];      return answer;   } }  

in java cannot refer not final variables inside anonymous inner class. attempt access array3 inside anonymous implementation of actionlistener. can change array3 final, ie:

final string [] array3 = new string [3]; 

also, print content of array can use arrays.tostring():

joptionpane.showmessagedialog(null, "the colors : " + (arrays.tostring(array3))); 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -