Hercles Posted March 16, 2015 at 03:06 PM Report Share #579538 Posted March 16, 2015 at 03:06 PM (edited) Caros, existe algum método que informa o tamanho da variável tipo: Scanner n1 = new Scanner(System.in); int b = n1.nextInt(); no Console eu digite 999 ai me informe 3 (dígitos). Edited March 16, 2015 at 04:58 PM by apocsantos Link to comment Share on other sites More sharing options...
GOZK Posted March 16, 2015 at 03:35 PM Report Share #579542 Posted March 16, 2015 at 03:35 PM (edited) Boa tarde, Penso que queiras o tamanho da tua variável, se for o caso podes usar: => Length Exprimenta: String texto = Integer.toString(b); System.out.println(texto.length()); Edited March 16, 2015 at 04:59 PM by apocsantos Link to comment Share on other sites More sharing options...
alexandre1985 Posted March 16, 2015 at 04:06 PM Report Share #579546 Posted March 16, 2015 at 04:06 PM tens que transformar o int b = n1.nextInt(); em String b = n1.nextLine(); 1º e depois usas int tamanho = b.length(); http://alexandre1985.github.io Link to comment Share on other sites More sharing options...
Hercles Posted March 16, 2015 at 04:13 PM Author Report Share #579548 Posted March 16, 2015 at 04:13 PM Isso, mas tem como transformar este resultado em int => System.out.println(texto.length()); Link to comment Share on other sites More sharing options...
GOZK Posted March 16, 2015 at 04:17 PM Report Share #579550 Posted March 16, 2015 at 04:17 PM Sim. Exprimenta: String texto = Integer.toString(b); String tamanho = texto.length(); Int vartamanho = Integer.parseInt(tamanho); Link to comment Share on other sites More sharing options...
Hercles Posted March 16, 2015 at 04:32 PM Author Report Share #579553 Posted March 16, 2015 at 04:32 PM (edited) na verdade o que eu quero e fazer, é o código abaixo, receber um numero tipo: 999 e saber se ele é Palíndromo (Palíndromo, quando visto de trás pra frente tem o mesmo valor => 123321 ... 121121.. 2222 public class Palindromo { public static void main(String[] args) { Scanner n1 = new Scanner(System.in); String b = n1.nextLine(); int n2 = n1.nextInt(); int n = b.length(); char vet[] = new char[n]; int i; for (i = 0; i < n; i++) { vet[i] = args[0].charAt(i); } if (testa(vet)) { System.err.println("É Palindromo"); } else { System.err.println("Não é Palodromio"); } } public static boolean testa(char[] vet) { int i, n = vet.length; for (i = 0; i < n / 2; i++) { if (vet[i] != vet[n - 1 - i]) { return false; } } return true; } } o método testa(char[] vet) funciona o que não estou conseguindo é fazer entrar os dados pelo método main (usando a classe scanner). Edited March 16, 2015 at 04:31 PM by Hercles Link to comment Share on other sites More sharing options...
alexandre1985 Posted March 17, 2015 at 08:10 PM Report Share #579655 Posted March 17, 2015 at 08:10 PM (edited) Faz assim public class Palindromo { public static void main(String[] args) { if (testa(arg[0])) { System.err.println("É Palindromo"); } else { System.err.println("Não é Palodromio"); } } public static boolean testa(String vet) { int n = vet.length(); for (int i = 0; i < n / 2; i++) { if (vet.charAt(i) != vet.charAt(n - 1 - i)) { return false; } } return true; } } E depois na linha de comandos fazes: java -jar prog.jar 9999 Edited March 18, 2015 at 05:04 PM by alexandre1985 http://alexandre1985.github.io Link to comment Share on other sites More sharing options...
Hercles Posted March 18, 2015 at 09:44 AM Author Report Share #579688 Posted March 18, 2015 at 09:44 AM Eu queria fazer com o Scanner. Link to comment Share on other sites More sharing options...
Solution alexandre1985 Posted March 18, 2015 at 04:25 PM Solution Report Share #579734 Posted March 18, 2015 at 04:25 PM (edited) entao nao precisas de usar o args[0] para nada. Com scanner faz assim: public class Palindromo { public static void main(String[] args) { Scanner n1 = new Scanner(System.in); String b = n1.nextLine(); if (testa(b)) { System.err.println("É Palindromo"); } else { System.err.println("Não é Palodromio"); } } public static boolean testa(String vet) { int i, n = vet.length(); for (i = 0; i < n / 2; i++) { if (vet.charAt(i) != vet.charAt(n - 1 - i)) { return false; } } return true; } } Edited March 18, 2015 at 06:21 PM by alexandre1985 1 Report http://alexandre1985.github.io Link to comment Share on other sites More sharing options...
Hercles Posted March 18, 2015 at 09:03 PM Author Report Share #579773 Posted March 18, 2015 at 09:03 PM isso! O nextLine() funciona também da palavras (String). nextint só funcionaria para int. Valeu. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now