• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Sorting vectors... a nightmare?

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, this is one of my first JAVA attempts, I am doing a project in which I have to make a program which allows the user to input a range of numeric values, and sorts the input numbers in an ascending order.
Another thing is that I am not supposed to use the Collections hence I cant do Collections.Sort instead I have to make a loop.
The difficulty I have it really getting an idea of how everything is structured together, like I know when I read a value from the keyboard its got to be wrapped to an integer, and also that a vector cannot accept raw integers (?) but I am not sure how to put it all together. If only I could find some detailed pseudocode for this.
As this is something I am doing for my college project it would be nice if you could give me hints, advice or guidelines instead of doing the project for me, because I have to understand how it works
Thank you very much
regards
Kaufmann
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you did not post the exact requirements of the assignment, it's not clear what you can or can not do. If you can set a reasonable limit on the number of integers entered from the keyboard, then a simple array of int and a bubble sort should do. You might consider posting the entire assignment, if possible.
 
Kirill NKaufmann
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it has to be done with a vector without using any pre-made class to sort such as Collection with a .sort method.
What I have to do is write a program that let the user input the numbers and each time it would ask done or would you like to input more. After it would be able to sort the numbers.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kirill
Perhaps you should break the problem into various pieces, and solve each piece. For example, do you know how to create a vector?
Do you know how to wrap an integer into an Integer object?
Do you know how to populate the vector?
Do you know how to move the elements of the vector around (i.e. change their relative position)?
Do you have the code for any sorting algorithm?
Is your answer "no" to all of these?
Allen
 
Kirill NKaufmann
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do you know how to create a vector?
yes,
Vector vec = new Vector();

Do you know how to wrap an integer into an Integer object?
no
Do you know how to populate the vector?
no
Do you know how to move the elements of the vector around (i.e. change their relative position)?
not yet but I can look that up on java.sun.com
Do you have the code for any sorting algorithm?
I have the so called bubble sort
Is your answer "no" to all of these?

My main problem is really putting it all together
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right, you cannot put primitive values into Vectors. You have to use the Wrapper class (eg. Integer) to wrap your primitive value, so it can be inserted into the Vector as an object. For example:
int primInteger = 312;
Integer wrappedInt = new Integer(primInteger);
//now that you have you primitive value wrapped
//add to vector
Vector v = new Vector;
v.add(wrappedInt);
I assume you know how to extract values from the vector.
To extract the value as a primitive you will have to use
intValue() which returns an int.
Hope that helps a bit!

 
Allen Alchian
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kirill,
Nisha's response gave you a quick-and-dirty example of wrapping an int into an Integer object. I'll assume you follow how that's done so I won't dwell on wrapping ints into Integer objects, unless you ask for more on the topic.
You wrote that you will get integers from "the user." This makes me wonder precisely how the user will provide those numbers. If the user inputs the numbers while the program runs, then I assume you are aware that you are actually dealing with Strings and not int values, and that you'll have to convert each String to an Integer object. Shall I assume you have a handle on this part of the problem?
Another aspect of the problem that you need to deal with is the fact that Vectors store objects of the Object class. Consequenly you will have to do some casting of each Object to an Integer when you manipulate the vector objects. Are you aware of this, and are you able to do this part of the problem?
If you haven't been exposed to the Java API Specification web site, I strongly encourage you to visit it and start getting familiar with it. It is a bit overwhelming at first, but with some experience you will eventually get the hang of the organization and how to use the information. Visit http://java.sun.com/j2se/1.3/docs/api/index.html
At that address three windows appear, one in the upper left, one in the lower left, and one on the right.
Let's use class Vector as our first excuse to visit the site. When it first loads, all Classes are displayed by default, in the bottom left window. You use the top left window to select the Package you want to view. The bottom left window will display the classes in the Package you select in the top left window. Scroll through the list of classes (bottom left window) to find the one you're interested in and when you click on a class, it's specification will show in the large right window.
Being sure you have All Classes selected in the top left window, scroll throught the list in the bottom left window until you find Vector. Click it and you'll see the description of Class Vector appear in the large right window. A lot of information is presented...it'll overwhelm you if you've never done this before. But take the time to learn how it's organized. I'd suggest printing it...it'll take several pages, but I think it's worth the investment of paper and ink to help you get the feel of the kind of information and how it's presented.
First you'll see a general description of the class. You'll see that the Vector class comes from (inherited from) the java.lang.Object class. This is an important point because this is the basis for all Vector elements being of class Object. Scroll down a bit and you'll see a "Field Summary", a "Constructor Summary" and a "Method Summary". They all contain very useful information about the Vector class that you'll be using to solve your problem.
At this point, I'll terminate my response because I've gotten long-winded. What aspects of your problem do you need help on now?
Allen
 
Kirill NKaufmann
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nisha, thank you very much for the practical example, I shall surely make use of it when I reach that stage.
Allen, that�s really great, you are extremely helpful I shall do some more research and look at the Java API Specification web site to try and figure some things out. As you might have guessed I did not know what the keyboard has to be converted to int objects, however I did suspect it
I think it would be of some help if I came up with some sort of a design model or a plan to stick to, I think I shall do that next now that have a basic idea of the aspects this project involves.
If you don�t mind I will post questions in this thread if I get stuck with something. Thanks again to all of you for your help which is being very useful.
regards
Kaufmann
 
Nisha Siddiqui
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Allen is right you maybe dealing with Strings, the approach would be similar. You can pass in the Strings into a Integer consructor (or any other wrapper class, except Character), be careful, it may throw a NumberFormatException and you should put the code in a try catch block incase the value is not a valid integer value. As you know Stings are objects, so you wouldn't have to convert them into Integer objects to add them to a vector, however it maybe easier for you when recovering the value from the vector to retrieve a Integer object and then just extract the int value from it.
I would be interested in finding out if Allen has any other not so quick and dirty methods to convert integer values into objects to be added to arrays!
As recommended do look at the documentation.
 
Kirill NKaufmann
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have made sort of a pseudocode for the project
is that more or less correct in terms of the conversion steps such as wrappers etc?
1) Create a Vector

2) Bring up on screen instructions (to enter a value)

3) Convert the input String to an integer object

4) After the value is entered ask t he user whether he or she wants to enter another value or to sort the numbers. If the user chooses to input another value go to step 2. If the user chooses to sort the values proceed to step 6.

5) Append the integer object into the Vector
6) Detect if only 1 or no values have been entered and give an error message and go to step 2. Else go to step 7
7) Use a loop to compare the elements in the Vector and sort them appropriately.

Thanks
Kaufmann
 
Sheriff
Posts: 17696
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kirill,
Suggestions:
#2: instructions would be to type a number and press Enter. Have the user type in something like "sort" or "done" to indicate that he/she is done entering numbers and wants to sort.
if you do it this way,
#4 will be: check if input is a number or if it the "sort" command. If it is a number, wrap it and add it to the vector and loop back. If it is the "sort" command, proceed with sort step. Otherwise, display "Invalid input.", remind user of the instructions, then loop back.
 
Allen Alchian
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kirill,
If your input mechanism can be simple, then I think the simplest (and fastest for data input) might be to post the full instructions once, at the start of the program, and then after after each user integer-input, just prompt the user with something like the following...
Enter another integer or press Enter to sort and display all data>
This way the user doesn't have to do anything but enter numbers and press enter during the data input phase. When all the numbers have been entered, a simple press of the Enter key with no data will signal the program it's time to sort the data.
If an invalid input is encountered, just display a simple error message then prompt for another input as before. It might look like this...
Invalid input
Enter another integer or press Enter to sort and display all data>
Of course there are as many ways to get input as there are programmers. Do what's best for you...just try to keep it simple.
Good luck.
------------
For Nisha,
You asked if I had "any other not so quick and dirty methods to convert integer values into objects to be added to arrays." No, Nisha, I use the same "quick and dirty" techniques.
Regards,
Allen
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kirill NKaufmann:
I have made sort of a pseudocode for the project
is that more or less correct in terms of the conversion steps such as wrappers etc?
1) Create a Vector

2) Bring up on screen instructions (to enter a value)

3) Convert the input String to an integer object

4) After the value is entered ask t he user whether he or she wants to enter another value or to sort the numbers. If the user chooses to input another value go to step 2. If the user chooses to sort the values proceed to step 6.

5) Append the integer object into the Vector
6) Detect if only 1 or no values have been entered and give an error message and go to step 2. Else go to step 7
7) Use a loop to compare the elements in the Vector and sort them appropriately.

Thanks
Kaufmann


Kirill,
In line with keeping the program simple, I suggest prompting the user to input how many integers they would like stored in the vector/array.
Then utilize the 'for' loop to place the user's input in to the vector/array. After the input "string(s)" is/are converted to integer format, you can then sort the integers within the vector in ascending order. I am not sure, however, how you want to compare the data or manipulate it in any other way.
I have made a program that does this and then displays the data contained within the vector on the screen for the user to see. Please let me know if posting this code may help you to get some ideas. I would be happy to do so.
Within my code, I used a class called SimpleIO, written by K.N. King. This class helps you to read a user's keyboard input. This class came with the book for a Java course that I am taking, and looks like this:
//////////////////////////////////////////////////////////////
// From JAVA PROGRAMMING: FROM THE BEGINNING, by K. N. King //
// Copyright (c) 2000 W. W. Norton & Company, Inc. //
// All rights reserved. //
// This program may be freely distributed for class use, //
// provided that this copyright notice is retained. //
// //
// SimpleIO.java (Appendix E, page 763) //
//////////////////////////////////////////////////////////////
// Provides prompt and readLine methods for console input
import java.io.*;
public class SimpleIO {
private static InputStreamReader streamIn =
new InputStreamReader(System.in);
private static BufferedReader in =
new BufferedReader(streamIn, 1);
// Displays the string s without terminating the current
// line
public static void prompt(String s) {
System.out.print(s);
System.out.flush();
}
// Reads and returns a single line of input entered by the
// user; terminates the program if an exception is thrown
public static String readLine() {
String line = null;
try {
line = in.readLine();
} catch (IOException e) {
System.out.println("Error in SimpleIO.readLine: " +
e.getMessage());
System.exit(-1);
}
return line;
}
}
Hope this helps Kirill.
 
Kirill NKaufmann
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I am done, I have learend a lot from this, thanks to all of you who have been so kind to give me a hand with this, I now understand how to write a program that uses vector class. Again, many thanks to all of you who have contributed.
Regards
Kaufmann
PS
My holliday is just 2 weeks ago, then I will really hack some JAVA code!
 
Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic