So I have the following code and it all works perfectly besi
So I have the following code and it all works perfectly besides the fact that it prints out an extra space at the very end of the program. this space is invisiable but it is important for me to pass an online test. so how do I get rid of this space or change the code to fix it??
System.out.print(\"Enter numbers from 1-100: \");
Scanner input = new Scanner (System.in);
int[] x = new int[100];
int len = 0, i, r;
while (input.hasNextInt() == true )
{
int n = input.nextInt(); //Accept a number
//Validates the entered number must be greater than equal to 1 to 100
if(n >= 1 && n <= 100)
{
//Checks for the single digit
if(n < 10)
{
r = n % 10; //Finds the remainder
x[r] = n; //At the remainder position store the inputed number
}
else if(n < 100) //Checks for the double digits
{
r = n % 100; //Finds the remainder
x[r] = n; //At the remainder position store the inputed number
}
else
x[99] = n;
}
}
//Displays the data in sorted order
for(i = 0; i < 100; i++)
{
if(x[i] != 0) {
System.out.print(x[i]+\" \");}
}
System.out.println();
}
}
Solution
In Display Part you are printing each integer and a white space.Due to that space at end of your output printing sapce .except that everything is good as said you.i also tested.
in below code display part
for(i = 0; i < 100; i++)
{
if(x[i] != 0 && i!=99)
System.out.print(x[i]+\" \");
if(x[i] != 0 && i==99)
System.out.print(x[i]); //dispaly last element without space
}

