• Post Reply Bookmark Topic Watch Topic
  • New Topic

Assistance with code  RSS feed

 
Jeff Boynton
Greenhorn
Posts: 18
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

I am writing a program that is supposed to take 3 numbers inputted by the user and sorting them out in an ascending order. I believe my code is correct but can't figure out why the program isn't behaving as expected.



Any pointers would be really appreciated!
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Jeff!

So... how is the program behaving, then? What does it do which you didn't expect? Does it throw exceptions? Produce incorrect output? Have a look at our FAQ entry TellTheDetails (<== click that link) to see what kind of information is helpful for people trying to answer questions.
 
Jeff Boynton
Greenhorn
Posts: 18
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems to work great up until the final "else if" statement. For instance, when I input 2 3 1 for numbers it comes back with 2 1 3. Forgive me for not knowing this. I am still really new to this!
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, have a look at these two lines of code:



The output from the second line suggests that you know that num1 is less than num3. But your test in the first line doesn't tell you that. For example the three numbers could be 3, 6, and 1. Now look at these two lines:



These two lines are good because the test you do matches the output you produce exactly. So I would change your code so all six comparisons work the same as this one.

 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to use arrays (read about it) for such a problems like this, when you need to store more than one input of the same type data.

import java.util.Scanner;

public class Array {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

// create an array of 3 elements
int[] array = new int[3];

// ask user input
System.out.println("Enter three numbers followed by space: ");

// store user's inputs in an array
for (int i = 0; i < 3; i++) {

array[i] = in.nextInt();
}

/*
* Over here, try to think and figure it out, how to sort these array elements.
* Each array element can be accessed by indexes.
* For ex. array[0] - 1st element in an array
* array [1] - 2nd
* array [2] - third element in array and it is your last element.
* As they start from index 0, 1, 2 << 3 (...new int[3])
*/


// print elements of array
for (int i = 0; i < 3; i++) {

System.out.println(array[i]);
}

in.close();
}
}



 
Jeff Boynton
Greenhorn
Posts: 18
Android Chrome Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That did it! Here is my final code. Everything works great! Thank you so much for the help.

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
!!! !!!

Ok, I understand that this is just Week 3 and you're probably in the early part of a learning progression. Hopefully, this is exercise just expects you to get a handle on how relational and boolean operations work. However, this is certainly not a good way to "sort" things with a computer program.
 
Jeff Boynton
Greenhorn
Posts: 18
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sure that there are much easier ways to do this, and yes you are correct this is week 3 of a course I am taking and the topic is boolean operators and how they work.
 
Jeff Boynton
Greenhorn
Posts: 18
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Out of curiosity, what would be the easier way to code this? Would an array be easier?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the usual approach is to have a container of some sort, like an array, to hold the items you want to sort. Then you would apply an algorithm to sort your collection of things and the algorithm usually involves a loop. A loop is a program execution flow control structure that repeats a set of instructions for a finite number of times. Each execution of the instructions in a loop is called an iteration. You'll learn all this eventually. If you're really curious, check out the Java Tutorials
 
Jeff Boynton
Greenhorn
Posts: 18
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the info! You all have been very helpful. I'm going to browse the site to familiarize myself a little more with what I have in store for me in the coming weeks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!