Reputation:
I'm still new to Java as I recently just took this subject in my course. I'm still puzzled in how to align the sorted inputs into its designated place.
for example you input: 1,2,3,4,5 the output would be all odd numbers will be aligned under the odd text/title then all even will fall under the even title.
Here is my code:
public static void main(String[] args) {
int num;
String odd="",even="",var, output="";
for (int cntr = 0; cntr < 5; cntr++){
var = JOptionPane.showInputDialog("Input Number: ");
num = Integer.parseInt(var);
if (num % 2 == 1)
odd = odd +"\n"+ String.valueOf(num);
if (num % 2 == 0)
even = even+"\n"+ String.valueOf(num);
output = odd +"\t"+even;
}
System.out.print("Odd\tEven"+ output);
The problem is that when you run it the output is that, it is sorted top digits are odds then bottom digits are even but its aligned under the odd title.
What can you recommend? New member here. Nice to meet you all!
Upvotes: 0
Views: 1689
Reputation: 1334
Try it like this
public static void main(String[] args)
{
int num;
ArrayList<String> odd = new ArrayList<>();
ArrayList<String> even = new ArrayList<>();
System.out.println("Odd\tEven");
for (int cntr = 0; cntr < 5; cntr++)
{
String var = JOptionPane.showInputDialog("Input Number: ");
num = Integer.parseInt(var);
if (num%2 == 0)
{
even.add(Integer.toString(num));
}else
{
odd.add(Integer.toString(num));
}
}
if (odd.size() > even.size())
{
while (even.size() < odd.size())
{
even.add("/");
}
}
else if (odd.size() < even.size())
{
while (odd.size() < even.size())
{
odd.add("/");
}
}
for (int i = 0; i<even.size(); i++)
{
if (!odd.get(i).equals("/"))
{
System.out.print(odd.get(i));
}
if (!even.get(i).equals("/"))
{
System.out.print("\t" + even.get(i));
}
System.out.println();
}
}
You save numbers into ArrayList as strings, and then ArrayList needs to have same size, because we will use just one loop. So in order to get them the same size, we fill them with "/", and if "/" is present when we are printing numbers, then we skip that entry. Hope it help
Edit: I've used ArrayList of Strings instead of ArrayList of Integers. Because, I couldn't fill the remaining spaces with some number. Sure, I could use Integer.MAX_VALUE or Integer.MIN_VALUE, but there is a chance that user can type that number in and then when the program would try to output numbers well, then this number would be skipped. So I ussed strings and for remaining spaces I used "/" because it's not a number. And please, use try-catch statment in this program. Especialy for converting from String to int.
Upvotes: 0
Reputation: 27946
If you are using Java 8, it provides a very nice way of partitioning data into two groups according to a predicate which seems ideal for your problem.
It would work like this:
List<Integer> myDigits;
Map<Boolean, List<Integer>> evenMap = myDigits.stream()
.collect(Collectors.partitioningBy(n -> n % 2 == 0);
You have now got two list of numbers: evenMap.get(true)
and evenMap.get(false)
for even and odd numbers respectively.
Printing them in two columns is not much more difficult. The only difficult part is coping with the shorter of the two lists. Once you have the Map
above the length of the longer list can be derived using evenMap.values().stream().map(List::size).max().orElse(0)
.
I hope that's not too confusing if you are just learning Java but I do think it's worth getting used to using Stream
as it is a very powerful and useful construct.
Upvotes: 1
Reputation: 24780
Wrong approach, you correctly differentiate odd and even numbers but the Strings are not the way to print them as stated. You end with:
odd = "1\n3\n5\n"
even = "2\n4\n"
output = "1\n3\n5\n\t2\n4\n"
That is, all odds number (one after the other), the a tab, and then the even numbers.
You need to store the numbers in a more structured way; best option being two java.util.ArrayList<Integer>
. After you have the two lists of integers, printing them should be easier (for the first line, print the first even number, the tab, and then the first odd number; do not forget to check if the lists have different sizes).
This has the added benefit that, if tomorrow the way of presenting the results changes, you do not need to change the logic that puts each number in its lists (decoupling business logic from presentation/UI).
Upvotes: 1