Results 1 to 10 of 10
- 11-17-2010, 05:23 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 6
- Rep Power
- 0
This one wont compile either.. any tips?
Here is another question i am stumped on. we are supposed to write a program that opens a user-inputted file, and dissects the text to find out a running total of how many a's c's g's and t's there are in the text. then it is supposed to print out something like this:
"gi|21071042|ref|NM_000193.2| Homo sapiens sonic hedgehog (SHH), mRNA
A: ========================= (25%)
G: ================================= (33%)
C: ============================= (29%)
T: ============== (14%)
Programmed by [your name here].
End of processing."
The first line is the header of the text file that you wrote in.
the next four lines are the letters that we have a running total of, with the percentage of the text file that the letter takes up. also it has a bunch of equals signs that corrrespond to the percentage.
Here is what i have so far:
Java Code:/* * COMP 1010 Section A06 * Instructor: Christina Penner * Assignment: Assignment 3, question 2 * @author: Erik Danielson */ import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import javax.swing.JFileChooser; //assignment name public class QAA3Q2 { private static Scanner scanner; private static File fastaFile; private static JFileChooser chooser; private static StringBuilder sb; private static int count; public static void main(String[] args) { getFile(); int aPercentage; int cPercentage; int gPercentage; int tPercentage; if (fastaFile != null) { // print message and get next input System.out.printf("%1$s\r\n", getHeader(fastaFile)); } else { System.out.println("Invalid FASTA file."); } aPercentage = (aTotal / getSequence.length); cPercentage = (cTotal / getSequence.length); gPercentage = (gTotal / getSequence.length); tPercentage = (tTotal / getSequence.length); printDNAHistogram(); countBase(); } public static File getFile() { JFileChooser chooser = new JFileChooser(); File fastaFile = null; int returnValue; // show the open file dialog returnValue = chooser.showOpenDialog(null); // check to see that the user clicked the OK button if (returnValue == JFileChooser.APPROVE_OPTION) { // get the selected file fastaFile = chooser.getSelectedFile(); } return fastaFile; }//close getFile public static String getHeader(File file) { if (file != null) { try { scanner = new Scanner(file); // returns header of file you chose without > symbol return scanner.nextLine().substring(1, scanner.nextLine().length() - 1); }catch (FileNotFoundException fnf) { fnf.printStackTrace(); }finally { //closes scanner object if (scanner != null) { scanner.close(); } } } return null; } //prints file without first line in it. public static String getSequence(File file) { if (file != null) { try { sb = new StringBuilder(); scanner = new Scanner(file); while (scanner.hasNextLine()) { if (!scanner.nextLine().contains(">")) { sb.append(scanner.nextLine()); } } return sb.toString(); } catch (FileNotFoundException fnf) { fnf.printStackTrace(); } finally { // closes scanner if (scanner != null) { scanner.close(); } } } return null; } //goes through all the letter and keeps a running total for each. public static int countBase(String getSequence, char base, int aTotal, int cTotal, int gTotal, int tTotal) { getSequence(); aTotal= 0; cTotal= 0; gTotal= 0; tTotal= 0; for (int i = 0; i < getSequence.length(); i++) { if (getSequence.charAt(i) == 'A') { aTotal= aTotal+1; } else if (getSequence.charAt(i) == 'C') { cTotal= cTotal+1; } else if (getSequence.charAt(i) == 'G') { gTotal= gTotal+1; } else if (getSequence.charAt(i) == 'T') { tTotal= tTotal+1; } return System.out.println(aTotal,cTotal,gTotal,tTotal); } return count; } public static void printDNAHistogram(String getHeader, int aPct, int cPct, int gPct, int tPct) { System.out.println(getHeader); printBar(); } public static void printBar(String label, int percent, char barSymbol) { countBase(); System.out.println("A: "); for(i=0; i<aPct; i++){ System.out.println("="); } System.out.println(aPct); System.out.println("C: "); for(i=0; i<cPct; i++){ System.out.println("="); } System.out.println(cPct); System.out.println("G: "); for(i=0; i<gPct; i++){ System.out.println("="); } System.out.println(gPct); System.out.println("T: "); for(i=0; i<tPct; i++){ System.out.println("="); } System.out.println(tPct); } }
- 11-17-2010, 05:32 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
What's your compilation error?
And where does it occur?
- 11-17-2010, 07:53 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 6
- Rep Power
- 0
There is a whole bunch of errors. it seems like it didnt work when i called the methods.
Here is the Error message:
C:\Users\chelsea\Downloads\QAA3Q2.java:39: cannot find symbol
symbol : variable aTotal
location: class QAA3Q2
aPercentage = (aTotal / getSequence.length);
^
C:\Users\chelsea\Downloads\QAA3Q2.java:39: cannot find symbol
symbol : variable getSequence
location: class QAA3Q2
aPercentage = (aTotal / getSequence.length);
^
C:\Users\chelsea\Downloads\QAA3Q2.java:40: cannot find symbol
symbol : variable cTotal
location: class QAA3Q2
cPercentage = (cTotal / getSequence.length);
^
C:\Users\chelsea\Downloads\QAA3Q2.java:40: cannot find symbol
symbol : variable getSequence
location: class QAA3Q2
cPercentage = (cTotal / getSequence.length);
^
C:\Users\chelsea\Downloads\QAA3Q2.java:41: cannot find symbol
symbol : variable gTotal
location: class QAA3Q2
gPercentage = (gTotal / getSequence.length);
^
C:\Users\chelsea\Downloads\QAA3Q2.java:41: cannot find symbol
symbol : variable getSequence
location: class QAA3Q2
gPercentage = (gTotal / getSequence.length);
^
C:\Users\chelsea\Downloads\QAA3Q2.java:42: cannot find symbol
symbol : variable tTotal
location: class QAA3Q2
tPercentage = (tTotal / getSequence.length);
^
C:\Users\chelsea\Downloads\QAA3Q2.java:42: cannot find symbol
symbol : variable getSequence
location: class QAA3Q2
tPercentage = (tTotal / getSequence.length);
^
C:\Users\chelsea\Downloads\QAA3Q2.java:44: printDNAHistogram(java.lang.String,int,int,int,int ) in QAA3Q2 cannot be applied to ()
printDNAHistogram();
^
C:\Users\chelsea\Downloads\QAA3Q2.java:45: countBase(java.lang.String,char,int,int,int,int) in QAA3Q2 cannot be applied to ()
countBase();
^
C:\Users\chelsea\Downloads\QAA3Q2.java:122: getSequence(java.io.File) in QAA3Q2 cannot be applied to ()
getSequence();
^
C:\Users\chelsea\Downloads\QAA3Q2.java:142: cannot find symbol
symbol : method println(int,int,int,int)
location: class java.io.PrintStream
return System.out.println(aTotal,cTotal,gTotal,tTotal);
^
C:\Users\chelsea\Downloads\QAA3Q2.java:155: printBar(java.lang.String,int,char) in QAA3Q2 cannot be applied to ()
printBar();
^
C:\Users\chelsea\Downloads\QAA3Q2.java:161: countBase(java.lang.String,char,int,int,int,int) in QAA3Q2 cannot be applied to ()
countBase();
^
C:\Users\chelsea\Downloads\QAA3Q2.java:164: cannot find symbol
symbol : variable i
location: class QAA3Q2
for(i=0; i<aPct; i++){
^
C:\Users\chelsea\Downloads\QAA3Q2.java:164: cannot find symbol
symbol : variable i
location: class QAA3Q2
for(i=0; i<aPct; i++){
^
C:\Users\chelsea\Downloads\QAA3Q2.java:164: cannot find symbol
symbol : variable aPct
location: class QAA3Q2
for(i=0; i<aPct; i++){
^
C:\Users\chelsea\Downloads\QAA3Q2.java:164: cannot find symbol
symbol : variable i
location: class QAA3Q2
for(i=0; i<aPct; i++){
^
C:\Users\chelsea\Downloads\QAA3Q2.java:167: cannot find symbol
symbol : variable aPct
location: class QAA3Q2
System.out.println(aPct);
^
C:\Users\chelsea\Downloads\QAA3Q2.java:170: cannot find symbol
symbol : variable i
location: class QAA3Q2
for(i=0; i<cPct; i++){
^
C:\Users\chelsea\Downloads\QAA3Q2.java:170: cannot find symbol
symbol : variable i
location: class QAA3Q2
for(i=0; i<cPct; i++){
^
C:\Users\chelsea\Downloads\QAA3Q2.java:170: cannot find symbol
symbol : variable cPct
location: class QAA3Q2
for(i=0; i<cPct; i++){
^
C:\Users\chelsea\Downloads\QAA3Q2.java:170: cannot find symbol
symbol : variable i
location: class QAA3Q2
for(i=0; i<cPct; i++){
^
C:\Users\chelsea\Downloads\QAA3Q2.java:174: cannot find symbol
symbol : variable cPct
location: class QAA3Q2
System.out.println(cPct);
^
C:\Users\chelsea\Downloads\QAA3Q2.java:177: cannot find symbol
symbol : variable i
location: class QAA3Q2
for(i=0; i<gPct; i++){
^
C:\Users\chelsea\Downloads\QAA3Q2.java:177: cannot find symbol
symbol : variable i
location: class QAA3Q2
for(i=0; i<gPct; i++){
^
C:\Users\chelsea\Downloads\QAA3Q2.java:177: cannot find symbol
symbol : variable gPct
location: class QAA3Q2
for(i=0; i<gPct; i++){
^
C:\Users\chelsea\Downloads\QAA3Q2.java:177: cannot find symbol
symbol : variable i
location: class QAA3Q2
for(i=0; i<gPct; i++){
^
C:\Users\chelsea\Downloads\QAA3Q2.java:181: cannot find symbol
symbol : variable gPct
location: class QAA3Q2
System.out.println(gPct);
^
C:\Users\chelsea\Downloads\QAA3Q2.java:184: cannot find symbol
symbol : variable i
location: class QAA3Q2
for(i=0; i<tPct; i++){
^
C:\Users\chelsea\Downloads\QAA3Q2.java:184: cannot find symbol
symbol : variable i
location: class QAA3Q2
for(i=0; i<tPct; i++){
^
C:\Users\chelsea\Downloads\QAA3Q2.java:184: cannot find symbol
symbol : variable tPct
location: class QAA3Q2
for(i=0; i<tPct; i++){
^
C:\Users\chelsea\Downloads\QAA3Q2.java:184: cannot find symbol
symbol : variable i
location: class QAA3Q2
for(i=0; i<tPct; i++){
^
C:\Users\chelsea\Downloads\QAA3Q2.java:188: cannot find symbol
symbol : variable tPct
location: class QAA3Q2
System.out.println(tPct);
^
34 errors
Tool completed with exit code 1
- 11-17-2010, 08:20 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
C:\Users\chelsea\Downloads\QAA3Q2.java:39: cannot find symbol
symbol : variable aTotal
location: class QAA3Q2
aPercentage = (aTotal / getSequence.length);
"cannot find symbol" is the compiler's way of saying it doesn't know what aTotal is supposed to be.
Variables do not "carry over" from other methods that might have been called. If you use a variable like aTotal then either it must be declared in the main() method (the way you do with aPercentage etc) or it must be declared in some enclosing scope (the way you do with scanner and the rest).
By "enclosing" I mean in terms of the {} structure. The "public class QAA3Q2 {" encloses main() in a way that "countBase() {" does not.
One "solution" would be to move the declaration of countA from countBase() to up where you declare scanner. I put solution in snear quotes because that really isn't very pretty - you might want to check with the exact wording of the assignment to see if so many static variables are really needed.
--------------------
There is another problem on the same line: "getSequence.length" doesn't make any sense. Instead:
(1) Declare a String variable - call it whatever you like, sequence would be good.
(2) Call getSequence() and assign the returned value to that variable.
(3) Do (1) and (2) before line 39. Then use sequence.length() instead of getSequence.length
-----------------------
It would be a very good idea not to let compiler messages build up.
Your assignment instructions (they have been posted here recently) are very complete and work step by step. You might want to consider scrapping what you ahve and following those instructions, proceeding from one step to the next only when you have responded to each of the compiler's messages.Last edited by pbrockway2; 11-17-2010 at 08:25 PM.
- 11-17-2010, 08:21 PM #5
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
When you declared all of your percent variables and other variables, they were declare within their own methods. Therefore, no other method can access them. You need to declare them as private data before the "main" class. In Java, you cannot use a variable outside of its own method/class without declaring in the WHOLE class.
- 11-17-2010, 08:28 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
You need to declare them as private data before the "main" class.
To repeat myself before the OP goes too far down that track: check the assignment instructions to see if so many static variables are really required.
- 11-17-2010, 08:30 PM #7
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
I don't think it would be a huge issue to declare them all static. Or would it?
- 11-17-2010, 09:03 PM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
I don't know if it would be a huge issue or not. But I'd make a couple of points about it:
(1) The instructions are here and they call for a countBase() method that returns the likes of countA. Ie these values are passed about as arguments/return values and not stored as static variables. Were the OP to adopt that strategy it would be a fail in terms of what was asked for.
(2) I am uneasy about static variables in general.
I can't see a problem like this without generalising in my mind how it might be useful for analysing base sequences in general (put another way how it might translate into a class representing the sequence: the static variables being instance variables of the class) You could have the base counts as such variables, but where would you stop? Why not pairs (or longer subsequences) of bases? Or the distance occuring between well known subsequences? There are a lot of statistics that could be calculated.
I don't know a lot about such design questions. Often I find myself imitating what Java's library writers do. Here the CharSequence comes to mind - with its limited behaviour and no mandated state whatsoever.
One problem with "static" is that it addresses a particular problem at the cost of generality. Think reuse: as anyone really interested in base sequences would. How can my representation today be used for tomorrow's analysis techniques.
Another is the slippery slope, and the fact that bugs breed amid the static declarations. How many variables are you prepared to keep track of in your mind, when limiting their scope to a single method would keep them organised? Extending the scope of countA to the entire class entails the risk of finding that it has the value zero when it comes to print - even though you carefully assigned it the correct value within your code: all because some other method stomped on it.
- 11-18-2010, 02:39 AM #9
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Kindly look at the comment in OP code at both thread the author are both Erik Danielson.
What?!?!
This one wont compile either.. any tips?
- 11-18-2010, 05:49 AM #10
Similar Threads
-
Why wont this compile?
By Student101 in forum New To JavaReplies: 8Last Post: 11-18-2010, 05:33 AM -
Tips needed
By adz06 in forum New To JavaReplies: 5Last Post: 12-07-2009, 01:35 AM -
why wont it compile
By bje98f in forum Advanced JavaReplies: 1Last Post: 04-23-2009, 10:55 PM -
Need some tips.
By PVL268 in forum New To JavaReplies: 4Last Post: 02-27-2009, 02:37 PM -
simple problem - code wont compile
By dirtycash in forum New To JavaReplies: 1Last Post: 11-20-2007, 05:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks