954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help with "Exception in thread "main" java.lang.NumberFormatException:"

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?

Sunshineserene
Junior Poster
187 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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); }
}
Sunshineserene
Junior Poster
187 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

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?

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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

Sunshineserene
Junior Poster
187 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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

Sunshineserene
Junior Poster
187 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

Show us the relevant code & data.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
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.

Sunshineserene
Junior Poster
187 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

~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.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

I thought I did? Line 98 to 102?

Sunshineserene
Junior Poster
187 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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

Sunshineserene
Junior Poster
187 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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

Sunshineserene
Junior Poster
187 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
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

Sunshineserene
Junior Poster
187 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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)
}
JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

Line 3: for (String s : temp) {

You are equating temp to s?

Sunshineserene
Junior Poster
187 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 
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?

Sunshineserene
Junior Poster
187 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: