java - Cannot refer to non Final Variable (Array) -


the following program supposed receive user input 4 digit color code resistor, proceed print in jframe (currently has set both bufferedreader , jframe) however, understand cannot print in outer method inner method. instance, attempting print array3 in action listener after input goes through, error.

i attempted set variables final in main method, have searched various pages on internet , still can't find solution specific problem. did ask question on here, however, lead more confusion because of attempting re write code jframe in own method lead near chaos, me having result original code. i'm wondering how can take user input , pass on methods below on jframe, because i'm attempting right doesn't work.

import java.io.*; import javax.swing.*; import java.awt.event.*; import java.util.arrays; import javax.swing.jframe;  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");     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"};     string[] tollerance = {"red", "gold", "silver", "no band"};        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);     f.setlocationrelativeto(null);     //jtextfield t = new jtextfield(16);      /*sodamachine machine = new sodamachine(); //class name      jbutton cokebutton = new jbutton();      cokebutton.addactionlistener(new actionlistener() {      public void actionperformed() {      machine.dispensecoke();      }      }); */         jpanel p = new jpanel ();      jbutton b = new jbutton("press me") ;      b.setalignmentx(0);      b.setalignmenty(0);       test x = new test();      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]);       }        });       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));      //tollerance band      system.out.println(tollerance(input, tollerance));       }        public static int[] values (int [] digit, string num, string[] colours)      {       string holder;      double numholder;      int lengthofinput;      int holder2;       //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);      }       return new int[] {digit[0], digit[1], digit[2]} ;// 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;      }       public static string tollerance(string num, string[] tollerance){       string holder;      int holder2;      double pererror;       //tollerance      holder = num.substring(3,4);      pererror = double.parsedouble(holder);      holder2 = integer.parseint(num);      // checks see if above 5      if(pererror < 5){      pererror = pererror/holder2 * 100;      }      else if(pererror > 5){      pererror = 10 - pererror;      pererror = pererror/holder2 * 100;      }       //changing colour      if(pererror <= 2){      pererror = 0;      }else if(pererror <= 5){      pererror = 1;      }else if(pererror <= 10){      pererror = 2;      }else if(pererror <= 20){      pererror = 3;      }       return tollerance[(int)pererror];      }      }  

your short-term problem you're trying use non-final variable local method in inner class. can solved either using final variable or class field (here, because being used in static method, must static).

but having said that, over-all problem again you've got shoved static main method (or static method of kind matter). absolutely need create classes can used create objects. believe we've discussed before.


edit

i have changed mind -- "model", code decides color string display can consist of nothing static methods. have method looks this:

public class resistorcolorsutility {    // create string array, all_colors, constant methods     // of class can use.    private static final string[] all_colors = { "black", "brown", "red",          "orange", "yellow", "green", "blue", "violet", "gray", "white" };     public static string[] getcolorsforvalue(int value) {       // code in here create array of strings value given ,       // return         // here use value int find appropriate colors       // create string array called result hold color strings       string[] result = ......; // you've got create code!        return result; // return result    }     // have other public methods gui use     // , other private methods use internally     // , not used other classes } 

the gui has fields input , display of result, , button find out when user wants see result. main method little more program running, here create jframe put main gui code's jpanel in , display jframe.

import java.awt.event.actionevent; import java.awt.event.actionlistener;  import javax.swing.*;  // simple gui illustrate point public class resistorgui extends jpanel {    private jbutton findcolorbutton = new jbutton("find colors");    private jtextfield valueinputfield = new jtextfield(2);    private jtextfield resultfield = new jtextfield(15);     public resistorgui() {       // user shouldn't able edit result field       resultfield.seteditable(false);       resultfield.setfocusable(false);        findcolorbutton.addactionlistener(new actionlistener() {           @override          public void actionperformed(actionevent arg0) {             // program logic in here              // value string valueinputfield             // translate int             // call utility method resistorcolorsutil class!             // display result in resultfield          }       });        add(new jlabel("value:"));       add(valueinputfield);       add(findcolorbutton);       add(new jlabel("result:"));       add(resultfield);    }      // main display gui, that's    public static void main(string[] args) {       resistorgui mainpanel = new resistorgui();        jframe frame = new jframe("resistorgui");       frame.setdefaultcloseoperation(jframe.exit_on_close);       frame.getcontentpane().add(mainpanel);       frame.pack();       frame.setlocationbyplatform(true);       frame.setvisible(true);    } } 

Comments