#Extracting float from strings ( .obj file parsing )

22 messages ยท Page 1 of 1 (latest)

native lion
#

Hi, I am working on a basic .obj renderer and ran into a problem when parsing, I was trying to get float values out of a vector of strings ( I just used Scanner to get the file's strings into a vector )

my current approach is to use split() then valueOf() but valueOf() seems to be giving me nothing but trouble
current code is as follows

public static void getFloats(Scanner scn){
        i = 0;
        while(scn.hasNextLine()){
            String[] str = File.get(i).split("  "); // obj fileLine = "v  1  0  1"
            float value = str[2].valueOf(float);
            Floats.add(value);
            i++;
        }
    }

is there an easier way to get those values or am I just doing it wrong?

cloud shoreBOT
#

โŒ› This post has been reserved for your question.

Hey @native lion! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

formal magnet
#

that doesn't work at all

#

String.valueOf means to get a String out of the given value

#

use Float.valueOf to get a Float, or more accurately Float.parseFloat to get a float

#

or you could use a scanner over the string and use nextFloat, that uses parseFloat as well

#

why not doubles, btw?

#

(sidenote, these are called arrays in java. Vector exists as a class but it isn't used much in favor of ArrayList)

#

also wouldn't this be an infinite loop? the loop checks if scn has a next line but you never consume from it

native lion
#

oh no I am using a Vector class currently for the File variable but Ill be sure to change it over later

but wouldn't the scanner running over the string throw an error with the file format? because of the v in the File string( v 1 0 1)

as for the infinite loop thats an artifact from before I changed it

formal magnet
#
  1. ๐Ÿ‘
native lion
#

ohh

#

๐Ÿ‘

formal magnet
#
  1. a bit confused there but ok. you're using an array here and i thought you meant that
native lion
#

ill try that brb

#

its not that clear honestly xD

formal magnet
#

/run

String line = "v  1  0  1";
String[] str = line.split("  ");
System.out.println(Float.parseFloat(str[2]));
untold cosmosBOT
#

Here is your java(15.0.2) output @formal magnet

0.0
formal magnet
#

also consider renaming that string array

cloud shoreBOT
#

๐Ÿ’ค Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.