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

Workaround for J2ME Hessian limitations?
Continuous Integration Servers [closed]
Dynamically load additional jar files using Web Start / JNLP
JAXB gives me: java.lang.IllegalArgumentException: is parameter must not be null
What is a good way to provide a different user experience based on the visitor type?
Does Java have native support for events, similar to that of C#?
How do you add files to a jar using Netbeans 6.5?
Has anyone successfully used signature timestamps in a java applet?
Is there a way to have expander icons for multiple roots in a JTree?
Find Java classes implementing an interface [duplicate]
Java window buffering keystrokes until the user clicks with the mouse
Java double comparison epsilon
Share file storage index with multiple open applications in Java
Launching a URL from an Eclipse plugin
Developing Web services applications from existing WSDL files
How to move only the current file with Eclipse and/or Ant?

Categories

HOME
cloud
listview
cookies
oracle11g
tinymce
plot
homebrew
getelementsbytagname
jira
amazon-ecs
packages
binary-tree
wamp
networkx
podio
swagger-ui
nstableview
jplayer
remote-access
zebra-printers
designer
netflix
autoconf
google-search-api
kudan
android-widget
shopware
uninstall
fopen
nat
r-raster
fabric8
key-value-observing
data-manipulation
google-sites-2016
android-ble
disassembling
bitbucket-pipelines
sharefile
twitch
reportbuilder
unobtrusive-validation
streamreader
powershell-dsc
android-mediaprojection
modelmapper
eclipse-gef
websphere-mq-fte
avconv
user-accounts
zip4j
rails-routing
python-webbrowser
fancybox-2
tizen-native-app
composite-key
arrow-keys
django-scheduler
crosswalk-runtime
nested-sets
medium.com
google-feed-api
angular-cache
android-textview
storekit
contact-list
holder.js
sigabrt
packagist
ptrace
wapiti
phpcas
wireshark-dissector
fouc
generic-programming
sniffer
oberon
system.reflection
coldbox
network-interface
android-imagebutton
phalanger
heisenbug
elliptic-curve
system.net.webexception
xceed-datagrid
braille
srs
pyhdf
.aspxauth
tidy
ruby-datamapper
cassini-dev
tinn-r
propertyeditor
buildr
amazon-appstore
code-cleanup
netdna-api
php-parser
cinema-4d
createwindow
uiq3

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App