can't figure out
I've searched the archives and did not find a solution. I'm trying to get rid of the unchecked warnings in my code, and I can't figure this one out. I have a method that receives a Vector of Vectors. The internal Vectors are all strings. Here's the code:
public Vector Method1(Vector<Vector> dataVector)
{
for (int i = 0; i < dataVector.size(); i++)
{
Vector temp = (Vector)dataVector.elementAt(i);
for (int j = 0; j < temp.size(); j++)
{
String checkString = (String)temp.elementAt(j);
try
{
checkString = checkString.trim();
checkString = checkString.replaceAll("&"," ");
temp.setElementAt(checkString, j);
}catch (Exception nf){}
}
}
return dataVector;
}
I can read the elements of the temp vector fine, but I get unchecked warning when I try to write or change an element, such as the setElement line above. I've tried changing the way temp is created like this:
Vector<String> temp = (Vector)dataVector.elementAt(i);
AND
Vector<String>temp = new Vector<String>();
temp = (Vector)dataVector.elementAt(i);
but these both give me this error:
warning: [unchecked] unchecked conversion
found: java.util.Vector
required: java.util.Vector<java.lang.String>
What am I missing here? I'm assuing I'm missing something pretty stupid, but Im not seeing it!
Thanks,
James
[2059 byte] By [
jpifera] at [2007-11-27 10:51:50]

Vector < Vector < String > > vector = new Vector < Vector < String > > (); // perfectly legal
You aren't using generics completely. If you specify what type the vector stores, you don't need to cast when you retrieve an element, it will already be of the correct type. You should also consider moving from Vectors to ArrayLists, they're faster.
public Vector<Vector><String>> Method1(Vector<Vector><String>> dataVector) // it's a vector that holds vectors of strings.
{
for (int i = 0; i < dataVector.size(); i++)
{
Vector<String> temp = dataVector.elementAt(i);// you get back a Vector<String> reference, no need to cast.
for (int j = 0; j < temp.size(); j++)
{
String checkString = temp.elementAt(j);// temp is now a vector of strings, no need to cast it's elements to strings.
try
{
checkString = checkString.trim();
checkString = checkString.replaceAll("&", " ");
temp.setElementAt(checkString, j);
} catch (Exception nf) {}
}
}
return dataVector;
}
> You aren't using generics completely. If you specify
> what type the vector stores, you don't need to cast
> when you retrieve an element, it will already be of
> the correct type. You should also consider moving
> from Vectors to ArrayLists, they're faster.
> public Vector<Vector><String>>
> Method1(Vector<Vector><String>> dataVector) // it's a
> vector that holds vectors of strings.
> {
> for (int i = 0; i < dataVector.size(); i++)
> {
> Vector<String> temp = dataVector.elementAt(i);
> i);// you get back a Vector<String> reference, no
> need to cast.
>
> for (int j = 0; j < temp.size(); j++)
> {
>
> String checkString = temp.elementAt(j);// temp
> emp is now a vector of strings, no need to cast it's
> elements to strings.
> try
> {
> checkString = checkString.trim();
> checkString = checkString.replaceAll("&", "
> &", " ");
> temp.setElementAt(checkString, j);
> } catch (Exception nf) {}
> }
> }
> return dataVector;
> }
Seems the forum bug with angle-brackets is fixed, then :-)
> Seems the forum bug with angle-brackets is fixed,
> then :-)
Awesome. I'm gonna go ahead and take credit for fixing it. :)
> > Seems the forum bug with angle-brackets is fixed,
> > then :-)
>
> Awesome. I'm gonna go ahead and take credit for
> fixing it. :)
I'll back you up :-)
> > Seems the forum bug with angle-brackets is fixed,
> > then :-)
>
> Awesome. I'm gonna go ahead and take credit for
> fixing it. :)
But it isn't fixed, so does that mean I get to blame you?
~Tim
> > > Seems the forum bug with angle-brackets is
> fixed,
> > > then :-)
> >
> > Awesome. I'm gonna go ahead and take credit for
> > fixing it. :)
>
> But it isn't fixed, so does that mean I get to blame
> you?
>
> ~Tim
It isn't fixed :-( I mis-glanced
Hunter, you lying b@stard get it fixed!!
your code with the spaces was right, but hunters code had the bug :)
I never noticed that extra spaces prevented the bug from occuring. I'll have to remember that.
~Tim
> > > > Seems the forum bug with angle-brackets is
> > fixed,
> > > > then :-)
> > >
> > > Awesome. I'm gonna go ahead and take credit for
> > > fixing it. :)
> >
> > But it isn't fixed, so does that mean I get to
> blame
> > you?
> >
> > ~Tim
>
> It isn't fixed :-( I mis-glanced
>
> Hunter, you lying b@stard get it fixed!!
****, you're right. Fortunately, I only took credit for fixing it, not for breaking it. Since it isn't fixed, it's not my problem. :p
> > > > > Seems the forum bug with angle-brackets is
> > > fixed,
> > > > > then :-)
> > > >
> > > > Awesome. I'm gonna go ahead and take credit
> for
> > > > fixing it. :)
> > >
> > > But it isn't fixed, so does that mean I get to
> > blame
> > > you?
> > >
> > > ~Tim
> >
> > It isn't fixed :-( I mis-glanced
> >
> > Hunter, you lying b@stard get it fixed!!
>
> ****, you're right. Fortunately, I only took credit
> for fixing it, not for breaking it. Since it isn't
> fixed, it's not my problem. :p
But your willing to steal the glory will haunt you for years :-)
That makes sense, and I think I've seen that before, but what am I changing? The way the method receives dataVector?
The temp Vector is just a Vector of Strings.
So far everything I've tried based on your hint has not worked.
Thanks,
James
How can a program (main) be change to a class (doesn't run by itself)
for example in order to change this program and turn in into a class...
import java.util.Scanner;//working perfectly
public class AmountToText//Not taking credit for this
{
public static void main(String... args)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("enter whole amount ONLY ");
//AmountofCheck sn = new Amou
int[] amount = new int [1]; //can hold 1 elements p. 451
amount[0] = keyboard.nextInt(); //in the position 0
for (int n : amount)//was int
{
System.out.printf("%n%,d%n", n);
System.out.printf("%s%n", AmountToText(n));
}
}//ends main
static final String[] units = { "", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine" };
static final String[] teens = { "ten", "eleven", "twelve", "thirteen",
"fourteen", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen" };
static final String[] tens = { "twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety" };
static final String[] mil = { "", "thousand" };
public static String AmountToText(int num)// numToWords=NumberToWordsMT
{
if (num == 0)
{
return "cero";
}
StringBuilder sb = new StringBuilder();//var-length array has sequence of characters*concanates Good
/*if (num < 0)
{
sb.append("Negative");
}*/
String numStr = String.valueOf(num).replaceFirst("^-", "");//replaces- w/nothing
String[] parts = numStr.split("(?=(?:\\d\\d\\d)++$)");//split them in parts with 3 digits
int mag = parts.length - 1;
for (int i = 0; i <= mag; i++)
{
if (parsePart(parts[i], sb) && mag > 0 && i < 6)//6=mag
{
sb.append(" ").append(mil[mag - i]).append(",");
}
}
return sb.toString();
}
/*************************************************/
static boolean parsePart(String str, StringBuilder sb)
{
boolean notZeroes = false;
int pos = 0; // pos=index
char ch = (char)0;
if (str.length() == 3 && (ch = str.charAt(pos++)) != '0')
{ //if it has 3 positions & is diff than 0
ExtraSpace(sb);//invoke units and append hundred
sb.append(units[ch - '0']).append(" hundred");//position 0=hundred
notZeroes = true;
}
if (str.length() > 1 && (ch = str.charAt(pos++)) != '0')//>1&diff than 0
{
ExtraSpace(sb);
notZeroes = true;
if (ch == '1')//if position 1 has a 1
{
sb.append(teens[str.charAt(pos)]);//invoke teeens
return notZeroes;
}
sb.append(tens[ch - '2']);//otherwise invoke tens
}
if (str.length() > 0 && (ch = str.charAt(pos)) != '0')
{
ExtraSpace(sb);//if it is in position 0
sb.append(units[ch - '0']);//invoke units array
notZeroes = true;
}
return notZeroes;
}
/*************************************************/
static void ExtraSpace(StringBuilder sb)
{
if (sb.length() > 0)
{
sb.append(" ");
}
}
}//ends the begining
Patmea at 2007-7-29 11:33:46 >

> That makes sense, and I think I've seen that before,
> but what am I changing? The way the method receives
> dataVector?
>
> The temp Vector is just a Vector of Strings.
>
> So far everything I've tried based on your hint has
> not worked.
>
> Thanks,
> James
You're changing the fact that the method takes a Vector of raw Vectors. It should take a Vector of Vectors of Strings
If Vector is really what you want, that is
@PatMe: Why are you hijacking this thread with an irrelevant question?
> That makes sense, and I think I've seen that before,
> but what am I changing? The way the method receives
> dataVector?
Change the way you declare the references to the Vectors. Give them a generic type that matches what you want it to actually store. If you were using an ArrayList of Maps that held Points and ActionListeners, it would look like this:ArrayList < Map <Point, ActionListener> >
> The temp Vector is just a Vector of Strings.
Then you need to declare it as such. Vector<String>
is a vector of strings. Vector
is just a Vector of Objects.
> So far everything I've tried based on your hint has
> not worked.
>
> Thanks,
> James
> But your willing to steal the glory will haunt you
> for years :-)
Only until I get promoted to middle management. Then it'll be a highlight of my resume. :)
Thank you all very much, at least the part pertaining to my issue.... :-) I see the light, at least for now, and it's working.
I appreciate all the help!
Thanks,
James
I apologize I didn't mean to do that...
this program also deals with arrays and I was thinking maybe you know the answer since you seem very knowledgeable about this subject
Sorry if I bother you
Patmea at 2007-7-29 11:33:51 >

> I apologize I didn't mean to do that...
> this program also deals with arrays and I was
> thinking maybe you know the answer since you seem
> very knowledgeable about this subject
> Sorry if I bother you
Not so much that it bothers us, but it makes it difficult to
a) focus on the "real" question of the thread
b) makes it less likely your problem will get looked at
This thread isn't about arrays anyway!
> Thank you all very much, at least the part pertaining
> to my issue.... :-) I see the light, at least for
> now, and it's working.
>
> I appreciate all the help!
>
> Thanks,
> James
No problem
again, I apologize. I didn't mean to interrupt or anything like that
Patmea at 2007-7-29 11:33:51 >

> again, I apologize. I didn't mean to interrupt or
> anything like that
It's ok, just create your own thread for your question (just one thread through).
> again, I apologize. I didn't mean to interrupt or
> anything like that
It's no big deal, seriously! Just start your own thread
I just wanted to ask YOU a question. I was hoping you would help me with this question. You seem to know a lot about java programing
Thank you for understanding
Patmea at 2007-7-29 11:33:51 >

> I just wanted to ask YOU a question. I was hoping you
> would help me with this question. You seem to know a
> lot about java programing
> Thank you for understanding
Who are you addressing? Hunter? Pah! He knows nothing! I'm kidding of course, but in general questions aren't aimed at a particular user, the forum as a whole has a knowledge that - thanks to the peer review, and the debate that often happens - is greater than the sum of it's parts