Display of results in java

2

I made a program that allows me to enter any number with a limit that the user establishes, and so display the odd and even ones, but when I do, I have a comma on the right.

Even numbers are:

2, 4,

Odd numbers are:

3, 5,

How can I correct that and remove the extra comma at the end?

String pares = "";
String impares = "";
int li, num;
System.out.println("Ingrese la cantidad de nuemros a evaluar: ");
Scanner sc = new Scanner(System.in);
li = sc.nextInt();       

for (int i = 1; i <= li; i++) {
    System.out.println("Ingrese su numero #" + i);
    num = sc.nextInt();
    if (num % 2 == 0) {
        pares += num + ", ";
    } else {
        impares += num + ", ";
    }
}
System.out.println("Los numeros pares son:" );
    System.out.print(pares);

System.out.println("\nLos numeros inpares pares son: ");
System.out.println(impares);
    
asked by Axwell Duarte 18.10.2018 в 06:46
source

5 answers

2

A different approach. Instead of creating the list of "odd" and "odd" results on a string, save them instead in an array. This seems more natural, as well you have the numbers separately (in case you would like to then operate with them, for example add all the pairs) and not a representation of them all together in a chain.

Also, if pares or impares are Arrays, you can at the end simply print them with System.out.println(pares) to get a representation automatically created by Java that would look like this: [2, 4] .

If the brackets bother you, you can convert the array into a string and then remove them, keeping the substring from character 1 to the penultimate.

This solution would therefore be implemented as follows:

  ArrayList<Integer> pares = new ArrayList<Integer>();
  ArrayList<Integer> impares = new ArrayList<Integer>();

  int li, num;
  String repr;

  System.out.println("Ingrese la cantidad de nuemros a evaluar: ");
  Scanner sc = new Scanner(System.in);
  li = sc.nextInt();       

  for (int i = 1; i <= li; i++) {
      System.out.println("Ingrese su numero #" + i);
      num = sc.nextInt();
      if (num % 2 == 0) {
          pares.add(num);
      } else {
          impares.add(num);
      }
  }
  System.out.println("Los numeros pares son:" );
  repr = pares.toString();
  System.out.println(repr.substring(1, repr.length()-1));

  System.out.println("\nLos numeros inpares pares son: ");
  repr = impares.toString();
  System.out.println(repr.substring(1, repr.length()-1));
    
answered by 18.10.2018 в 09:56
2

Add the comma before of the number

 pares += ", " + num;

but not before the first issue

 if (pares.isEmpty()) {
    pares += num;
 } else {
    pares += ", " + num;
 }

Or, using the conditional operator to do it in a line

 pares += (pares.isEmpty() ? "" : ", ") + num;
    
answered by 18.10.2018 в 10:03
1

Another different approach, using StringJoiner from java 8 It gives you the advantage that you do not have to change almost anything in your code. Change the declaration of pares e impares to:

StringJoiner pares = new StringJoiner(",");       // "," es el delimitador que StringJoiner
StringJoiner impares =  new StringJoiner(",");    // necesita para juntar las String

And the addition to:

if (num % 2 == 0) {
    pares.add(String.valueOf(i));
} else {
    impares.add(String.valueOf(i));
}
    
answered by 18.10.2018 в 10:10
0

Your problem is simple, you just have to remove the comma after each of the chains.

When not knowing the numbers that they are going to introduce, we can not do it inside the loop where the numbers are being asked, we must do it after having collected all the values.

To do this, let's use the public String substring(int beginIndex, int endIndex)

method

This method returns a "trimmed" text string from the initial index to the final index.

Since we only want to remove the last character, we have to cut the chain from position 0 to the last -1 with which, we would have to do it like this:

pares = pares.substring(0, pares.length()-2);
impares = impares.substring(0, impares.length()-2);
    
answered by 18.10.2018 в 08:14
0
  

How can I correct that, and remove the extra comma from the end?

Another way of doing this would be to create a delimiter for each of the variables odd and even

    String delimiterPar = "";
    String delimiterImpar = "";

And when it meets the condition in each of the cases, add the delimiter to the beginning, which in the first place will be worth "" , and then we already match the value of the delimiter to ", "

if (num % 2 == 0) {
     pares += delimiterPar + num;
     delimiterPar =", ";
} else {
     impares += delimiterImpar + num;
     delimiterImpar = ", ";         
}

Source or information extracted from here: How to create a text string with separators efficiently?

    
answered by 18.10.2018 в 11:08