> Can anyone point me to some information about
> Vectors. I want to write a program that will read
> numbers from a Text file and sort them.
Don't use a Vector, use an ArrayList.Here's a good guide to get you started with Collections:
http://java.sun.com/j2se/1.5.0/docs/guide/collections/index.html
> The assignment I'm tring to do require me to use
> Vector objects with primitive data types. The chapter
> I read was very short will little examples.
Either you misunderstood your assignment or it's completely bogus. That is absolutely unequivocally impossible. You cannot ever have a Vector of primitive types.
Are you talking about an array?
Sorry, yeah its an array. The section is on Primitive Data Types and the class Vector. I'm suppose to read number from a text file that are out of order then the program is suppose to sort them as the numbers are read in. Don't laugh at what I did but here what I written so far:
import java.util.*;
import java.io.*;
public class BldList;
{
public static void main(String[] args)
FilesNotFoundException
{
Scanner inFile = new Scanner(new FileReader("Numbers.txt"));
int number;
number = infile.nextInt( );
numList.addElement (new Integer(number) );
for (int m = 1; m < 20; m++)
number = infile.nextInt( );
boolean insert = false;
{
for(numList.size( ))
}
Integer temp = (Integer) listNum.elementAt(i);
if (temp.intValue( ) > number)
{
listNum.insertElementAt(new Integer(number), i);
inserted = true;
break;
}
if (!inserted) listNum.addElement(new Ineger(number));
System.out.println((Integer)numList.elementAt(count));
}
}
This is my first semester in Java, so I'm still learning.
Well you've got a good start and it looks like more or less the right idea. You basically just want a loop that reads each number in and then iterates backwards over the Vector until it finds a number it's not bigger than or reaches the beginning, then insert it there.
I would start with getting a good clean loop for reading in each number. Right now you have a lot of code that looks like it is leftovers and can be deleted. I also see nothing to handle exceptions thrown.
> There are loops that look back over a file,
> interesting.. I'll look in my textbook for that. The
> school book I have really sucks on examples.
Not over the file, over the Vector. But don't worry about that yet. Your loop that gets the numbers would look something like this:
for (int num = inFile.nextInt(); inFile.hasNext(); num = inFile.nextInt()) {
numberList.add(new Integer(num));
}
There's no error handling there. But that's probably beyond the scope of your assignment.
Do it look better now?
import java.util.Vector;
import java.io.*;
public class BldList
{
public static void main(String[] args)
throws FileNotFoundException
{
Scanner inFile = new Scanner(new FileReader("Numbers.txt"));
PrintWriter outFile = new PrintWriter("Number.out");
Vector<Integer> numList = new Vector<Integer>(20);
int number;
boolean inserted = false;
System.out.println("Size: " + numList.size());
System.out.println("Capacity: " + numList.capacity());
number = inFile.nextInt();
numList.addElement (new Integer(number) );
//for (int loop = 1; loop < 20; loop++)
{
for(int index = 0; index < numList.size(); index++);
}
//for (int number = inFile.nextInt(); inFile.hasNext(); number = inFile.hasNext())
//{
//numList.add(new Integer(number));
//}
Integer temp = (Integer) listNum.elementAt(index);
if (temp.intValue() > number)
{
listNum.insertElementAt(new Integer(number), index);
inserted = true;
break;
}
if (!inserted) listNum.addElement(new Integer(number));
for (int count = 1; count < 20; count++)
System.out.println((Integer)numList.elementAt(count));
inFile.close();
outFile.close();
}
}
This should look better.
import java.util.Vector;
import java.io.*;
import java.util.*;
public class BldList
{
public static void main(String[] args)
throws FileNotFoundException
{
Scanner inFile = new Scanner(new FileReader("Numbers.txt"));
PrintWriter outFile = new PrintWriter("Number.out");
Vector<Integer> numList = new Vector<Integer>();
int number;
number = inFile.nextInt();
numList.addElement (new Integer(number));
boolean inserted = false;
System.out.println("Size: " + numList.size());
System.out.println("Capacity: " + numList.capacity());
for (int loop = 1; loop < numList.size(); loop++)
{
System.out.println(numList.get(loop));
}
//for (int number = inFile.nextInt(); inFile.hasNext(); number = inFile.hasNext())
//{
//numList.add(new Integer(number));
//}
Integer temp = (Integer) numList.elementAt(0);
if (temp.intValue() > number)
{
numList.insertElementAt(new Integer(number), 0);
inserted = true;
break;
}
if (!inserted) numList.addElement(new Integer(number));
for (int count = 1; count < 20; count++)
System.out.println((Integer)numList.elementAt(count));
inFile.close();
outFile.close();
}
}
When I run my code I get this:
-jGRASP exec: java BldList
Your sorted vector
42
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
at java.util.Vector.elementAt(Unknown Source)
at BldList.main(BldList.java:40)
-jGRASP wedge2: exit code for process is 1.
-jGRASP: operation complete.
I'm reading in a text file with integers as such that must be sorted in the end:
42
468
335
1
170
225
479
359
463
465
206
146
282
329
462
492
496
443
328
437
What is stoping me for going through the list?
import java.util.Vector;
import java.io.*;
import java.util.*;
public class BldList
{
public static void main(String[] args)
throws FileNotFoundException
{
Scanner inFile = new Scanner(new FileReader("Numbers.txt"));
PrintWriter outFile = new PrintWriter("Number.out");
Vector<Integer> numList = new Vector<Integer>();
int number;
number = inFile.nextInt();
numList.addElement (new Integer(number));
//above line converts primitive int to an object and adds it to the vector
System.out.println("Your sorted vector\n");
/*to add the Integer objects in sorted order, you loop from 1 to 20: */
for (int insert = 1; insert < 20; insert++)
number = inFile.nextInt(insert);//reads it in
boolean inserted = false;
/* Use an inner loop to loop from 0 to numList.size(). In this loop you will compare each object in the vector
with the one you are trying to inset to find the proper positin for it*/
{
for (int loop = 0; loop < numList.size(); loop++)
number = inFile.nextInt();
Integer temp = (Integer) numList.elementAt(0);//retrieves Objeect at the position 'loop'
if (temp.intValue() > number) //compares number to the integer equivalent of temp
{
numList.insertElementAt(new Integer(number), 0); //inserts into corrrect place
inserted = true; // you've found it so set inserted to true
break;// break out of loop once number's been inserted
}
}
/* Once the for loop is done, check to see if you've inserted the item.
If not, it must be bigger than everything in the vector so insert it at the end*/
if (!inserted) numList.addElement(new Integer(number)); // adds at end
/*Once the vector has been populated in sorted order, then print it out usinf the
elementAt method. Use a for loop that iterates fron 1 to 20. Don't forget to cast it to an Integer
before printing.*/
for (int count = 1; count < 20; count++)
System.out.println((Integer)numList.elementAt(count));//To cast it to an integer and Print it out.
inFile.close();
outFile.close();
}
}
I added the hints my teacher gave me to work this problem. I redid some of the code but got this error:
Exception in thread "main" java.util.InputMismatchException: radix 1 less than Character.MIN_RADIX
at java.util.Scanner.nextInt(Unknown Source)
at BldList.main(BldList.java:25)
Can anyone help a brother out. I need to have this done by Friday for the last day of class.
sorry for the code button..
import java.util.Vector;
import java.io.*;
import java.util.*;
public class BldList
{
public static void main(String[] args)
throws FileNotFoundException
{
Scanner inFile = new Scanner(new FileReader("Numbers.txt"));
PrintWriter outFile = new PrintWriter("Number.out");
Vector<Integer> numList = new Vector<Integer>();
int number;
number = inFile.nextInt();
numList.addElement (new Integer(number));
//above line converts primitive int to an object and adds it to the vector
System.out.println("Your sorted vector\n");
/*to add the Integer objects in sorted order, you loop from 1 to 20: */
for (int insert = 1; insert < 20; insert++)
number = inFile.nextInt(insert);//reads it in
boolean inserted = false;
/* Use an inner loop to loop from 0 to numList.size(). In this loop you will compare each object in the vector
with the one you are trying to inset to find the proper positin for it*/
{
for (int loop = 0; loop < numList.size(); loop++)
number = inFile.nextInt();
Integer temp = (Integer) numList.elementAt(0);//retrieves Objeect at the position 'loop'
if (temp.intValue() > number) //compares number to the integer equivalent of temp
{
numList.insertElementAt(new Integer(number), 0); //inserts into corrrect place
inserted = true; // you've found it so set inserted to true
break;// break out of loop once number's been inserted
}
}
/* Once the for loop is done, check to see if you've inserted the item.
If not, it must be bigger than everything in the vector so insert it at the end*/
if (!inserted) numList.addElement(new Integer(number)); // adds at end
/*Once the vector has been populated in sorted order, then print it out usinf the
elementAt method. Use a for loop that iterates fron 1 to 20. Don't forget to cast it to an Integer
before printing.*/
for (int count = 1; count < 20; count++)
System.out.println((Integer)numList.elementAt(count));//To cast it to an integer and Print it out.
inFile.close();
outFile.close();
}
}
Always use braces after a 'for', 'while', 'if', 'else if', 'else', etc., even if there is only one statement. Makes the code easier to maintain and read.
/* Use an inner loop to loop from 0 to numList.size(). In this loop you will compare each object in the vector
with the one you are trying to inset to find the proper positin for it*/
{ // WHAT IS THE POINT OF THIS BLOCK (it's not an if, for, while, etc.)
// MAYBE THIS NEXT FOR LOOP IS SUPPOSED TO DO MORE THAN READING nextInt OVER AND OVER
for (int loop = 0; loop < numList.size(); loop++)
number = inFile.nextInt();
You have some serious logic flaws in your code.
One such error is your trying to access some 20 elements of your vector, however, you only push one object into you vector.
Also your code formatting is very hard to read and follow. What are you tyring to do with that thing you call an inner loop-to-loop? Get rid of it..
Also Until you become a Java Zen master, always use the curly braces for any if, for, while, else, try/catch constructs in your code, even if you only have one statement. It will save you headaches in the long run.
I would highly suggest you download an integrated IDE such as Netbeans, Eclipse, or BlueJay and dump what ever your using now to edit your code.
Those will help you with keeping with current codeing standards.
Have fun!
JJ
I was using only compiler that my instructor told me to use and the comments in my code are her hints to doing the code. I'm not trying to be a programmer only pass the class and move on. With few examples to go by is very hard for me. We ran out of time this semester so we haven't got to do exception handling, so I don't know nothing about try/catch constructs. I'm trying to do the Vector and Inheritage programs so I can chill for the summer.
Ok people, (even though I know I'm talking to myself)
I got an output that look like this:
Your unsorted values:
42
42
468
42
1
42
335
1
42
170
335
1
42
170
225
335
468
335
1
170
225
1
42
170
225
335
468
1
42
170
225
335
359
468
1
42
170
225
335
359
463
468
1
42
170
225
1
42
170
1
42
146
170
206
225
335
1
42
146
170
206
225
282
335
1
42
146
170
206
225
282
329
335
359
463
1
42
146
170
206
225
282
329
335
359
462
463
465
468
335
1
170
225
479
359
463
465
206
146
282
329
462
1
42
146
170
206
225
282
329
335
359
462
463
465
468
335
1
170
225
479
359
463
465
206
146
282
329
462
492
1
42
146
170
206
225
282
329
335
359
462
1
42
146
170
206
225
282
329
1
42
146
170
206
225
282
328
329
335
359
443
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
Still too many number. just suppose to be 17.
I got it to print the corract amount of numbers but they are still not sorted yhey come out like this:
Your sorted values:
42
328
443
462
329
282
146
206
465
463
359
225
170
1
335
42
468
335
1
170
This is what I done to the code
import java.util.Vector;
import java.io.*;
import java.util.*;
public class BldList
{
public static void main(String[] args)
throws FileNotFoundException
{
Vector<Integer> numList = new Vector<Integer>(20);
Scanner inFile = new Scanner(new FileReader("Numbers.txt"));
int number;
number = inFile.nextInt();
numList.addElement(new Integer(number));
System.out.println("Your sorted values:\n" + number);
//above line converts primitive int to an object and adds it to the vector
/*to add the Integer objects in sorted order, you loop from 1 to 20: */
for (int x = 1; x < 20; x++)
{
number = inFile.nextInt();//reads it in.
//Also set a boolean variable called inserted to false.
boolean inserted = false;
/* Use an inner loop to loop from 0 to numList.size(). In this loop you will compare each object in the vector
with the one you are trying to insert to find the proper positin for it*/
for (int i = 0; i < numList.size(); i++)
{
Integer temp = (Integer)numList.elementAt(i);//retrieves Objeect at the position 'loop'
if (temp.intValue() > number) //compares number to the integer equivalent of temp
{
numList.insertElementAt(new Integer(number), 0); //inserts into corrrect place
inserted = true; // you've found it so set inserted to true
break;// break out of loop once number's been inserted
}
}
if (!inserted) numList.addElement(new Integer(number)); // adds at end
else
numList.addElement(number);
}
/* Once the for loop is done, check to see if you've inserted the item.
If not, it must be bigger than everything in the vector so insert it at the end*/
/*Once the vector has been populated in sorted order, then print it out using the
elementAt method. Use a for loop that iterates fron 1 to 20. Don't forget to cast it to an Integer
before printing.*/
for (int order = 1; order < 20; order++)
{
System.out.println((Integer)numList.elementAt(order));//To cast it to an integer and Print it out.
}
inFile.close();
}
}
Look at this snipit of code.
// Dont need the 20. Vectors grow Dynamically
Vector<Integer> numList = new Vector<Integer>();
Scanner inFile = new Scanner(new FileReader("f:\\Numbers.txt"));
int number = 0;
// Look at this while loop. Isnt it much cleaner and easier
// to understand?
while (inFile.hasNext()) {
// Read the number in
number = inFile.nextInt();
// The next 2 system prints are for debugging only
System.out.print("Adding " + number + " to the Vector. ");
System.out.println(
"The Vector size is: " + (numList.size() + 1));
// Put the number into the Vector
numList.addElement(new Integer(number));
// Do more Stuff if you need to...
}
Dont you think it is easier to read and follow?
JJ
> After 19 Hours I fixed my problems. One more
> assignment to do and I am thru for the summer. Thank
> you Yoda.
I fiddled with your code for about 15 minutes, chopped it down to 21 lines of actual code and I have a program that outputs the following:
Adding 42 to the Vector. The Vector size is: 1
Adding 468 to the Vector. The Vector size is: 2
Adding 335 to the Vector. The Vector size is: 3
Adding 1 to the Vector. The Vector size is: 4
Adding 170 to the Vector. The Vector size is: 5
Adding 225 to the Vector. The Vector size is: 6
Adding 479 to the Vector. The Vector size is: 7
Adding 359 to the Vector. The Vector size is: 8
Adding 463 to the Vector. The Vector size is: 9
Adding 465 to the Vector. The Vector size is: 10
Adding 206 to the Vector. The Vector size is: 11
Adding 146 to the Vector. The Vector size is: 12
Adding 282 to the Vector. The Vector size is: 13
Adding 329 to the Vector. The Vector size is: 14
Adding 462 to the Vector. The Vector size is: 15
Adding 492 to the Vector. The Vector size is: 16
Adding 496 to the Vector. The Vector size is: 17
Adding 443 to the Vector. The Vector size is: 18
Adding 328 to the Vector. The Vector size is: 19
Adding 437 to the Vector. The Vector size is: 20
The Vector Contains:
1
42
146
170
206
225
282
328
329
335
359
437
443
462
463
465
468
479
492
496
And 3 of those lines do the printing to the screen. All the real work I did in the while loop I posted above. Inserting the integers in order..
JJ