Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: It's not printing out the correct percentage...

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default It's not printing out the correct percentage...

    public static void main(String[] args) {
    		getFile();
     
    		if (fastaFile != null) {
    			totalCount = countBase(getSequence(fastaFile), 'A')
    					+ countBase(getSequence(fastaFile), 'G')
    					+ countBase(getSequence(fastaFile), 'C')
    					+ countBase(getSequence(fastaFile), 'T');
     
    			System.out.println(getHeader(fastaFile));
    			System.out.printf("A: == (%1$1.0f%%)\r\n", (float) (countBase(
    					getSequence(fastaFile), 'A') / totalCount) * 100);
    		} else {
    			System.out.println("Invalid FASTA file chosen.");
    		}
    	}

    getFile(); - This just prompts the user to input the FASTA file.
    countBase(String sequence, char base); - This returns how many times the char base appears in the sequence.
    getSequence(File file); - This returns a certain part of the file (a String).

    My issue is this line:
    System.out.printf("A: == (%1$1.0f%%)\r\n", (float) (countBase(
    					getSequence(fastaFile), 'A') / totalCount) * 100);

    I need it to print it out like: A: == (45%), A: == (37%) etc. For some reason it keeps returning 0%. Though when I do a test for the countBase(); return for 'A', it tells me there are 242 character A's in the total count (which in this case is 511) which is (I did this via calculator: count / total * 100 - what I am trying to do but it isn't working) 47%. My question is how can I fix the above line to do that for me?

    Thanks.


  2. #2
    Junior Member
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: It's not printing out the correct percentage...

    Never-mind I got it to work:

    I had to add:
    System.out.printf("A: == (%1$1.0f%%)\r\n", ((float) countBase(
    					getSequence(fastaFile), 'A') / totalCount) * 100);

Similar Threads

  1. Crude method of using offset whilst using BufferedReader - Correct way?
    By Pr0ject-Rec0n in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 9th, 2010, 10:59 PM
  2. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 03:41 AM
  3. Arrays.equals not returning correct answer.
    By Anyone in forum Collections and Generics
    Replies: 2
    Last Post: February 11th, 2010, 10:12 AM
  4. No Percentage?
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 18th, 2009, 12:02 PM
  5. Printing JTable that retrieve data from the Database
    By hundu in forum AWT / Java Swing
    Replies: 3
    Last Post: June 28th, 2009, 02:50 PM