5 Replies - 5082 Views - Last Post: 30 July 2011 - 01:41 PM Rate Topic: -----

#1 glenak1911  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 16-May 11

FileReader Errors

Posted 30 July 2011 - 09:23 AM

Hi guys this is my final project for my java 1 class. We are implementing filewriter and filereader and stuff, but so far I'm getting an error message and I'm not sure why.

Here are the instructions for this part of the project:
Worth 5 points
- Download the file called hs_chr1.dna from:
http://users.cis.fiu...amples/1200.dna
- open the previous file.
- load it into a one dimension array or a String, your choice
this file contains nueclotides/bases/letters in it such as a, c, g, and t, others.
- Find out the total amount of neuclotides a, how many c, how many g, how many t and how many all others are in this file.
Make sure to test for upper and lower cases of each a,c,g,t base.
- Print the first and the last neuclodide in the file, and using the ceil() method print the one exactly in the middle of the file
- Print all total in an aligned column the numeric format 1,234,567


Here is my code

package knight_glen_project5;

import java.io.*;
import java.util.ArrayList;

public class Main {

    
    public static void main(String[] args) throws IOException
    {
        int[] nameList = null;

        DNA(nameList);
        
    }

    public static void DNA(int[] nameList) throws IOException
    {
        int index=0;
        FileReader freader = new FileReader("C:\\1200.dna");
        BufferedReader inputfile = new BufferedReader(freader);
        String str;

        str=inputfile.readLine();

        while(str != null)
        {
            nameList[index] = Integer.parseInt(str);
            index++;
            str = inputfile.readLine();
        }
        
        inputfile.close();
        
        int occurence=0;
        int occurence1=0;
        int occurence2=0;
        int occurence3=0;
        int occurence4=0;
        
	for (int i = 0; i < nameList.length; i++)
        { 
            char letter = (char) nameList[i];
            String s1= Character.toString(letter);
            if (s1.equalsIgnoreCase("a"))
            {
			occurence++;
            }
            else if (s1.equalsIgnoreCase("c"))
            {
                        occurence1++;
            }
            else if (s1.equalsIgnoreCase("g"))
            {
                        occurence2++;
            }
            else if(s1.equalsIgnoreCase("t"))
            {
                        occurence3++;
            }
            else
            {
                occurence4++;
            }
        }
        System.out.println("There are " +occurence +" occurences of a");
        System.out.println("There are " +occurence1 +" occurences of c");
        System.out.println("There are " +occurence2 +" occurences of g");
        System.out.println("There are " +occurence3 +" occurences of t");
        System.out.println("There are " +occurence4 +" occurences of other letters");

    }

}



And here is the error I'm receiving when I try to run the program:
run:
Exception in thread "main" java.lang.NumberFormatException: For input string: "Tttaaagagaccggcgattctagtgaaatcgaacgggcaggtcaatttccaaccagcgan"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:449)
at java.lang.Integer.parseInt(Integer.java:499)
at knight_glen_project5.Main.DNA(Main.java:51)
at knight_glen_project5.Main.main(Main.java:36)
Java Result: 1
BUILD SUCCESSFUL (total time: 5 seconds)

Can anyone help me out with this, I'm a bit lost :online2long:

Is This A Good Question/Topic? 0
  • +

Replies To: FileReader Errors

#2 immeraufdemhund  Icon User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

Re: FileReader Errors

Posted 30 July 2011 - 09:37 AM

your getting that error because you are trying to parse a string value into a number

you can only parse numbers (with teh exception of hex numbers) from a string into a number ie

String number = "1020459"; //just a random number by bashing my numpad
int realNumber = Integer.parseInt(number);


that will work

String notANumber = "123Z";
int notRealNumber = Integer.parseInt(notANumber); // will not work will throw a Number Format Exception


this will not because of the letter in it. with the exception of Hex numbers...Letters are never numbers, therefor if you try to make it a number you will get a number format exception
Was This Post Helpful? 0
  • +
  • -

#3 Viske  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 70
  • Joined: 07-June 11

Re: FileReader Errors

Posted 30 July 2011 - 09:45 AM

Try converting the characters in the string to numbers instead.

char c = 'a';
int i = c;    // i = 97



Edit: Wrong value

This post has been edited by Viske: 30 July 2011 - 10:36 AM

Was This Post Helpful? 0
  • +
  • -

#4 glenak1911  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 16-May 11

Re: FileReader Errors

Posted 30 July 2011 - 11:00 AM

Thanks guys but I'm still a bit confused. I understand the error about trying to parse a string into an integer, but how can I load the array with the values from the text?

The file is full of letters in the form Tttaaagagaccggcgattctagtgaaatcgaacgggcaggtcaatttccaaccagcgan.
Was This Post Helpful? 0
  • +
  • -

#5 Viske  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 70
  • Joined: 07-June 11

Re: FileReader Errors

Posted 30 July 2011 - 11:10 AM

You mean the int array?

Instead of doing this:
while(str != null)
{
    nameList[index] = Integer.parseInt(str);
    index++;
    str = inputfile.readLine();
}



parse the characters of str instead. I'm assuming you know how to get char values from a String.

while ((str = inputFile.readLine()) != null)
    nameList[index] = // char from str
    index++;
}


Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: FileReader Errors

Posted 30 July 2011 - 01:41 PM

And how is

Tttaaagagaccggcgattctagtgaaatcgaacgggcaggtcaatttccaaccagcgan

supposed to be converted into a number ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1