• 13
• 18
• 19
• 27
• 10

# How do I add Floats into a Vector?

This topic is 2571 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Dumb [color="#FF0000"][censor] won't [color="#FF0000"][censor] let me anymore..... I have no real clue wtf I'm doing and I need help.... this is part of a test and I need to add the coordinates xyz into a vector. As it stands i'll be able to read them just fine. So I want to add the variables of X,y,z into a vector (they must be float) But when I implement the add function it just says incompatible type or some [color="#FF0000"][censor] error.... what am I doing wrong? And how [color="#FF0000"][censor] am I?

import java.util.*; import java.io.*; public class Project02 {Vector<TwoDimensions> TwoDim = new Vector<TwoDimensions>() ; Vector<ThreeDimensions> ThreeDim = new Vector<ThreeDimensions>() ; int coord; float x,y,z; public static void main(String[] args) { Project02 proj2=new Project02(); proj2.Scanner(); } public Project02() { } public void Scanner() {try { File file = new File("entrada.txt") ; Scanner s = new Scanner(file) ; while(s.hasNext()) { int Dim = s.nextInt() ; if (Dim==3) { x = s.nextFloat() ; y = s.nextFloat() ; z = s.nextFloat() ; System.out.println("Dim: " + Dim + "\nx: " + x +"\ny: "+ y +"\nz: "+ z +"\n") ; } else if (Dim==2) { x = s.nextFloat() ; y = s.nextFloat() ; System.out.println("Dim:"+ Dim+"\nx: " + x +"\ny: "+ y +"\n"); } } s.close() ; } catch(Exception e) { e.printStackTrace() ; } } public class TwoDimensions { } public class ThreeDimensions { } } 

So yeah.. how pathethic is this code eh? XD I prolyl should just quit.... Edited by jbadams
Censored profanities -- please avoid pointless use of such language in future.

##### Share on other sites

First you read your floats and creeate the correct type of mathematical vector (2 dimensiona/3 dimesional). Then you add that mathematical vector to the Vector container of the appropriate type. Trying to add a 2d vector to a Vector<3 dimesions> won't work so make sure your adding tot he right type.

Its rather confusing with data containers called Vector and maths type vectors there.

if dimensions == 2 create 2d vector add to vector of 2d vectors else if dimensions == 3 create 3d vetcor add vector to vector of 3d vectors else perhaps write some error end 
It also helps to get help if you swear less in your posts.

##### Share on other sites
If you are adding x, y and z individually and getting bad type, perhaps your vector is made of vectors (we herd u liek vectors) and you would have better luck putting the xyz in the vec3d format your vector takes and then adding that vector to the vector.

##### Share on other sites
Sorry for the swearing... It's just.... I've coming to terms with the fact that I"m essentially a worthless programmer. So worthless that I cannot understand a word that you guys are saying anymore.... the code up there compiles and runs. I simply created vectors with generics. Tried to add but nothign worked apprently creating a vector withouth generics works just fine, but I can't acces anything due to indifferent type or no identefire..... I'm this close to quitting.... been stuggling with programming for clsoe to 6 months can't understand anything..... and well as you can see I'm probably not even cut out for a stupid java program [color="#FF0000"][censor] great. Edited by jbadams
Removed inappropriate language.

##### Share on other sites
[color="#000000"]The Vector<TwoDimensions> does not take floats, it takes TwoDimesion objects. The Vector<ThreeDimensions> also does not take floats; it takes ThreeDimensions objects. So first you need to create an appropriate object

if dimensions == 2 then float x = however you get x; float y = however you get y; TwoDimensions vec(x, y); // create an actual TwoDimension object TwoDim.push_back(vec); // add it to the appropriate vector end 

I've never used java so I don't know the functions for adding to a vector but you should get the general idea. You need to add a constructor to your TwoDimensions object that takes 2 floats (x and y). Personally I would use only ThreeDimensions object and set the z component as 0. They can both go in the same Vector<ThreeDimensions> then.

##### Share on other sites
I took a break... and I cleared my head and I think I got it to work. It'll add (to my knowledge) to the vectors... now my question is how do use the getters? They kind of won't work. Tell me that float or something...

Here's my new code

import java.util.*; import java.io.*; public class Project02 { Vector<Float> TwoDim=new Vector<Float>(); Vector<Float> ThreeDim= new Vector<Float>(); int coord; float x,y,z; public static void main(String[] args) { Project02 proj2=new Project02(); proj2.Scanner(); } public Project02() { } public void Scanner() {try { File file = new File("entrada.txt") ; Scanner s = new Scanner(file) ; while(s.hasNext()) { int Dim = s.nextInt() ; if (Dim==3) { x = s.nextFloat() ; y = s.nextFloat() ; z = s.nextFloat() ; System.out.println("Dim: " + Dim + "\nx: " + x +"\ny: "+ y +"\nz: "+ z +"\n") ; ThreeDim.add(x); ThreeDim.add(y); ThreeDim.add(z); } else if (Dim==2) { x = s.nextFloat() ; y = s.nextFloat() ; System.out.println("Dim:"+ Dim+"\nx: " + x +"\ny: "+ y +"\n"); TwoDim.add(x); TwoDim.add(y); } } s.close() ; } catch(Exception e) { e.printStackTrace() ; } } }

##### Share on other sites
Looking at it, it appears that you will end up with two vectors. TwoDim will have 2 * the number of two dimensional points in the file floats. ThreeDim will have 3 * the number of three dimensional points in the file. The floats should be arranged in the vectors so that (for TwoDim) the first two floats are from the first two dimensional point, the third and forth floats are from the second point, and so on.

##### Share on other sites

Looking at it, it appears that you will end up with two vectors. TwoDim will have 2 * the number of two dimensional points in the file floats. ThreeDim will have 3 * the number of three dimensional points in the file. The floats should be arranged in the vectors so that (for TwoDim) the first two floats are from the first two dimensional point, the third and forth floats are from the second point, and so on.

I don't understand.... I want two vectors. One for 2D and one for 3D. After that all I want is to save that in the vectors order them from closest to farthest from the point of origin and save them in an output file. But they don't work anymore due to the get's not being able to handle floats apprently...

##### Share on other sites

[quote name='EricTheRed' timestamp='1299370491' post='4782256']
Looking at it, it appears that you will end up with two vectors. TwoDim will have 2 * the number of two dimensional points in the file floats. ThreeDim will have 3 * the number of three dimensional points in the file. The floats should be arranged in the vectors so that (for TwoDim) the first two floats are from the first two dimensional point, the third and forth floats are from the second point, and so on.

I don't understand.... I want two vectors. One for 2D and one for 3D. After that all I want is to save that in the vectors order them from closest to farthest from the point of origin and save them in an output file. But they don't work anymore due to the get's not being able to handle floats apprently...
[/quote]

But here's the question: do you want 2 vectors of floats (what you have now), or a vector of 2d points and a vector of 3d points? Right now you're just storing successive floats in separate vectors. If you design such that when you then call for the various points, you just group your calls in 2's for the 2d and 3's for the 3d, you should be okay. Another option is to create simple 2d and 3d point classes to store the x, y, an sometimes z variables, and then do all your further operations on the point classes (adding/removing them from vectors, ordering them, etc).

eg:

 public class 2dPoint { public float x, y; public 2dPoint(float x, float y) { this.x = x; this.y = y; } } 

##### Share on other sites
1. You seem to have stopped already, but please try to avoid swearing like that in future.

2. You're not a worthless programmer unless you give up; this is a learning opportunity, and solving your current problems will better prepare you for similar situations in future.

3. "They kind of won't work" isn't a very good description of the problem. Help us to help you by describing your problems in detail.

When looking for help with programming problems, you should::
• Describe what you want to happens, so we know what you're trying to achieve.
• Describe what actually happens, so we have an idea of what is going wrong.
• Show any error messages you are receiving -- the exact message.
• Show relevant samples of code, as you are already doing.