<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Linea de Codigo &#187; Java</title>
	<atom:link href="http://lineadecodigo.com/categoria/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://lineadecodigo.com</link>
	<description>/* Programación en la red */</description>
	<lastBuildDate>Tue, 07 Sep 2010 06:00:40 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Calcular signo del zodiaco con Java</title>
		<link>http://lineadecodigo.com/java/calcular-signo-del-zodiaco-con-java/</link>
		<comments>http://lineadecodigo.com/java/calcular-signo-del-zodiaco-con-java/#comments</comments>
		<pubDate>Sun, 05 Sep 2010 06:00:19 +0000</pubDate>
		<dc:creator>lineadecodigo</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[if]]></category>
		<category><![CDATA[nextInt]]></category>
		<category><![CDATA[Scanner]]></category>
		<category><![CDATA[signo zodiaco]]></category>
		<category><![CDATA[switch]]></category>

		<guid isPermaLink="false">http://lineadecodigo.com/?p=2576</guid>
		<description><![CDATA[En este ejemplo vamos a ver como, mediante el lenguaje Java, podemos saber el signo del zodiaco de una persona, dada su fecha de nacimiento (día y mes).
Lo primero será pedirle al usuario su día y mes de nacimiento. Esto lo hacemos accediendo a la consola con la clase Scanner:
Scanner reader = new Scanner&#40;System.in&#41;;&#160;System.out.println&#40;&#34;Qué día [...]]]></description>
			<content:encoded><![CDATA[<p>En este ejemplo vamos a ver como, mediante el lenguaje <a href="http://www.manualweb.net/tutorial-java/" title="Java">Java</a>, podemos saber el signo del zodiaco de una persona, dada su fecha de nacimiento (día y mes).</p>
<p>Lo primero será pedirle al usuario su día y mes de nacimiento. Esto lo hacemos <a href="http://lineadecodigo.com/java/lectura-de-datos-en-java-con-scanner/" title="lectura de consola con Scanner">accediendo a la consola con la clase Scanner</a>:</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Scanner reader = <span style="color: #000000; font-weight: bold;">new</span> Scanner<span style="color: #66cc66;">&#40;</span><a href="http://w3api.com/wiki/Java:System"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">in</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://w3api.com/wiki/Java:System"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Qué día naciste&quot;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">dia = reader.<span style="color: #006600;">nextInt</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://w3api.com/wiki/Java:System"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Qué mes naciste&quot;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mes = reader.<span style="color: #006600;">nextInt</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li></ol></pre>
<p>Una vez que tenemos el día y el mes veamos la lógica a aplicar. En un mes solo puede haber dos signos del zodiaco. Por ejemplo, en Enero, si naciste antes del 21 eres Capricornio y si naciste el 21 o algún día posterior del mes de Enero, eres Acuario.</p>
<p>Otro ejemplo, el mes de noviembre. Si naciste antes del 23 eres Escorpio y si naciste el mismo 23 o día superior eres Sagitario.</p>
<p>Con esta dedución vemos que las estructuras a aplicar son sencillas. En primer lugar utilizremos la sentencia switch pasa posicionarnos en el mes:</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span>mes<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">1</span>:</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #808080; font-style: italic;">// Enero</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000; font-weight: bold;">break</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">2</span>:</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #808080; font-style: italic;">// Febrero</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000; font-weight: bold;">break</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  ...</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>
<p>Y luego, dentro de cada mes la sentencia de decisión que aplicaremos mediante una sentencia if:</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span>mes<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">1</span>:</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #808080; font-style: italic;">// Enero</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>dia&gt;=<span style="color: #cc66cc;">21</span><span style="color: #66cc66;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      <a href="http://w3api.com/wiki/Java:System"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Acuario&quot;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #b1b100;">else</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      <a href="http://w3api.com/wiki/Java:System"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Capricornio&quot;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000; font-weight: bold;">break</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">2</span>:</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #808080; font-style: italic;">// Febrero</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>dia&gt;=<span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      <a href="http://w3api.com/wiki/Java:System"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Piscis&quot;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #b1b100;">else</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">      <a href="http://w3api.com/wiki/Java:System"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Acuario&quot;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <span style="color: #000000; font-weight: bold;">break</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  ...</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>
<p>Como vemos la implementación es muy sencilla y nos ayuda a enteder fácilmente las sentencias de decisión if y switch.<strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://lineadecodigo.com/java/sumar-dos-numeros-con-java/" rel="bookmark" title="Enero 22, 2009">Sumar dos números con Java</a></li>
<li><a href="http://lineadecodigo.com/java/restar-dos-numeros-con-java/" rel="bookmark" title="Septiembre 4, 2010">Restar dos números con Java</a></li>
<li><a href="http://lineadecodigo.com/java/numeros-divisibles-con-java/" rel="bookmark" title="Julio 28, 2009">Números divisibles con Java</a></li>
<li><a href="http://lineadecodigo.com/java/notas-americanas/" rel="bookmark" title="Abril 9, 2007">Notas Americanas</a></li>
<li><a href="http://lineadecodigo.com/java/ultimo-digito-de-un-numero-con-java/" rel="bookmark" title="Enero 11, 2009">Último dígito de un número con Java</a></li>
</ul>
<p><!-- Similar Posts took 3.854 ms --></p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2.1] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Flineadecodigo.com%2Fjava%2Fcalcular-signo-del-zodiaco-con-java%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:hidden; width:450px; height:80px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2.1] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://lineadecodigo.com/java/calcular-signo-del-zodiaco-con-java/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Restar dos números con Java</title>
		<link>http://lineadecodigo.com/java/restar-dos-numeros-con-java/</link>
		<comments>http://lineadecodigo.com/java/restar-dos-numeros-con-java/#comments</comments>
		<pubDate>Sat, 04 Sep 2010 06:00:12 +0000</pubDate>
		<dc:creator>lineadecodigo</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[nextInt]]></category>
		<category><![CDATA[numeros]]></category>
		<category><![CDATA[resta]]></category>
		<category><![CDATA[Scanner]]></category>

		<guid isPermaLink="false">http://lineadecodigo.com/?p=2570</guid>
		<description><![CDATA[Ya teníamos publicados un grupo de ejemplos que nos enseñaban a realizar algunas de las operaciones matemáticas básicas: suma, multiplicación y división. Si bien, como muchos nos habéis indicado por email nos faltaba la resta.
Vamos a ver como poder implementar una resta, si bien veremos que el procedimiento es totalmente idéntico a los anteriores y [...]]]></description>
			<content:encoded><![CDATA[<p>Ya teníamos publicados un grupo de ejemplos que nos enseñaban a realizar algunas de las operaciones matemáticas básicas: <a href="http://lineadecodigo.com/java/sumar-dos-numeros-con-java/" title="sumar dos numeros con java">suma,</a> <a href="http://lineadecodigo.com/java/multiplicar-dos-numeros-con-java/" title="multiplicar dos numeros con java">multiplicación</a> y <a href="http://lineadecodigo.com/java/dividir-dos-numeros-con-java/" title="dividir dos numeros con java">división</a>. Si bien, como muchos nos habéis indicado por email nos faltaba la resta.</p>
<p>Vamos a ver como poder implementar una resta, si bien veremos que el procedimiento es totalmente idéntico a los anteriores y que solo cambiará la operación matemática en cuestión.</p>
<p>Lo primero que hacemos es crear una clase <a href="http://w3api.com/wiki/Java:Scanner" title="Scanner">Scanner</a> para pedir los datos al usuario</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Scanner reader = <span style="color: #000000; font-weight: bold;">new</span> Scanner<span style="color: #66cc66;">&#40;</span><a href="http://w3api.com/wiki/Java:System"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">in</span><span style="color: #66cc66;">&#41;</span>;</div></li></ol></pre>
<p>Acto seguido le pedimos los dos números a restar:</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://w3api.com/wiki/Java:System"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Introduce el primer número:&quot;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">numero1 = reader.<span style="color: #006600;">nextInt</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://w3api.com/wiki/Java:System"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Introduce el segundo número:&quot;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">numero2 = reader.<span style="color: #006600;">nextInt</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li></ol></pre>
<p>La lectura de lo que ha insertado el usuario la realizamos mediante el método <a href="http://w3api.com/wiki/Java:Scanner.nextInt()" title="nextInt()">nextInt()</a>. Ya que esperamos que el usuario haya insertado números por consola.</p>
<p>Ahora solo nos quedará ejecutar la resta:</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">resultado = numero1-numero2;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li></ol></pre>
<p>Y mostrar nuestro resultado de la resta, por pantalla:</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://w3api.com/wiki/Java:System"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;La resta es &quot;</span> + numero1 + <span style="color: #ff0000;">&quot; - &quot;</span> + numero2 + <span style="color: #ff0000;">&quot; = &quot;</span> + resultado<span style="color: #66cc66;">&#41;</span>;</div></li></ol></pre>
<p><strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://lineadecodigo.com/java/sumar-dos-numeros-con-java/" rel="bookmark" title="Enero 22, 2009">Sumar dos números con Java</a></li>
<li><a href="http://lineadecodigo.com/java/dividir-dos-numeros-con-java/" rel="bookmark" title="Junio 10, 2010">Dividir dos números con Java</a></li>
<li><a href="http://lineadecodigo.com/java/numeros-divisibles-con-java/" rel="bookmark" title="Julio 28, 2009">Números divisibles con Java</a></li>
<li><a href="http://lineadecodigo.com/java/ultimo-digito-de-un-numero-con-java/" rel="bookmark" title="Enero 11, 2009">Último dígito de un número con Java</a></li>
<li><a href="http://lineadecodigo.com/java/multiplicar-dos-numeros-con-java/" rel="bookmark" title="Diciembre 30, 2008">Multiplicar dos números con Java</a></li>
</ul>
<p><!-- Similar Posts took 2.945 ms --></p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2.1] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Flineadecodigo.com%2Fjava%2Frestar-dos-numeros-con-java%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:hidden; width:450px; height:80px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2.1] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://lineadecodigo.com/java/restar-dos-numeros-con-java/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Vaciar el contenido de un fichero mediante borrado y creación</title>
		<link>http://lineadecodigo.com/java/vaciar-el-contenido-de-un-fichero-mediante-borrado-y-creacion/</link>
		<comments>http://lineadecodigo.com/java/vaciar-el-contenido-de-un-fichero-mediante-borrado-y-creacion/#comments</comments>
		<pubDate>Sun, 01 Aug 2010 06:00:04 +0000</pubDate>
		<dc:creator>lineadecodigo</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[contenido]]></category>
		<category><![CDATA[createNewFile]]></category>
		<category><![CDATA[delete]]></category>
		<category><![CDATA[File]]></category>
		<category><![CDATA[IOException]]></category>
		<category><![CDATA[vaciar]]></category>

		<guid isPermaLink="false">http://lineadecodigo.com/?p=2545</guid>
		<description><![CDATA[Si ayer veíamos como Vaciar el contenido de un fichero con Java. Hoy vamos a proponer otra alternativa a lo explicado en dicho artículo.
En este caso el procedimiento para vaciar el contenido de un fichero con Java será el de borrar el fichero y volverlo a crear. Un procedimiento mucho más brusco que el anterior. [...]]]></description>
			<content:encoded><![CDATA[<p>Si ayer veíamos como <a href="http://lineadecodigo.com/java/vaciar-el-contenido-de-un-fichero-con-java/" title="Vaciar el contenido de un fichero con Java">Vaciar el contenido de un fichero con Java</a>. Hoy vamos a proponer otra alternativa a lo explicado en dicho artículo.</p>
<p>En este caso el procedimiento para vaciar el contenido de un fichero con Java será el de borrar el fichero y volverlo a crear. Un procedimiento mucho más brusco que el anterior. <img src='http://lineadecodigo.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>Hay que tener especial cuidado ya que en ciertos entornos podemos manipular el contenido de un fichero, si bien no tenemos capacidades de borrado y creación de los mismos. Es por ello que este código no valdría. De igual manera es brusco, ya que el proceso podría realizar solo uno de los dos pasos -el borrado- y dejar incosistente el sistema. Es por ello que seguimos recomendando lo explicado en <a href="http://lineadecodigo.com/java/vaciar-el-contenido-de-un-fichero-con-java/" title="Vaciar el contenido de un fichero con Java">Vaciar el contenido de un fichero con Java</a>.</p>
<p>Si bien, vamos con ello... Con la clase <a href="http://w3api.com/wiki/Java:File" title="File">File</a> obtenemos una instancia del fichero que queremos manipular</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://w3api.com/wiki/Java:String"><span style="color: #aaaadd; font-weight: bold;">String</span></a> sFichero = <span style="color: #ff0000;">&quot;fichero1.txt&quot;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://w3api.com/wiki/Java:File"><span style="color: #aaaadd; font-weight: bold;">File</span></a> f = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://w3api.com/wiki/Java:File"><span style="color: #aaaadd; font-weight: bold;">File</span></a><span style="color: #66cc66;">&#40;</span>sFichero<span style="color: #66cc66;">&#41;</span>;</div></li></ol></pre>
<p>Ahora solo tenemos que enviar los dos comandos: el de borrado mediante el método <a href="http://w3api.com/wiki/Java:File.delete()">.delete()</a> y el de creación mediante el método <a href="http://w3api.com/wiki/Java:File.createNewFile()" title="createNewFile()">.createNewFile()</a></p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">f.<span style="color: #006600;">delete</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  f.<span style="color: #006600;">createNewFile</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><a href="http://w3api.com/wiki/Java:IOException"><span style="color: #aaaadd; font-weight: bold;">IOException</span></a> ioe<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  ioe.<span style="color: #006600;">printStackTrace</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>
<p>Cuando lanzamos el método <a href="http://w3api.com/wiki/Java:File.createNewFile()" title="createNewFile()">.createNewFile()</a> hay que tener cuidado ya que se puede elevar la excepción <a href="http://w3api.com/wiki/Java:IOException" title="IOException">IOException</a>.</p>
<p>Ya hemos visto que con unas pocas líneas podemos vaciar el contenido de un fichero mediante borrado y creación.<strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://lineadecodigo.com/java/vaciar-el-contenido-de-un-fichero-con-java/" rel="bookmark" title="Julio 31, 2010">Vaciar el contenido de un fichero con Java</a></li>
<li><a href="http://lineadecodigo.com/java/borrar-un-fichero-con-java/" rel="bookmark" title="Febrero 26, 2008">Borrar un fichero con Java</a></li>
<li><a href="http://lineadecodigo.com/java/crear-un-fichero-en-java/" rel="bookmark" title="Julio 21, 2007">Crear un fichero en Java</a></li>
<li><a href="http://lineadecodigo.com/java/conocer-el-tamano-de-un-fichero-con-java/" rel="bookmark" title="Julio 7, 2007">Conocer el tamaño de un fichero con Java</a></li>
<li><a href="http://lineadecodigo.com/asp/borrar-un-fichero-con-asp/" rel="bookmark" title="Enero 25, 2009">Borrar un fichero con ASP</a></li>
</ul>
<p><!-- Similar Posts took 4.549 ms --></p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2.1] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Flineadecodigo.com%2Fjava%2Fvaciar-el-contenido-de-un-fichero-mediante-borrado-y-creacion%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:hidden; width:450px; height:80px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2.1] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://lineadecodigo.com/java/vaciar-el-contenido-de-un-fichero-mediante-borrado-y-creacion/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Vaciar el contenido de un fichero con Java</title>
		<link>http://lineadecodigo.com/java/vaciar-el-contenido-de-un-fichero-con-java/</link>
		<comments>http://lineadecodigo.com/java/vaciar-el-contenido-de-un-fichero-con-java/#comments</comments>
		<pubDate>Sat, 31 Jul 2010 06:00:56 +0000</pubDate>
		<dc:creator>lineadecodigo</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[BufferedWriter]]></category>
		<category><![CDATA[close]]></category>
		<category><![CDATA[File]]></category>
		<category><![CDATA[FileWriter]]></category>
		<category><![CDATA[vaciar]]></category>
		<category><![CDATA[write]]></category>

		<guid isPermaLink="false">http://lineadecodigo.com/?p=2538</guid>
		<description><![CDATA[Nos llegaba una pregunta vía comentarios sobre como vaciar el contenido de un fichero con Java. Y nos pusimos manos a la obra.
El procedimiento es muy sencillo. Lo primero que tenemos que hacer es abrir un stream de escritura sobre el fichero. Para ello necesitaremos de un FileWriter y un BufferedWriter.
BufferedWriter bw = new BufferedWriter&#40;new [...]]]></description>
			<content:encoded><![CDATA[<p>Nos <a href="http://lineadecodigo.com/java/borrar-un-fichero-con-java/#49357" title="pregunta en comentarios">llegaba una pregunta vía comentarios</a> sobre como vaciar el contenido de un fichero con <a href="http://www.manualweb.net/tutorial-java/" title="Java">Java</a>. Y nos pusimos manos a la obra.</p>
<p>El procedimiento es muy sencillo. Lo primero que tenemos que hacer es abrir un stream de escritura sobre el fichero. Para ello necesitaremos de un <a href="http://w3api.com/wiki/Java:FileWriter" title="FileWriter">FileWriter</a> y un <a href="http://w3api.com/wiki/Java:BufferedWriter" title="BufferedWriter">BufferedWriter</a>.</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://w3api.com/wiki/Java:BufferedWriter"><span style="color: #aaaadd; font-weight: bold;">BufferedWriter</span></a> bw = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://w3api.com/wiki/Java:BufferedWriter"><span style="color: #aaaadd; font-weight: bold;">BufferedWriter</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://w3api.com/wiki/Java:FileWriter"><span style="color: #aaaadd; font-weight: bold;">FileWriter</span></a><span style="color: #66cc66;">&#40;</span>sFichero<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></li></ol></pre>
<p>Una vez que tenemos el stream sobre el <a href="http://w3api.com/wiki/Java:BufferedWriter" title="BufferedWriter">BufferedWriter</a> lo que vamos a hacer es escribir una línea vacía. Para ello utilizamos el método .<a href="http://w3api.com/wiki/Java:BufferedWriter.write()">write()</a>.</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">bw.<span style="color: #006600;">write</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>;</div></li></ol></pre>
<p>Así lo que conseguimos es machacar todo el contenido que pudiese existir en el fichero. Vaciándolo de su contenido actual. Ahora, no hay que olvidarnos de cerrar nuestro fichero. Esto lo hacemos mediante el método <a href="http://w3api.com/wiki/Java:BufferedWriter.close()">.close()</a> del <a href="http://w3api.com/wiki/Java:BufferedWriter" title="BufferedWriter">BufferedWriter</a>.</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">bw.<span style="color: #006600;">close</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li></ol></pre>
<p>Y ya hemos conseguido vaciar el contenido de un fichero con <a href="http://www.manualweb.net/tutorial-java/" title="Java">Java</a>.</p>
<p>Ojo, hay que tener cuidado de que el fichero exista o se nos generará una excepción <a href="http://w3api.com/wiki/Java:FileNotFoundException" title="FileNotFoundException">FileNotFoundException</a> o <a href="http://w3api.com/wiki/Java:IOException" title="IOException">IOException</a>.<strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://lineadecodigo.com/java/escribir-en-un-fichero-de-texto-con-java/" rel="bookmark" title="Febrero 6, 2008">Escribir en un fichero de texto con Java</a></li>
<li><a href="http://lineadecodigo.com/java/vaciar-el-contenido-de-un-fichero-mediante-borrado-y-creacion/" rel="bookmark" title="Agosto 1, 2010">Vaciar el contenido de un fichero mediante borrado y creación</a></li>
<li><a href="http://lineadecodigo.com/java/generar-un-fichero-gzip-con-java/" rel="bookmark" title="Diciembre 31, 2006">Generar un fichero GZIP con Java</a></li>
<li><a href="http://lineadecodigo.com/java/numero-de-lineas-de-un-fichero/" rel="bookmark" title="Noviembre 20, 2006">Número de líneas de un fichero</a></li>
<li><a href="http://lineadecodigo.com/java/comparar-el-contenido-de-dos-ficheros-con-java/" rel="bookmark" title="Julio 30, 2010">Comparar el contenido de dos ficheros con Java</a></li>
</ul>
<p><!-- Similar Posts took 2.991 ms --></p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2.1] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Flineadecodigo.com%2Fjava%2Fvaciar-el-contenido-de-un-fichero-con-java%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:hidden; width:450px; height:80px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2.1] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://lineadecodigo.com/java/vaciar-el-contenido-de-un-fichero-con-java/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Comparar el contenido de dos ficheros con Java</title>
		<link>http://lineadecodigo.com/java/comparar-el-contenido-de-dos-ficheros-con-java/</link>
		<comments>http://lineadecodigo.com/java/comparar-el-contenido-de-dos-ficheros-con-java/#comments</comments>
		<pubDate>Fri, 30 Jul 2010 21:50:14 +0000</pubDate>
		<dc:creator>lineadecodigo</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[BufferedReader]]></category>
		<category><![CDATA[FileNotFoundException]]></category>
		<category><![CDATA[FileReader]]></category>
		<category><![CDATA[IOException]]></category>
		<category><![CDATA[null]]></category>
		<category><![CDATA[readLine]]></category>
		<category><![CDATA[try-catch]]></category>

		<guid isPermaLink="false">http://lineadecodigo.com/?p=2533</guid>
		<description><![CDATA[En este ejemplo vamos a ver como podemos comparar el contenido de dos ficheros de texto con Java y comprobar si el contenido es el mismo o es diferente.
Lo primero será crear una clase del tipo FileReader. Esta clase nos va a ayudar a acceder al contenido de los ficheros, conjuntamente con un BufferedReader. De [...]]]></description>
			<content:encoded><![CDATA[<p>En este ejemplo vamos a ver como podemos comparar el contenido de dos ficheros de texto con <a href="http://www.manualweb.net/tutorial-java/" title="Java">Java</a> y comprobar si el contenido es el mismo o es diferente.</p>
<p>Lo primero será crear una clase del tipo <a href="http://w3api.com/wiki/Java:FileReader" title="FileReader">FileReader</a>. Esta clase nos va a ayudar a acceder al contenido de los ficheros, conjuntamente con un <a href="http://w3api.com/wiki/Java:BufferedReader" title="BufferedReader">BufferedReader</a>. De esta forma crear un stream de un fichero a un <a href="http://w3api.com/wiki/Java:BufferedReader" title="BufferedReader">BufferedReader</a>r lo podemos hacer con el siguiente código:</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://w3api.com/wiki/Java:FileReader"><span style="color: #aaaadd; font-weight: bold;">FileReader</span></a> fr1 = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://w3api.com/wiki/Java:FileReader"><span style="color: #aaaadd; font-weight: bold;">FileReader</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;fichero1.txt&quot;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://w3api.com/wiki/Java:BufferedReader"><span style="color: #aaaadd; font-weight: bold;">BufferedReader</span></a> bf1 = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://w3api.com/wiki/Java:BufferedReader"><span style="color: #aaaadd; font-weight: bold;">BufferedReader</span></a><span style="color: #66cc66;">&#40;</span>fr1<span style="color: #66cc66;">&#41;</span>;</div></li></ol></pre>
<p>Como tenemos dos ficheros, repetimos la misma sentencia para el segundo fichero:</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://w3api.com/wiki/Java:FileReader"><span style="color: #aaaadd; font-weight: bold;">FileReader</span></a> fr1 = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://w3api.com/wiki/Java:FileReader"><span style="color: #aaaadd; font-weight: bold;">FileReader</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;fichero1.txt&quot;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://w3api.com/wiki/Java:FileReader"><span style="color: #aaaadd; font-weight: bold;">FileReader</span></a> fr2 = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://w3api.com/wiki/Java:FileReader"><span style="color: #aaaadd; font-weight: bold;">FileReader</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;fichero2.txt&quot;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://w3api.com/wiki/Java:BufferedReader"><span style="color: #aaaadd; font-weight: bold;">BufferedReader</span></a> bf1 = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://w3api.com/wiki/Java:BufferedReader"><span style="color: #aaaadd; font-weight: bold;">BufferedReader</span></a><span style="color: #66cc66;">&#40;</span>fr1<span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://w3api.com/wiki/Java:BufferedReader"><span style="color: #aaaadd; font-weight: bold;">BufferedReader</span></a> bf2 = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://w3api.com/wiki/Java:BufferedReader"><span style="color: #aaaadd; font-weight: bold;">BufferedReader</span></a><span style="color: #66cc66;">&#40;</span>fr2<span style="color: #66cc66;">&#41;</span>;</div></li></ol></pre>
<p>Ahora que tenemos un <a href="http://w3api.com/wiki/Java:BufferedReader" title="BufferedReader">BufferedReader</a> será sencillo ir leyendo el contenido del fichero, ya que solo tendremos que ir lanzando el método <a href="http://w3api.com/wiki/Java:BufferedReader.readLine()" title="readline">readLine()</a> hasta que lleguemos al final de los ficheros.</p>
<p>Así recorreremos los ficheros mientras lo leído no sea null, ya que significará que hemos llegado al final del fichero, o bien que asumamos que los ficheros siguen siendo iguales, lo cual controlaremos mediante una variable de bandera que denominaremos iguales.</p>
<p>De esta manera el bucle para recorrer los ficheros será de la siguiente forma:</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">sCadena1 = bf1.<span style="color: #006600;">readLine</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">sCadena2 = bf2.<span style="color: #006600;">readLine</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>sCadena1!=<span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> &amp;&amp; <span style="color: #66cc66;">&#40;</span>sCadena2!=<span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> &amp;&amp; iguales<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>...<span style="color: #66cc66;">&#125;</span> </div></li></ol></pre>
<p>Dentro del bucle compararemos las líneas almacenadas en las cadenas. Si las líneas son iguales asumiremos que los ficheros siguen siendo iguales, leeremos e iteraremos otra vez.</p>
<p>Si las líneas no son iguales significa que el contenido es diferente. En ese caso cambiaremos el valor de la variable iguales a "false" para provocar una salida del bucle.</p>
<p>El código quedará de la siguiente manera:</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">sCadena1 = bf1.<span style="color: #006600;">readLine</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">sCadena2 = bf2.<span style="color: #006600;">readLine</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>sCadena1!=<span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> &amp;&amp; <span style="color: #66cc66;">&#40;</span>sCadena2!=<span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> &amp;&amp; iguales<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!sCadena1.<span style="color: #006600;">equals</span><span style="color: #66cc66;">&#40;</span>sCadena2<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    iguales = <span style="color: #000000; font-weight: bold;">false</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  sCadena1 = bf1.<span style="color: #006600;">readLine</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  sCadena2 = bf2.<span style="color: #006600;">readLine</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div></li></ol></pre>
<p>Una vez hemos salido del bucle sabremos que los ficheros son iguales si la variable iguales sigue siendo true y si hemos llegado al final de la lectura de los dos ficheros, es decir, que las variables de lectura contengan un valor null. Así tendremos el siguiente código:</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>iguales<span style="color: #66cc66;">&#41;</span> &amp;&amp; <span style="color: #66cc66;">&#40;</span>sCadena1==<span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> &amp;&amp; <span style="color: #66cc66;">&#40;</span>sCadena2==<span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  <a href="http://w3api.com/wiki/Java:System"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Los ficheros son iguales&quot;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #b1b100;">else</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://w3api.com/wiki/Java:System"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Los ficheros son diferentes&quot;</span><span style="color: #66cc66;">&#41;</span>;</div></li></ol></pre>
<p>En todo este código tenemos que tener cuidado ya que podemos tener excepciones <a href="http://w3api.com/wiki/Java:FileNotFoundException" title="FileNotFoundException">FileNotFoundException</a>, si los fichero a leer no existen o <a href="http://w3api.com/wiki/Java:IOException" title="IOException">IOException</a> si tenemos problemas en la lectura de los mismos. Es por ello que todo el código irá dentro de su estructura try-catch.<strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://lineadecodigo.com/java/leer-fichero-de-texto-con-java/" rel="bookmark" title="Diciembre 26, 2006">Leer fichero de texto con Java</a></li>
<li><a href="http://lineadecodigo.com/java/numero-de-lineas-de-un-fichero/" rel="bookmark" title="Noviembre 20, 2006">Número de líneas de un fichero</a></li>
<li><a href="http://lineadecodigo.com/java/multiplicar-dos-numeros-con-java/" rel="bookmark" title="Diciembre 30, 2008">Multiplicar dos números con Java</a></li>
<li><a href="http://lineadecodigo.com/java/lectura-de-datos-por-consola-en-java/" rel="bookmark" title="Diciembre 16, 2006">Lectura de datos por consola en Java</a></li>
<li><a href="http://lineadecodigo.com/java/listar-un-directorio-con-java-io/" rel="bookmark" title="Julio 11, 2007">Listar un directorio con Java IO</a></li>
</ul>
<p><!-- Similar Posts took 3.035 ms --></p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2.1] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Flineadecodigo.com%2Fjava%2Fcomparar-el-contenido-de-dos-ficheros-con-java%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:hidden; width:450px; height:80px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2.1] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://lineadecodigo.com/java/comparar-el-contenido-de-dos-ficheros-con-java/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Sumar matrices en Java</title>
		<link>http://lineadecodigo.com/java/sumar-matrices-en-java/</link>
		<comments>http://lineadecodigo.com/java/sumar-matrices-en-java/#comments</comments>
		<pubDate>Wed, 28 Jul 2010 17:48:10 +0000</pubDate>
		<dc:creator>lineadecodigo</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Exception]]></category>
		<category><![CDATA[for]]></category>
		<category><![CDATA[length]]></category>
		<category><![CDATA[matrices]]></category>
		<category><![CDATA[sumar]]></category>
		<category><![CDATA[try-catch]]></category>

		<guid isPermaLink="false">http://lineadecodigo.com/?p=2527</guid>
		<description><![CDATA[Seguimos con los ejemplos de matrices, ahora que ya hemos visto como crear una matriz con Java. En este caso vamos a realizar la suma de dos matrices.
Para llevar a cabo el ejemplo hemos definido un método Java que nos permite sumar las matrices. La signatura de dicho método será la siguiente:
public static int&#91;&#93;&#91;&#93; sumarmatrices [...]]]></description>
			<content:encoded><![CDATA[<p>Seguimos con los ejemplos de matrices, ahora que ya hemos visto <a href="http://lineadecodigo.com/java/crear-una-matriz-en-java/" title="crear una matriz en java">como crear una matriz con Java</a>. En este caso vamos a realizar la suma de dos matrices.</p>
<p>Para llevar a cabo el ejemplo hemos definido un método <a href="http://www.manualweb.net/tutorial-java/" title="Java">Java</a> que nos permite sumar las matrices. La signatura de dicho método será la siguiente:</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #993333;">int</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> sumarmatrices <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> m1,<span style="color: #993333;">int</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> m2<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://w3api.com/wiki/Java:Exception"><span style="color: #aaaadd; font-weight: bold;">Exception</span></a> <span style="color: #66cc66;">&#123;</span>...<span style="color: #66cc66;">&#125;</span></div></li></ol></pre>
<p>Como podemos ver el método <a href="http://www.manualweb.net/tutorial-java/" title="Java">Java</a> recibe dos matrices y devuelve una, que será el resultado de la misma. Por otro lado puede generar una <a href="http://w3api.com/wiki/Java:Exception" title="Exception">Exception</a>. Este caso será cuando las dimensiones de las matrices sean diferentes. Ya que cuando las dimensiones de las matrices son diferentes, estas no se pueden sumar.</p>
<p>Así la comprobación de las dimensiones de las matrices las haremos apoyándonos en la propiedad length:</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>m1.<span style="color: #006600;">length</span> == m2.<span style="color: #006600;">length</span><span style="color: #66cc66;">&#41;</span> &amp;&amp; <span style="color: #66cc66;">&#40;</span>m1<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">length</span>==m2<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">length</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>...<span style="color: #66cc66;">&#125;</span></div></li></ol></pre>
<p>En el caso de no coincidir los tamaños, es cuando elevamos la <a href="http://w3api.com/wiki/Java:Exception" title="Exception">Exception</a>.</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>m1.<span style="color: #006600;">length</span> == m2.<span style="color: #006600;">length</span><span style="color: #66cc66;">&#41;</span> &amp;&amp; <span style="color: #66cc66;">&#40;</span>m1<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">length</span>==m2<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">length</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  ...</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://w3api.com/wiki/Java:Exception"><span style="color: #aaaadd; font-weight: bold;">Exception</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #66cc66;">&#125;</span></div></li></ol></pre>
<p>En caso afirmativo procederemos a sumar los elementos de las matrices. Hay que recordar que la suma de las matrices es la suma de todos sus elementos posición a posición. Por lo tanto la suma es sencilla. Solo tenemos que recorrer todos los elementos y dejar el resultado en la matriz destino.</p>
<p>Así, lo primero es crear una matriz resultado de las mismas dimensiones:</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">int</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> suma = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #993333;">int</span><span style="color: #66cc66;">&#91;</span>m1.<span style="color: #006600;">length</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>m1<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">length</span><span style="color: #66cc66;">&#93;</span>;</div></li></ol></pre>
<p>Para recorrer los elementos nos apoyamos en un bucle for y, nuevamente, en la propiedad length.</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> x=<span style="color: #cc66cc;">0</span>; x &lt; m1.<span style="color: #006600;">length</span>; x++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> y=<span style="color: #cc66cc;">0</span>; y &lt; m1<span style="color: #66cc66;">&#91;</span>x<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">length</span>; y++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    suma<span style="color: #66cc66;">&#91;</span>x<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>y<span style="color: #66cc66;">&#93;</span>=m1<span style="color: #66cc66;">&#91;</span>x<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>y<span style="color: #66cc66;">&#93;</span>+m2<span style="color: #66cc66;">&#91;</span>x<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>y<span style="color: #66cc66;">&#93;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  <span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>
<p>Ahora solo nos quedará devolver la matriz con el resultado de la suma.</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> <span style="color: #000000; font-weight: bold;">return</span> suma;</div></li></ol></pre>
<p>Invocar el método es sencillo. Solo hay que tener en cuenta que puede soltar una <a href="http://w3api.com/wiki/Java:Exception" title="Exception">Exception</a> y por lo tanto lo deberemos de invocar desde un método try-catch</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">int</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> matriz1 = <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#123;</span><span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#125;</span>,<span style="color: #66cc66;">&#123;</span><span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">9</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#125;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">int</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> matriz2 = <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#123;</span><span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#125;</span>,<span style="color: #66cc66;">&#123;</span><span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">9</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#125;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">int</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> matriz = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #993333;">int</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  matriz = sumarmatrices<span style="color: #66cc66;">&#40;</span>matriz1,matriz2<span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  pintarmatriz<span style="color: #66cc66;">&#40;</span>matriz<span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span><span style="color: #66cc66;">&#40;</span><a href="http://w3api.com/wiki/Java:Exception"><span style="color: #aaaadd; font-weight: bold;">Exception</span></a> e<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  <a href="http://w3api.com/wiki/Java:System"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Matrices de diferente dimensión&quot;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>
<p><strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://lineadecodigo.com/java/crear-una-matriz-en-java/" rel="bookmark" title="Julio 9, 2010">Crear una matriz en Java</a></li>
<li><a href="http://lineadecodigo.com/java/sumar-dos-numeros-con-java/" rel="bookmark" title="Enero 22, 2009">Sumar dos números con Java</a></li>
<li><a href="http://lineadecodigo.com/java/calcular-media-de-numeros-con-java/" rel="bookmark" title="Abril 23, 2007">Calcular media de numeros con Java</a></li>
<li><a href="http://lineadecodigo.com/vbscript/recorrer-una-matriz-en-vbscript/" rel="bookmark" title="Marzo 30, 2008">Recorrer una matriz en VBScript</a></li>
<li><a href="http://lineadecodigo.com/java/listar-elementos-de-un-array/" rel="bookmark" title="Junio 17, 2007">Listar elementos de un array</a></li>
</ul>
<p><!-- Similar Posts took 2.961 ms --></p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2.1] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Flineadecodigo.com%2Fjava%2Fsumar-matrices-en-java%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:hidden; width:450px; height:80px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2.1] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://lineadecodigo.com/java/sumar-matrices-en-java/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Crear una matriz en Java</title>
		<link>http://lineadecodigo.com/java/crear-una-matriz-en-java/</link>
		<comments>http://lineadecodigo.com/java/crear-una-matriz-en-java/#comments</comments>
		<pubDate>Fri, 09 Jul 2010 06:00:11 +0000</pubDate>
		<dc:creator>lineadecodigo</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[array bidimensional]]></category>
		<category><![CDATA[length]]></category>
		<category><![CDATA[matriz]]></category>

		<guid isPermaLink="false">http://lineadecodigo.com/?p=2522</guid>
		<description><![CDATA[Una matriz matemática es una tabla bidimensional de números. Las matrices pueden sumarse, multiplicarse,... Y suelen ser utilizadas para describir sistemas de ecuaciones lineales. Leer más sobre matrices en Wikipedia.
Si queremos representar una matriz en Java hay que crear un array bidimensional. Por ejemplo para declarar una matriz de 3x3 haríamos lo siguiente:
int matriz&#91;&#93;&#91;&#93; = new [...]]]></description>
			<content:encoded><![CDATA[<p>Una matriz matemática es una tabla bidimensional de números. Las matrices pueden sumarse, multiplicarse,... Y suelen ser utilizadas para describir sistemas de ecuaciones lineales.<a title="Matrices" href="http://es.wikipedia.org/wiki/Matriz_(matem%C3%A1tica)" title="matrices"> Leer más sobre matrices en Wikipedia</a>.</p>
<p>Si queremos representar una matriz en Java hay que crear un <strong>array bidimensional</strong>. Por ejemplo para declarar una matriz de 3x3 haríamos lo siguiente:</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">int</span> matriz<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #993333;">int</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span>;</div></li></ol></pre>
<p>Ahora procedemos a cargar la matriz con valores:</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">matriz<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #cc66cc;">2</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">matriz<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #cc66cc;">4</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">matriz<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #cc66cc;">4</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">matriz<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #cc66cc;">6</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">matriz<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #cc66cc;">6</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">matriz<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #cc66cc;">9</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">matriz<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #cc66cc;">8</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">matriz<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #cc66cc;">10</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">matriz<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #cc66cc;">12</span>;</div></li></ol></pre>
<blockquote><p>Hay que recordar que los elementos empiezan a numerarse por 0. Así la esquina superior izquierda de la matriz será el elemento [0][0] y la esquina inferior derecha será el [2][2].</p></blockquote>
<p>Podemos crear e instanciar la matriz en una única línea:</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">int</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> matriz = <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#123;</span><span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#125;</span>,<span style="color: #66cc66;">&#123;</span><span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">9</span><span style="color: #66cc66;">&#125;</span>,<span style="color: #66cc66;">&#123;</span><span style="color: #cc66cc;">8</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">12</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#125;</span>;</div></li></ol></pre>
<p>De igual manera, si nos apoyamos en el método .lenght del array podremos listar el contenido de la matriz</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> x=<span style="color: #cc66cc;">0</span>; x &lt; matriz.<span style="color: #006600;">length</span>; x++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> y=<span style="color: #cc66cc;">0</span>; y &lt; matriz<span style="color: #66cc66;">&#91;</span>x<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">length</span>; y++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    <a href="http://w3api.com/wiki/Java:System"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span> <span style="color: #66cc66;">&#40;</span>matriz<span style="color: #66cc66;">&#91;</span>x<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>y<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  <span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>
<p><strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://lineadecodigo.com/java/sumar-matrices-en-java/" rel="bookmark" title="Julio 28, 2010">Sumar matrices en Java</a></li>
<li><a href="http://lineadecodigo.com/java/listar-elementos-de-un-array/" rel="bookmark" title="Junio 17, 2007">Listar elementos de un array</a></li>
<li><a href="http://lineadecodigo.com/java/copiar-dos-arrays-en-uno-con-java/" rel="bookmark" title="Octubre 6, 2007">Copiar dos arrays en uno con Java</a></li>
<li><a href="http://lineadecodigo.com/vbscript/recorrer-una-matriz-en-vbscript/" rel="bookmark" title="Marzo 30, 2008">Recorrer una matriz en VBScript</a></li>
<li><a href="http://lineadecodigo.com/java/listar-un-directorio-de-forma-recursiva-en-java/" rel="bookmark" title="Diciembre 27, 2008">Listar un directorio de forma recursiva en Java</a></li>
</ul>
<p><!-- Similar Posts took 3.032 ms --></p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2.1] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Flineadecodigo.com%2Fjava%2Fcrear-una-matriz-en-java%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:hidden; width:450px; height:80px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2.1] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://lineadecodigo.com/java/crear-una-matriz-en-java/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Dividir dos números con Java</title>
		<link>http://lineadecodigo.com/java/dividir-dos-numeros-con-java/</link>
		<comments>http://lineadecodigo.com/java/dividir-dos-numeros-con-java/#comments</comments>
		<pubDate>Thu, 10 Jun 2010 18:00:19 +0000</pubDate>
		<dc:creator>lineadecodigo</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[dividir]]></category>
		<category><![CDATA[división]]></category>
		<category><![CDATA[int]]></category>
		<category><![CDATA[long]]></category>
		<category><![CDATA[nextFloat]]></category>
		<category><![CDATA[numeros]]></category>
		<category><![CDATA[Scanner]]></category>

		<guid isPermaLink="false">http://lineadecodigo.com/?p=2509</guid>
		<description><![CDATA[Dentro del grupo de ejemplos básicos y siendo otro que nos le pide mucha gente que empieza con la programación en Java, hoy vamos a explicar como hacer un programa que nos ayude a dividir dos números. (Discúlpenme los expertos).
Lo primero será definir las variables. En el artículo sobre Sumar dos números con Java veíamos [...]]]></description>
			<content:encoded><![CDATA[<p>Dentro del grupo de ejemplos básicos y siendo otro que nos le pide mucha gente que empieza con la programación en <a href="http://www.manualweb.net/tutorial-java/" title="java">Java</a>, hoy vamos a explicar como hacer un programa que nos ayude a dividir dos números. (Discúlpenme los expertos).</p>
<p>Lo primero será definir las variables. En el artículo sobre <a href="http://lineadecodigo.com/java/sumar-dos-numeros-con-java/" title="Sumar dos números con Java">Sumar dos números con Java</a> veíamos que las variables eran de tipo int. Pero en el caso de la división hay que tener cuidado, ya que el resultado de una división puede dar lugar a un número con decimales. Por ejemplo:</p>
<pre>9/2 = 4.5</pre>
<p>Es por ello que vamos a utilizar tipos float para los números:</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">float</span> numero1 = <span style="color: #cc66cc;">0</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">float</span> numero2 = <span style="color: #cc66cc;">0</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">float</span> resultado;</div></li></ol></pre>
<p>Una vez definidas las variables pasaremos a solicitar los números al usuario apoyándonos en un objeto <a href="http://w3api.com/wiki/Java:Scanner" title="Scanner">Scanner</a>. Instanciamos el <a href="http://w3api.com/wiki/Java:Scanner" title="Scanner">Scanner</a>, con la entrada del sistema (<a href="http://w3api.com/wiki/Java:System.in" title="System.in">System.in</a>) como parámetro:</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Scanner reader = <span style="color: #000000; font-weight: bold;">new</span> Scanner<span style="color: #66cc66;">&#40;</span><a href="http://w3api.com/wiki/Java:System"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">in</span><span style="color: #66cc66;">&#41;</span>;</div></li></ol></pre>
<p>Ahora pasamos a leer los números mediante el método <a href="http://w3api.com/wiki/Java:Scanner.nextFloat()" title="nextFloat">.nextFloat</a>. El cual leerá de la consola el dato y lo almacenará en nuestras variables:</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://w3api.com/wiki/Java:System"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Introduce el primer número:&quot;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">numero1 = reader.<span style="color: #006600;">nextFloat</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://w3api.com/wiki/Java:System"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Introduce el segundo número:&quot;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">numero2 = reader.<span style="color: #006600;">nextFloat</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li></ol></pre>
<p>Ya solo nos quedará ejecutar la división y mostrarla en la pantalla.</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">esultado = numero1/numero2;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://w3api.com/wiki/Java:System"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;La división es &quot;</span> + numero1 + <span style="color: #ff0000;">&quot; / &quot;</span> + numero2 + <span style="color: #ff0000;">&quot; = &quot;</span> + resultado<span style="color: #66cc66;">&#41;</span>;</div></li></ol></pre>
<p>Si están aprendiendo <a href="http://www.manualweb.net/tutorial-java/" title="java">Java</a>, es un buen ejemplo para empezar. Codificarlo, compilarlo y probarlo. A ver que tal.<strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://lineadecodigo.com/java/sumar-dos-numeros-con-java/" rel="bookmark" title="Enero 22, 2009">Sumar dos números con Java</a></li>
<li><a href="http://lineadecodigo.com/java/restar-dos-numeros-con-java/" rel="bookmark" title="Septiembre 4, 2010">Restar dos números con Java</a></li>
<li><a href="http://lineadecodigo.com/java/numeros-divisibles-con-java/" rel="bookmark" title="Julio 28, 2009">Números divisibles con Java</a></li>
<li><a href="http://lineadecodigo.com/java/multiplicar-dos-numeros-con-java/" rel="bookmark" title="Diciembre 30, 2008">Multiplicar dos números con Java</a></li>
<li><a href="http://lineadecodigo.com/java/ultimo-digito-de-un-numero-con-java/" rel="bookmark" title="Enero 11, 2009">Último dígito de un número con Java</a></li>
</ul>
<p><!-- Similar Posts took 2.974 ms --></p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2.1] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Flineadecodigo.com%2Fjava%2Fdividir-dos-numeros-con-java%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:hidden; width:450px; height:80px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2.1] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://lineadecodigo.com/java/dividir-dos-numeros-con-java/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Convertir un array de caracteres en un String</title>
		<link>http://lineadecodigo.com/java/convertir-un-array-de-caracteres-en-un-string/</link>
		<comments>http://lineadecodigo.com/java/convertir-un-array-de-caracteres-en-un-string/#comments</comments>
		<pubDate>Sun, 06 Jun 2010 06:00:20 +0000</pubDate>
		<dc:creator>lineadecodigo</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[caracter]]></category>
		<category><![CDATA[char]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[valueOf]]></category>

		<guid isPermaLink="false">http://lineadecodigo.com/?p=2494</guid>
		<description><![CDATA[En el ejemplo Convertir un array en un String con Java veíamos el procedimiento a seguir para coger un array de String y convertirlo en una cadena. Pero, si el array que estamos manejando es de caracteres en vez de String, el procedimiento será mucho más sencillo.
El array de caracteres:
char miarray&#91;&#93; = &#123;'A','V','I','L','A'&#125;;
Ahora utilizamos el [...]]]></description>
			<content:encoded><![CDATA[<p>En el ejemplo <a href="http://lineadecodigo.com/java/convertir-un-array-en-un-string-con-java/" title="Convertir un array en un String con Java">Convertir un array en un String con Java</a> veíamos el procedimiento a seguir para coger un array de <a href="http://w3api.com/wiki/Java:String" title="String">String</a> y convertirlo en una cadena. Pero, si el array que estamos manejando es de caracteres en vez de <a href="http://w3api.com/wiki/Java:String" title="String">String</a>, el procedimiento será mucho más sencillo.</p>
<p>El array de caracteres:</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">char</span> miarray<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #66cc66;">&#123;</span><span style="color: #ff0000;">'A'</span>,<span style="color: #ff0000;">'V'</span>,<span style="color: #ff0000;">'I'</span>,<span style="color: #ff0000;">'L'</span>,<span style="color: #ff0000;">'A'</span><span style="color: #66cc66;">&#125;</span>;</div></li></ol></pre>
<p>Ahora utilizamos el método <a href="http://w3api.com/wiki/Java:String.valueOf()" title="valueOf">.valueOf()</a> de la clase <a href="http://w3api.com/wiki/Java:String" title="String">String</a> pasando el array de caracteres como parámetro:</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://w3api.com/wiki/Java:System"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><a href="http://w3api.com/wiki/Java:String"><span style="color: #aaaadd; font-weight: bold;">String</span></a>.<span style="color: #006600;">valueOf</span><span style="color: #66cc66;">&#40;</span>miarray<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></li></ol></pre>
<p>Y ya tenemos convertir el array de caracteres en un <a href="http://w3api.com/wiki/Java:String" title="String">String</a><strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://lineadecodigo.com/java/convertir-una-cadena-en-un-array-de-caracteres-con-java/" rel="bookmark" title="Febrero 14, 2009">Convertir una cadena en un array de caracteres con Java</a></li>
<li><a href="http://lineadecodigo.com/java/listar-elementos-de-un-array/" rel="bookmark" title="Junio 17, 2007">Listar elementos de un array</a></li>
<li><a href="http://lineadecodigo.com/java/convertir-un-array-en-un-string-con-java/" rel="bookmark" title="Marzo 24, 2010">Convertir un array en un String con Java</a></li>
<li><a href="http://lineadecodigo.com/java/recibir-un-parametro-numerico-en-una-jsp/" rel="bookmark" title="Julio 30, 2009">Recibir un parámetro numérico en una JSP</a></li>
<li><a href="http://lineadecodigo.com/java/usando-la-api-de-twitter4j-en-java/" rel="bookmark" title="Abril 27, 2010">Usando la API de Twitter4j en java</a></li>
</ul>
<p><!-- Similar Posts took 2.799 ms --></p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2.1] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Flineadecodigo.com%2Fjava%2Fconvertir-un-array-de-caracteres-en-un-string%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:hidden; width:450px; height:80px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2.1] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://lineadecodigo.com/java/convertir-un-array-de-caracteres-en-un-string/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Cerrar una Ventana con Java Swing</title>
		<link>http://lineadecodigo.com/java/cerrar-una-ventana-con-java-swing/</link>
		<comments>http://lineadecodigo.com/java/cerrar-una-ventana-con-java-swing/#comments</comments>
		<pubDate>Fri, 04 Jun 2010 13:54:51 +0000</pubDate>
		<dc:creator>lineadecodigo</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[addWindowListener]]></category>
		<category><![CDATA[cerrar]]></category>
		<category><![CDATA[java swing]]></category>
		<category><![CDATA[System.exit]]></category>
		<category><![CDATA[ventana]]></category>
		<category><![CDATA[WindowAdapter]]></category>
		<category><![CDATA[windowClosing]]></category>

		<guid isPermaLink="false">http://lineadecodigo.com/?p=2463</guid>
		<description><![CDATA[Una vez visto el ejemplo de Hola Mundo con Java Swing vamos a dar paso a realizar más cosas con Java Swing. En este caso, partiendo del ejemplo de Hola Mundo, vamos a ver como podemos cerrar una ventana con Java Swing. De esta manera controlaremos las acciones a realizar una vez que el usuario [...]]]></description>
			<content:encoded><![CDATA[<p>Una vez visto el ejemplo de <a href="http://lineadecodigo.com/java/hola-mundo-con-swing/" title="Hola Mundo con Java Swing">Hola Mundo con Java Swing</a> vamos a dar paso a realizar más cosas con <a href="http://w3api.com/wiki/Categor%C3%ADa:Java_Swing" title="Java Swing">Java Swing</a>. En este caso, partiendo del <a href="http://lineadecodigo.com/java/hola-mundo-con-swing/" title="ejemplo de Hola Mundo con Java Swing">ejemplo de Hola Mundo</a>, vamos a ver como podemos cerrar una ventana con <a href="http://w3api.com/wiki/Categor%C3%ADa:Java_Swing" title="Java Swing">Java Swing</a>. De esta manera controlaremos las acciones a realizar una vez que el usuario cierre la ventana.</p>
<p>La idea principal consiste en escuchar el evento <a href="http://w3api.com/wiki/Java:WindowAdapter.windowClosing()" title="WindowClosing">windowClosing</a>. El evento <a href="http://w3api.com/wiki/Java:WindowAdapter.windowClosing()" title="WindowClosing">windowClosing</a> es un evento del adaptador de ventana o <a href="http://w3api.com/wiki/Java:WindowAdapter" title="WindowAdapter">WindowAdapter</a>.</p>
<blockquote><p>Recuerda que para la gestión de eventos tenemos Interfaces y Adaptadores. Los interfaces nos obligan a codificar todos los eventos a gestionar, mientras que en el Adaptador solo tenemos que gestionar el evento que necesitemos.</p></blockquote>
<p>Pero lo primero es utilizar el método .addWindowListerner para suscribirnos a los eventos que se produzcan en la ventana. Así, en el constructor de nuestro programa utilizaremos dicho método:</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> CerrarVentana<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  addWindowListener<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://w3api.com/wiki/Java:WindowAdapter"><span style="color: #aaaadd; font-weight: bold;">WindowAdapter</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>...<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>
<p>Como podemos comprobar en el código, al método .addWindowListener le estamos pasando una clase <a href="http://w3api.com/wiki/Java:WindowAdapter" title="WindowAdapter">WindowAdapter</a>.</p>
<p>El método asociado al cierre de la ventana es <a href="http://w3api.com/wiki/Java:WindowAdapter.windowClosing()" title="WindowClosing">windowClosing</a>. En él solo vamos a realizar un exit del sistema con <a href="http://w3api.com/wiki/Java:System.exit()" title="System.exit()">System.exit</a>.</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> windowClosing<span style="color: #66cc66;">&#40;</span><a href="http://w3api.com/wiki/Java:WindowEvent"><span style="color: #aaaadd; font-weight: bold;">WindowEvent</span></a> e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">  <a href="http://w3api.com/wiki/Java:System"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">exit</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>
<p>Así nuestro <a href="http://w3api.com/wiki/Java:WindowAdapter" title="WindowAdapter">WindowAdapter</a> quedaría de la siguiente forma para gestionar el cierre de una ventana con <a href="http://w3api.com/wiki/Categor%C3%ADa:Java_Swing" title="Java Swing">Java Swing</a>.</p>
<pre class="java"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> CerrarVentana<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> addWindowListener<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://w3api.com/wiki/Java:WindowAdapter"><span style="color: #aaaadd; font-weight: bold;">WindowAdapter</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> windowClosing<span style="color: #66cc66;">&#40;</span><a href="http://w3api.com/wiki/Java:WindowEvent"><span style="color: #aaaadd; font-weight: bold;">WindowEvent</span></a> e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">     <a href="http://w3api.com/wiki/Java:System"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">exit</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">   <span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>
<p>Aunque nosotros solo hemos forzado un <a href="http://w3api.com/wiki/Java:System.exit()" title="System.exit()">System.exit</a>, en el método <a href="http://w3api.com/wiki/Java:WindowAdapter.windowClosing()" title="WindowClosing">windowClosing</a> podemos hacer lo que queramos. Por ejemplo, preguntar al usuario si está seguro de cerrar.<strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://lineadecodigo.com/java/cerrar-un-frame-en-awt/" rel="bookmark" title="Diciembre 16, 2007">Cerrar un frame en AWT</a></li>
<li><a href="http://lineadecodigo.com/java/acceder-al-xml-con-sax/" rel="bookmark" title="Abril 17, 2010">Acceder al XML con SAX</a></li>
<li><a href="http://lineadecodigo.com/java/scrollbars-y-elipse/" rel="bookmark" title="Enero 23, 2008">Scrollbars y Elipse</a></li>
<li><a href="http://lineadecodigo.com/java/hola-mundo-con-swing/" rel="bookmark" title="Junio 1, 2010">Hola Mundo con Swing</a></li>
<li><a href="http://lineadecodigo.com/java/formulario-basico-con-java-swing/" rel="bookmark" title="Junio 3, 2010">Formulario básico con Java Swing</a></li>
</ul>
<p><!-- Similar Posts took 2.786 ms --></p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2.1] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Flineadecodigo.com%2Fjava%2Fcerrar-una-ventana-con-java-swing%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:hidden; width:450px; height:80px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2.1] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://lineadecodigo.com/java/cerrar-una-ventana-con-java-swing/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
