java


creating a class to store some integers Java [duplicate]


This question already has an answer here:
Storing a string and two doubles java
3 answers
I have a datafile, which I am trying to read and store its values,
The file looks something like this
Mammal fat_content(%) protein_content(%)
Bison 7.9 5.9
Guinea_pig 3.9 8.1
Dolphin 14.0 10.4
Donkey 1.4 1.7
The thing I am trying to do is to make a class which will store the doubles,
I have a scanner object in my main class like
Scanner scan=new Scanner(System.in);
String elementInfo=scan.nextLine(); //It will have the first line which is Mammal fat_content(%) protein_content(%)
Scanner elemInfo=new Scanner(elementInfo); //Scanner to parse the elementInfo
String kindOfElement=elementInfo.next(); // It will give the mammal
String [] varNames=new String[numOfVariables];
for(int i=0;i<numOfVariables;i++){ //I have made the for loop for this because there could be more variable values like fat_content and protein_content,.....,weight
varNames[i]=elemInf.next();
}
This code is just storing the names of the variables,but I want to make a class which will store a number of doubles for example fat_content would have its values stored in an array....
I am sorry if I am not making any sense, all I want is that to store all values of fat_content in an array with the help of a new class.....Any ideas would be appreciated.
Thanks,
Jeena
You just need a conversion between String to Double.
This:
String [] varNames=new String[numOfVariables];
varNames[i]=elemInf.next();
Should be:
double[] varNames = new double[numOfVariables];
varNames[i] = elemInf.nextDouble();
You might want to use List for this case. Mainly something like:
List<Double> fat_content = new ArrayList<Double>();
fat_content.add(elemInf.nextDouble());
Or
varNames[i] = elemInf.nextDouble();
Scanner has an added advantage of giving separated tokens according to type. (ref. here)

Related Links

Eclipse won't read a config file
How to order event in multiple streams in one single Epser Engine
Compile plugin code in Java without parent class body
How to use Enum.valueOf from Scala?
Why does Content-Length HTTP header field use a value other than the one given in Java code?
Do interfaces inherit from Object class in java
Jersey Client API - Getting JavaIOException premature EOF when consuming a service
is fantom generated bytecode as performant as java equivalent bytecode?
Object pooling alternatives in Java
Java inheritance
Getting amount of children with XOM
JMX MXBean Attributes all UNDEFINED - Spring 3.0.x/Tomcat 6.0
$1.class and $2.class not Serializable exception in jgroups
Naming for static final fields of java.io.File are not standard
PrintWriter vs FileWriter in Java
MacOS Java 6 mishandles AppleDisplayScaleFactor when set globally, and ignores it entirely when set application-specific

Categories

HOME
pandas
testng
lodash
bookshelf.js
at-command
yum
pivotal-cloud-foundry
awesome-wm
elasticsearch-hadoop
metatrader4
correlation
jplayer
windows-phone-7
jsprit
saxon
oracle-coherence
normalizr
samoa
openedx
swiftlint
kryo
tapestry
pepper
minitab
typo3-6.2.x
fabric8
socialengine
airconsole
scorm2004
copying
framemaker
git-merge
scaffold
x11-forwarding
event-driven
twitch
xenforo
streamreader
tasker
email-templates
android-mediaprojection
turbogears
unixodbc
flashair
osx-mavericks
filepicker
rails-routing
forever
kbuild
windows-iot-core-10
cudafy.net
django-scheduler
smart-table
vmware-tools
httplistener
savon
trash
simplewebrtc
rhino-servicebus
javax.sound.midi
tcpserver
msys
atk4
embedded-code
ideamart
service-accounts
ami
javax.mail
npapi
mdt
device-manager
code-access-security
starcluster
dml
system.net.webexception
commoncrypto
dexterity
google-cloud-save
gwt-rpc
jquery-mobile-dialog
factory-method
bluepill
runas
hosts-file
adk
libc++
file-comparison
compiler-specific
django-notification
ntvdm.exe

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App