How to call an array from a static method? -
in code, how call array globally other methods use?
background info on code, asked scan file contains dna strands translating rna strand.
i receive error: " cannot find symbol - variable dna " when call dna array on translation method (it can't find dna.length) for(int i=0; < dna.length; i++){
public class filescannerexample { public static void main(string[] args) throws ioexception { //this how create scanner read file scanner infile = new scanner(new file("dnafile.txt")); string dnasequence = infile.next(); int dnalength = dnasequence.length(); string[] dna = new string[dnalength]; for(int i=0; i<=dna.length-2 ; i++) { dna[i]=dnasequence.substring(i,i+1); //looking ahead , taking each character , placing in array } dna[dna.length-1]=dnasequence.substring(dna.length-1); //reading last spot in order put in array //testing array identical string system.out.println(dnasequence); for(int = 0 ; i<=dna.length-1; i++) { system.out.print(dna[i]); } } public void translation() { for(int i=0; < dna.length; i++){ //store temporary if (dna[i] = "a"){ dna[i] = "u"; } if(dna[i] = "t"){ dna[i] = "a"; } if(dna[i] = "g"){ dna[i]= "c"; } if(dna[i] = "c"){ dna[i] = "g"; } } } }
you need bring symbol scope before can reference it. can this, either pulling higher scope (as field in class), or sending local scope passing method parameter.
as class member:
public class test { private string myfield; void a() { myfield = "i can see you"; } void b() { myfield = "i can see too"; } }
as method parameter:
public class test { void a() { string myvar = "i can see you"; system.out.println(myvar); b(myvar); } void b(string param) { param += " too"; system.out.println(param); } }
note in order see instance member, must referencing non-static context. can around declaring field static too, although want careful static state in class, makes code more messy , harder work with.
Comments
Post a Comment