Hi, I am getting this error for my codes:

Exception in thread "main" java.lang.NumberFormatException: For input string: "{ 1.0, 2.0, 3.0, 4.0, 5.0, 1.0, 2.0, 3.0, 4.0, 5.0, 1.0, 2.0, 3.0, 4.0, 5.0, 1.0, 2.0, 3.0, 4.0, 5.0, 1.0, 2.0, 3.0, 4.0, 5.0, 1.0, 2.0, 3.0, 4.0, 5.0},"
        at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1224)
        at java.lang.Double.parseDouble(Double.java:510)
        at MatchApplet.init(DPTest.java:126)
        at MatchApplet.main(DPTest.java:75)

May I know what is wrong with my codes, which part?

Recommended Answers

All 32 Replies

Can't tell without the your code, but if you're trying to parse that "{" at the beginning of your input string, that could be the cause of your problem.

Even if I don't put {, the same error message still comes out.

Here is my code:

import java.io.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;

class MatchApplet extends JApplet {


	TextArea outputArea;
	JButton button;
	JButton reset;

	JTextField tF1;
	JTextField tF2;

	JLabel l1;
	JLabel l2;

	String s1;
	String s2;

        static String display1 = "";
  	static String display2 = "";
        static String display = "";
        static double mydisplay;

        static double[][] mydouble = new double [1000][1000];
        int a, b;




	public static void main(String[] args)
	{
            //Application for program
            Frame f = new Frame();
            f.addWindowListener(new java.awt.event.WindowAdapter()
            {
                public void windowClosing(java.awt.event.WindowEvent e)
                {
                System.exit(0);
                };
            });

  		MatchApplet ut = new MatchApplet();
  		ut.setSize(900,900); // same size as defined in the HTML APPLET
  		f.add(ut);
  		f.pack();
  		ut.init();
  		f.setSize(900,900 + 100); // add 20, seems enough for the Frame title,
  		f.show();
		//end of application for program

	}//end of public static void main(String[] args)



  	public void init()
	{

		try
		{
                BufferedReader in1 = new BufferedReader(new FileReader("C:\\Users\\Serene\\Documents\\Major Project\\Alignment Algorithms\\1a00(50).txt"));	//reading files in specified directory
                BufferedReader in2 = new BufferedReader(new FileReader("C:\\Users\\Serene\\Documents\\Major Project\\Alignment Algorithms\\1a0a(50).txt"));	//reading files in specified directory

                FileInputStream read = new FileInputStream("C:\\Users\\Serene\\Documents\\Major Project\\Alignment Algorithms\\Testing.txt");
                DataInputStream read1 = new DataInputStream(read); // Get the object of DataInputStream
                BufferedReader in = new BufferedReader(new InputStreamReader(read1));

  			String str1;
			while ((str1 = in1.readLine()) != null)	//file reading
			{
                            display1 = str1;
                            System.out.print(display1);
  			}
  			in1.close();

  			System.out.println("");

			String str2;
			while ((str2 = in2.readLine()) != null)	//file reading
			{
                            display2 = str2;
                            System.out.print(display2);
  			}
			in2.close();

                        String str;
			while ((str = in.readLine()) != null)	//file reading
			{
                            StringTokenizer st = new StringTokenizer(str);
                            while (st.hasMoreTokens())
                            {
                            System.out.print(st.nextToken());
                            }


                            //System.out.println(java.util.Arrays.toString(str.split("  ")));
                            display = str;
                            mydisplay = Double.parseDouble(display);
                            mydouble[a][b] = mydisplay;
                            System.out.print(mydouble[a][b]);
  			}
			in.close();




		Container c = getContentPane();
		c.setLayout(new FlowLayout());

		outputArea = new TextArea(40,110);
		Font font = new Font("Courier", Font.PLAIN, 12);
		outputArea.setFont(font);
		outputArea.setEditable(false);

		tF1 = new JTextField(display1);
		tF2 = new JTextField(display2);

		l1 = new JLabel("Sequence 1:");
		l2 = new JLabel("Sequence 2:");

		c.add(l1);
		c.add(tF1);
		c.add(l2);
		c.add(tF2);
		c.add(outputArea);


	final Substitution sub = new Blosum50();

		s1 += tF1.getText();
		s2 += tF2.getText();
		Output out = new Output ()
		{
	  		public void print(String s)
	  		{ outputArea.append(s); }

	  		public void println(String s)
	  		{ outputArea.append(s); outputArea.append("\n"); }

	  		public void println()
	  		{ outputArea.append("\n"); }
		};

	outputArea.setText("");
      (new NW      (sub, 8,     s1, s2)).domatch(out, "GLOBAL ALIGNMENT");
      (new SW      (sub, 8,     s1, s2)).domatch(out, "LOCAL ALIGNMENT");


      }catch( IOException ioException ) {}

  }//end of init()

}//end of class




// The class of substitution (scoring) matrices

abstract class Substitution {
  public double[][] score;

  void buildscore(String residues, String residues2, double[][] residuescores)
  {
    // Allow lowercase and uppercase residues (ASCII code <= 127):
    score = new double[127][127];
    for (int i=0; i<residues.length(); i++)
    {
      char res1 = residues.charAt(i);
      for (int j=0; j<=i; j++)
      {
        char res2 = residues2.charAt(j);
        score[res1][res2] = score[res2][res1]
	  = score[res1][res2+32] = score[res2+32][res1]
	  = score[res1+32][res2] = score[res2][res1+32]
	  = score[res1+32][res2+32] = score[res2+32][res1+32]
	  = residuescores[i][j];
      }
    }
  }

  abstract public String getResidues();
  abstract public String getResidues2();
}


// The BLOSUM50 substitution matrix for amino acids (Durbin et al, p 16)

class Blosum50 extends Substitution
{


  private String residues = MatchApplet.display1;
  private String residues2 = MatchApplet.display2;

  public String getResidues()
  { return residues; }

  public String getResidues2()
  { return residues2; }

  private double[][] residuescores = MatchApplet.mydouble;
            

  public Blosum50()
  { buildscore(residues, residues2, residuescores); }
}

The exception says that you are passing an entire line to the parseDouble method instead of passing a String which contains a valid double representation. After reading the line, split it (look into the split method of String class) based on whatever separator char you have in the file (comma or space). For each split chunk, remove any superfluous spaces by invoking trim() on that chunk. After that call Double.parseDouble() and you should be good to go.

I'm looking at lines 90-100
I see you read a line and then split it into tokens, but then you try to parse the whole line as just one Double value. If the line contains multiple tokens, then it presumably has more than 1 Double in it. Maybe you should be parsing each token individually?

Thanks people! I will try it out and see if it works! (:

Hey guys, I trim away the white spaces already but it still doesn't work?

Show us the relevant code & data.

import java.io.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;

class MatchApplet extends JApplet {


	TextArea outputArea;
	JButton button;
	JButton reset;

	JTextField tF1;
	JTextField tF2;

	JLabel l1;
	JLabel l2;

	String s1;
	String s2;

        static String display1 = "";
  	static String display2 = "";
        static String display = "";
        static double mydisplay;

        static double[][] mydouble = new double [1000][1000];
        int a, b;




	public static void main(String[] args)
	{
            //Application for program
            Frame f = new Frame();
            f.addWindowListener(new java.awt.event.WindowAdapter()
            {
                public void windowClosing(java.awt.event.WindowEvent e)
                {
                System.exit(0);
                };
            });

  		MatchApplet ut = new MatchApplet();
  		ut.setSize(900,900); // same size as defined in the HTML APPLET
  		f.add(ut);
  		f.pack();
  		ut.init();
  		f.setSize(900,900 + 100); // add 20, seems enough for the Frame title,
  		f.show();
		//end of application for program

	}//end of public static void main(String[] args)



  	public void init()
	{

		try
		{
                BufferedReader in1 = new BufferedReader(new FileReader("C:\\Users\\Serene\\Documents\\Major Project\\Alignment Algorithms\\1a00(50).txt"));	//reading files in specified directory
                BufferedReader in2 = new BufferedReader(new FileReader("C:\\Users\\Serene\\Documents\\Major Project\\Alignment Algorithms\\1a0a(50).txt"));	//reading files in specified directory

                FileInputStream read = new FileInputStream("C:\\Users\\Serene\\Documents\\Major Project\\Alignment Algorithms\\Testing.txt");
                DataInputStream read1 = new DataInputStream(read); // Get the object of DataInputStream
                BufferedReader in = new BufferedReader(new InputStreamReader(read1));

  			String str1;
			while ((str1 = in1.readLine()) != null)	//file reading
			{
                            display1 = str1;
                            System.out.print(display1);
  			}
  			in1.close();

  			System.out.println("");

			String str2;
			while ((str2 = in2.readLine()) != null)	//file reading
			{
                            display2 = str2;
                            System.out.print(display2);
  			}
			in2.close();

                        String str;
			while ((str = in.readLine()) != null)	//file reading
			{
                            /*StringTokenizer st = new StringTokenizer(str);
                            while (st.hasMoreTokens())
                            {
                            System.out.print(st.nextToken());
                            }*/


                            System.out.println(java.util.Arrays.toString(str.split("  ")).trim());
                            display = str;
                            mydisplay = Double.parseDouble(display);
                            mydouble[a][b] = mydisplay;
                            System.out.print(mydouble[a][b]);
  			}
			in.close();




		Container c = getContentPane();
		c.setLayout(new FlowLayout());

		outputArea = new TextArea(40,110);
		Font font = new Font("Courier", Font.PLAIN, 12);
		outputArea.setFont(font);
		outputArea.setEditable(false);

		tF1 = new JTextField(display1);
		tF2 = new JTextField(display2);

		l1 = new JLabel("Sequence 1:");
		l2 = new JLabel("Sequence 2:");

		c.add(l1);
		c.add(tF1);
		c.add(l2);
		c.add(tF2);
		c.add(outputArea);


	final Substitution sub = new Blosum50();

		s1 += tF1.getText();
		s2 += tF2.getText();
		Output out = new Output ()
		{
	  		public void print(String s)
	  		{ outputArea.append(s); }

	  		public void println(String s)
	  		{ outputArea.append(s); outputArea.append("\n"); }

	  		public void println()
	  		{ outputArea.append("\n"); }
		};

	outputArea.setText("");
      (new NW      (sub, 8,     s1, s2)).domatch(out, "GLOBAL ALIGNMENT");
      (new SW      (sub, 8,     s1, s2)).domatch(out, "LOCAL ALIGNMENT");


      }catch( IOException ioException ) {}

  }//end of init()

}//end of class

Here is the codes.

~s.o.s~ and I have already told you what the problem is. You still haven't done what we said. Read our posts again.

I thought I did? Line 98 to 102?

You carefully trim and split the string to print it (that's good), but then you ignore that and try to parse the original un-split string.

Oh. But how would I know which is the splited string now? What variable?

You don't have a variable with the split string in it yet. You need to create a new String array to hold the split-up string, then loop thru that array parsing the Doubles one at a time.

Erm JamesCherrill, can you please explain more in detail? I don't quite get it.

I don't know how to make that any clearer without giving you the actual code - which is against the forum guidelines. Just re-read all the previous posts very carefully and slowly.

import java.io.*;
import java.awt.*;
import javax.swing.*;

class MatchApplet extends JApplet {


	TextArea outputArea;
	JButton button;
	JButton reset;

	JTextField tF1;
	JTextField tF2;

	JLabel l1;
	JLabel l2;

	String s1;
	String s2;

        static String display1 = "";
  	static String display2 = "";
        static String display = "";
        static double mydisplay;

        static double[][] mydouble = new double [1000][1000];
        int a, b;

        String temp;




	public static void main(String[] args)
	{
            //Application for program
            Frame f = new Frame();
            f.addWindowListener(new java.awt.event.WindowAdapter()
            {
                public void windowClosing(java.awt.event.WindowEvent e)
                {
                System.exit(0);
                };
            });

  		MatchApplet ut = new MatchApplet();
  		ut.setSize(900,900); // same size as defined in the HTML APPLET
  		f.add(ut);
  		f.pack();
  		ut.init();
  		f.setSize(900,900 + 100); // add 20, seems enough for the Frame title,
  		f.show();
		//end of application for program

	}//end of public static void main(String[] args)



  	public void init()
	{

		try
		{
                BufferedReader in1 = new BufferedReader(new FileReader("C:\\Users\\Serene\\Documents\\Major Project\\Alignment Algorithms\\1a00(50).txt"));	//reading files in specified directory
                BufferedReader in2 = new BufferedReader(new FileReader("C:\\Users\\Serene\\Documents\\Major Project\\Alignment Algorithms\\1a0a(50).txt"));	//reading files in specified directory

                /*FileInputStream read = new FileInputStream("C:\\Users\\Serene\\Documents\\Major Project\\Alignment Algorithms\\Testing.txt");
                DataInputStream read1 = new DataInputStream(read); // Get the object of DataInputStream
                BufferedReader in = new BufferedReader(new InputStreamReader(read1));*/

  			String str1;
			while ((str1 = in1.readLine()) != null)	//file reading
			{
                            display1 = str1;
                            System.out.print(display1);
  			}
  			in1.close();

  			System.out.println("");

			String str2;
			while ((str2 = in2.readLine()) != null)	//file reading
			{
                            display2 = str2;
                            System.out.print(display2);
  			}
			in2.close();

                        /*String str;
			while ((str = in.readLine()) != null)	//file reading
			{
                            /*StringTokenizer st = new StringTokenizer(str);
                            while (st.hasMoreTokens())
                            {
                            System.out.print(st.nextToken());
                            }*/


                            //System.out.println(java.util.Arrays.toString(str.split("  ")).trim());


                            /*temp = java.util.Arrays.toString(str.split("  ")).trim();
                            display = temp;

                            
                            mydisplay = Double.parseDouble(display);
                            mydouble[a][b] = mydisplay;
                            System.out.print(mydouble[a][b]);


                            
  			}
			in.close();*/

        try
        {
            BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\Serene\\Documents\\Major Project\\Alignment Algorithms\\Testing.txt"));	//reading files in specified directory

            String str;
            while ((str = in.readLine()) != null)	//file reading
            {

                temp = java.util.Arrays.toString(str.split("  ")).trim();
                display = temp;

                mydisplay = Double.parseDouble(display);
                mydouble[a][b] = mydisplay;
                System.out.print(mydouble[a][b]);

            }
            in.close();

        
        }catch( NumberFormatException e ) {}



		Container c = getContentPane();
		c.setLayout(new FlowLayout());

		outputArea = new TextArea(40,110);
		Font font = new Font("Courier", Font.PLAIN, 12);
		outputArea.setFont(font);
		outputArea.setEditable(false);

		tF1 = new JTextField(display1);
		tF2 = new JTextField(display2);

		l1 = new JLabel("Sequence 1:");
		l2 = new JLabel("Sequence 2:");

		c.add(l1);
		c.add(tF1);
		c.add(l2);
		c.add(tF2);
		c.add(outputArea);


	final Substitution sub = new Blosum50();

		s1 += tF1.getText();
		s2 += tF2.getText();
		Output out = new Output ()
		{
	  		public void print(String s)
	  		{ outputArea.append(s); }

	  		public void println(String s)
	  		{ outputArea.append(s); outputArea.append("\n"); }

	  		public void println()
	  		{ outputArea.append("\n"); }
		};

	outputArea.setText("");
      (new NW      (sub, 8,     s1, s2)).domatch(out, "GLOBAL ALIGNMENT");
      (new SW      (sub, 8,     s1, s2)).domatch(out, "LOCAL ALIGNMENT");


      }catch( IOException ioException ) {}


  }//end of init()

}//end of class

Okay this is what I've done up so far. I can run my program already, but the output is wrong. It seems like there's smth wrong with the file read part where I try to parse it. I don't know why it can't work. It should read the file (Testing) and parse it then passed into another part of my program, but the info passed is not right. Lines 115-134

Getting closer, but you still need to parse the split-up numbers in the string one at a time in a loop, a bit like this:

String[] temp;    // temp is an array, one element for each number
temp = str.split(",");  // split up using the commas as deimiters
for (String s : temp) {   // for each of the elemnets in temp
   s = s.trim();   // remove extra blanks
   Double d = Double.parseDouble(s); // parse the number thateremians
   // do whatever with d (print it first to see if its right)
}

Line 3: for (String s : temp) {

You are equating temp to s?

import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.util.*;


public class Testing
{


	public static void main(String[] args)
	{

	String[] temp;


		try{

		BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\Serene\\Documents\\Major Project\\Alignment Algorithms\\Testing.txt"));	//reading files in specified directory

		String str;
		while ((str = in.readLine()) != null)	//file reading
		{
                temp = str.split(" ");

                for (String s : temp)
                {                       // for each of the elemnets in temp
                s = s.trim();           // remove extra blanks
                Double d = Double.parseDouble(s); // parse the number thateremians
				System.out.println(d);
                }    // do whatever with d (print it first to see if its right)





  		}
		in.close();



		}catch( IOException ioException ) {}
	}
}

This is what I've done so far for the reading part. It can be compiled, but it cannot run. The error says empty string. Why is it empty? It is because of the for loop?

Like I said, find someone locally who can sit beside you as you don't seem to be able to digest what we're telling you here. You've been told about split (and other things) more than a few times (for weeks now) and you still can't seem to get it right. Not that I know why you included the braces ({}) in your text file, but hey.

Line 24 you split on " " but I suggested split on the commas "," so try that.
Your file should NOT have any { or }

If you get an error you must ALWAYS post the full message, including the line number where it happened.

I'm sticking with this because you do seem to be trying, I'm hoping that when you get this working you will then understand the things that are currently blocking your progress, but Masijade has a good point.

I'm sticking with this because you do seem to be trying

I agree, he is and I commend both of you. I just think that he could really benefit from "hands-on" help as he seems to be having a really hard time of it on the forum.

My file do not have any { or }, it only has numbers (decimal numbers, both positive and negative).


Exception in thread "main" java.lang.NumberFormatException: For input string: " 1.0 2.0 3.0 4.0 5.0 1.0 2.0 3.0 4.0 5.0 1.0 2.0 3.0 4.0 5.0 1.0 2.0 3.0 4.0 5.0 1.0 2.0 3.0 4.0 5.0 1.0 2.0 3.0 4.0 5.0"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1224)
at java.lang.Double.parseDouble(Double.java:510)
at Testing.main(Testing.java:43)

This is the error now.

I have absolutely no idea why you are trying to pass the whole string to parseDouble after all the work we have done on split. The latest code you posted doesn't even have a relevant line 43, so I don't know what code you are executing here.
Is there anybody who can sit with you and give hands-on help?

Exception in thread "main" java.lang.NumberFormatException: For input string: " 1.0 2.0 3.0 4.0 5.0 1.0 2.0 3.0 4.0 5.0 1.0 2.0 3.0 4.0 5.0 1.0 2.0 3.0 4.0 5.0 1.0 2.0 3.0 4.0 5.0 1.0 2.0 3.0 4.0 5.0"

OK, let's try it this way: try answering the questions I post.

What do you understand of the error message which you posted? What do you think is the runtime trying to tell you? Can you think of a way wherein you can do away with these runtime exceptions?

Also a suggestion: put aside the entire code you have written for the time being and start afresh. Write a *minimal* piece of code which reproduces the problem for you. It need not be reading from a file; hard-code a string (or string array) and assume that it was read from the file and write a program to achieve the same.

I have no one to help at all. Sigh, it's alright then. I will try and figure it out myself.

Thanks people. I appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.