8 Replies - 12726 Views - Last Post: 07 March 2009 - 10:48 AM Rate Topic: -----

#1 appleheart2008  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-January 09

How to convert string array into float array

Post icon  Posted 07 March 2009 - 07:20 AM

Can anyone help me please how to convert my string array into a float array...
my program is as follows....
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.String.*;
import java.lang.String;




/**
 *
 * @author userad
 */
public class Float extends HttpServlet {
   
	/** 
	 * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	protected void processRequest(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();

		try {
		   String num[] = request.getParameterValues ("num");

		   
			out.println("<html>");
			out.println("<head>");
			out.println("<title>Servlet Float</title>");  
			out.println("</head>");
			out.println("<body>");
			out.println("<h1>Servlet Float at " + request.getContextPath () + "</h1>");
			int iter=0;
			String allNum = "";
			String submitbutton ="wala";
			
			out.println("<form action =\"/FloatAverage/Float\" method=\"post\">" +

					"NUMBER 1 : <input type=\"text\" name=\"num\" /><br/>" +
					"NUMBER 2 : <input type=\"text\" name=\"num\" /><br/>" +
					"NUMBER 3 : <input type=\"text\" name=\"num\" /><br/>" +
					"NUMBER 4 : <input type=\"text\" name=\"num\" /><br/>" +
					"NUMBER 5 : <input type=\"text\" name=\"num\" /><br/>" +
					"NUMBER 6 : <input type=\"text\" name=\"num\" /><br/>" +
					"NUMBER 7 : <input type=\"text\" name=\"num\" /><br/>" +
					"NUMBER 8 : <input type=\"text\" name=\"num\" /><br/>" +
					"NUMBER 9 : <input type=\"text\" name=\"num\" /><br/>" +
					"NUMBER 10 : <input type=\"text\" name=\"num\" /><br/>" +
					"<input type=\"submit\" name=\"submitbutton\" value=\"Submit\">" +
					"</form>");

			if (submitbutton.equals("Submit"));
			{
				for (int x=0; x<num.length; x++)
				{
				out.println(num[x]+"<br/>" );
				}
			}

		   
			out.println("</body>");
			out.println("</html>");
		   
		} finally {
		   
			out.close();
		}

	}

	// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
	/** 
	 * Handles the HTTP <code>GET</code> method.
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		processRequest(request, response);
	} 

	/** 
	 * Handles the HTTP <code>POST</code> method.
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		processRequest(request, response);
	}

	/** 
	 * Returns a short description of the servlet.
	 * @return a String containing servlet description
	 */
	@Override
	public String getServletInfo() {
		return "Short description";
	}// </editor-fold>

}



Is This A Good Question/Topic? 0
  • +

Replies To: How to convert string array into float array

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: How to convert string array into float array

Posted 07 March 2009 - 07:54 AM

if you know your strings contain real numbers representated by a series of characters (e.g. digits 0 to 9 - + .) you can use parseFloat() from the Float class
http://java.sun.com/...lang/Float.html

e.g.
		String s="3.141526";
		float pi=Float.parseFloat(s);
		System.out.printf("String " + s + " float " + pi);


when run gives
String 3.141526 float 3.141526

if there is a possibilty some of the strings may contain other information you should catch NumberFormatException
http://java.sun.com/...tException.html

This post has been edited by horace: 07 March 2009 - 07:55 AM

Was This Post Helpful? 0
  • +
  • -

#3 appleheart2008  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-January 09

Re: How to convert string array into float array

Posted 07 March 2009 - 08:19 AM

i still don't get it.....i tried doing it this way
public Float[] convertStringArraytoFloatArray(String[] num) throws Exception {  
if (num != null) {  
Float floatarray[] = new Float[num.length];  
for (int i = 0; i <num.length; i++) {  
   
	floatarray[i] = Float.parseFloat(num[i]);
	
}  
return floatarray;  
}  
return null;  
} 


but i still have an error....and it says
C:\Documents and Settings\mommyluvko\Desktop\JAVA EE\JAVA EE 1ST DAY\FloatAverage\src\java\Float.java:94: cannot find symbol
symbol : method parseFloat(java.lang.String)
location: class Float
floatarray[i] = Float.parseFloat(num[i]);
1 error
Was This Post Helpful? 0
  • +
  • -

#4 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: How to convert string array into float array

Posted 07 March 2009 - 08:42 AM

View Postappleheart2008, on 7 Mar, 2009 - 02:19 PM, said:

but i still have an error....and it says
C:\Documents and Settings\mommyluvko\Desktop\JAVA EE\JAVA EE 1ST DAY\FloatAverage\src\java\Float.java:94: cannot find symbol
symbol : method parseFloat(java.lang.String)
location: class Float
floatarray[i] = Float.parseFloat(num[i]);
1 error

it looks like you have called your class Float. This means when you call
	floatarray[i] = Float.parseFloat(num[i]);


it looks in your class Float for method parseFloat()

you could try
	floatarray[i] = java.lang.Float.parseFloat(num[i]);


or change your class name - I would try to avoid calling classes by names that java uses
Was This Post Helpful? 0
  • +
  • -

#5 appleheart2008  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-January 09

Re: How to convert string array into float array

Posted 07 March 2009 - 09:44 AM

this is my new code...but it's still not working....
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */




import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.Float.*;
import java.lang.String.*;
import java.lang.String;






/**
 *
 * @author userad
 */
public class Compute extends HttpServlet {

	/**
	 * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	protected void processRequest(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();

		try {
		   String num[] = request.getParameterValues ("num");


			out.println("<html>");
			out.println("<head>");
			out.println("<title>Servlet Float</title>");
			out.println("</head>");
			out.println("<body>");
			out.println("<h1>Servlet Float at " + request.getContextPath () + "</h1>");
			int iter=0;
			String allNum = "";
			String submitbutton ="wala";

			out.println("<form action =\"/FloatAverage/Compute\" method=\"post\">" );

			out.println(		"NUMBER 1 : <input type=\"text\" name=\"num\" /><br/>");


			out.println(		"NUMBER 2 : <input type=\"text\" name=\"num\" /><br/>");
			out.println(		"NUMBER 3 : <input type=\"text\" name=\"num\" /><br/>");
			out.println(		"NUMBER 4 : <input type=\"text\" name=\"num\" /><br/>");
			out.println(		"NUMBER 5 : <input type=\"text\" name=\"num\" /><br/>");
			out.println(		"NUMBER 6 : <input type=\"text\" name=\"num\" /><br/>");
			out.println(		"NUMBER 7 : <input type=\"text\" name=\"num\" /><br/>");
			out.println(		"NUMBER 8 : <input type=\"text\" name=\"num\" /><br/>");
			out.println(		"NUMBER 9 : <input type=\"text\" name=\"num\" /><br/>");
			out.println(		"NUMBER 10 : <input type=\"text\" name=\"num\" /><br/>");
			out.println(		"<input type=\"submit\" name=\"submitbutton\" value=\"Submit\">");
			out.println(		"</form>");
			Float farray[] = new Float[num.length];
			Float[] convertStringArraytoFloatArray(String[] num);



			if (submitbutton.equals("Submit"));
			{



				for (int x=0; x<farray.length; x++)
				{
				out.println(farray[x]+"<br/>" );

				}
			}


			out.println("</body>");
			out.println("</html>");

		} finally {

			out.close();
		}

	}
public Float[] convertStringArraytoFloatArray(String[] num) throws Exception {
if (num != null) {
Float farray[] = new Float[num.length];
for (int i = 0; i <num.length; i++) {

 farray[i] = java.lang.Float.parseFloat(num[i]);

}
return farray;
}
return null;
}






  // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
	/**
	 * Handles the HTTP <code>GET</code> method.
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * Handles the HTTP <code>POST</code> method.
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * Returns a short description of the servlet.
	 * @return a String containing servlet description
	 */
	@Override
	public String getServletInfo() {
		return "Short description";
	}// </editor-fold>

}





the errors are...
C:\Documents and Settings\mommyluvko\Desktop\JAVA EE\JAVA EE 1ST DAY\FloatAverage\src\java\Compute.java:73: ';' expected
Float[] convertStringArraytoFloatArray(String[] num);
C:\Documents and Settings\mommyluvko\Desktop\JAVA EE\JAVA EE 1ST DAY\FloatAverage\src\java\Compute.java:73: ';' expected
Float[] convertStringArraytoFloatArray(String[] num);
2 errors
C:\Documents and Settings\mommyluvko\Desktop\JAVA EE\JAVA EE 1ST DAY\FloatAverage\nbproject\build-impl.xml:383: The following error occurred while executing this line:
C:\Documents and Settings\mommyluvko\Desktop\JAVA EE\JAVA EE 1ST DAY\FloatAverage\nbproject\build-impl.xml:211: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 0 seconds)
Was This Post Helpful? 0
  • +
  • -

#6 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: How to convert string array into float array

Posted 07 March 2009 - 09:57 AM

the following method call is wrong
   Float[] convertStringArraytoFloatArray(String[] num);


try
	  farray= convertStringArraytoFloatArray(num);


This post has been edited by horace: 07 March 2009 - 09:57 AM

Was This Post Helpful? 0
  • +
  • -

#7 appleheart2008  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-January 09

Re: How to convert string array into float array

Posted 07 March 2009 - 10:17 AM

i've change
Float[] convertStringArraytoFloatArray(String[] num);


to
farray= convertStringArraytoFloatArray(num);



now it seems the error is like this....
C:\Documents and Settings\mommyluvko\Desktop\JAVA EE\JAVA EE 1ST DAY\FloatAverage\src\java\Compute.java:74: unreported exception java.lang.Exception; must be caught or declared to be thrown
farray= convertStringArraytoFloatArray(num);
1 error
C:\Documents and Settings\mommyluvko\Desktop\JAVA EE\JAVA EE 1ST DAY\FloatAverage\nbproject\build-impl.xml:383: The following error occurred while executing this line:
C:\Documents and Settings\mommyluvko\Desktop\JAVA EE\JAVA EE 1ST DAY\FloatAverage\nbproject\build-impl.xml:211: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 0 seconds)


it says unreported exception...it must be caught or thrown......but i don't know how to do it......
Was This Post Helpful? 0
  • +
  • -

#8 appleheart2008  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-January 09

Re: How to convert string array into float array

Posted 07 March 2009 - 10:28 AM

i'm having a hard time converting the string array to float array......my program is supposed to find the average of the 10 input string that should be converted to float.....before i can get it's average......:(

This post has been edited by appleheart2008: 07 March 2009 - 10:29 AM

Was This Post Helpful? 0
  • +
  • -

#9 appleheart2008  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-January 09

Re: How to convert string array into float array

Posted 07 March 2009 - 10:48 AM

I've finally got the exception!!

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */




import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.Float.*;
import java.lang.String.*;
import java.lang.String;






/**
 *
 * @author userad
 */
public class Compute extends HttpServlet {

	/**
	 * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	protected void processRequest(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();

		try {
		   String num[] = request.getParameterValues ("num");


			out.println("<html>");
			out.println("<head>");
			out.println("<title>Servlet Float</title>");
			out.println("</head>");
			out.println("<body>");
			out.println("<h1>Servlet Float at " + request.getContextPath () + "</h1>");
			int iter=0;
			String allNum = "";
			String submitbutton ="wala";

			out.println("<form action =\"/FloatAverage/Compute\" method=\"post\">" );

			out.println(		"NUMBER 1 : <input type=\"text\" name=\"num\" /><br/>");


			out.println(		"NUMBER 2 : <input type=\"text\" name=\"num\" /><br/>");
			out.println(		"NUMBER 3 : <input type=\"text\" name=\"num\" /><br/>");
			out.println(		"NUMBER 4 : <input type=\"text\" name=\"num\" /><br/>");
			out.println(		"NUMBER 5 : <input type=\"text\" name=\"num\" /><br/>");
			out.println(		"NUMBER 6 : <input type=\"text\" name=\"num\" /><br/>");
			out.println(		"NUMBER 7 : <input type=\"text\" name=\"num\" /><br/>");
			out.println(		"NUMBER 8 : <input type=\"text\" name=\"num\" /><br/>");
			out.println(		"NUMBER 9 : <input type=\"text\" name=\"num\" /><br/>");
			out.println(		"NUMBER 10 : <input type=\"text\" name=\"num\" /><br/>");
			out.println(		"<input type=\"submit\" name=\"submitbutton\" value=\"Submit\">");
			out.println(		"</form>");
			Float farray[] = new Float[num.length];
		   try {
			farray= convertStringArraytoFloatArray(num);
		   }
		   catch(Exception e){
			 out.println("NumberFormat Exception");
		   }
			   

			//Float[] convertStringArraytoFloatArray(String[] num);



			if (submitbutton.equals("Submit"));
			{



				for (int x=0; x<farray.length; x++)
				{
				out.println(farray[x]+"<br/>" );

				}
			}


			out.println("</body>");
			out.println("</html>");

		} finally {

			out.close();
		}

	}
public Float[] convertStringArraytoFloatArray(String[] num) throws Exception {
if (num != null) {
Float farray[] = new Float[num.length];
for (int i = 0; i <num.length; i++) {

 farray[i] = java.lang.Float.parseFloat(num[i]);

}
return farray;
}
return null;
}






  // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
	/**
	 * Handles the HTTP <code>GET</code> method.
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * Handles the HTTP <code>POST</code> method.
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * Returns a short description of the servlet.
	 * @return a String containing servlet description
	 */
	@Override
	public String getServletInfo() {
		return "Short description";
	}// </editor-fold>

}




horace than you very much for your help hopefully...i will be able to finish my program before i go to sleep..........it's already 1:47 am here. Thank you!

This post has been edited by appleheart2008: 07 March 2009 - 10:51 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1