How would I use hashMap?
up vote
0
down vote
favorite
How would hashmap apply to this specific code, when an element can be anything within the periodic table if its from another source outside of the code.
public class element {
public static void main(String args)throws IOException{
Scanner kbd = new Scanner(System.in);
File file = new File("elements.txt");
Scanner read = new Scanner(file);
System.out.println("Enter a chemical compound: ");
String compound = kbd.nextLine();
compound = compound+" ";
System.out.println("Chemical compound of "+compound);
for (int x=0; x<compound.length();x++){
if (compound.charAt(x)=='H'){
String Hvalue = String.valueOf(compound.charAt(x+1));
if (Character.isLetter(compound.charAt(x+1))) {
Hvalue = String.valueOf(1);
} else if (Character.isWhitespace(compound.charAt(x+1))){
Hvalue = String.valueOf(1);
}
System.out.println(Hvalue+" part/s Hydrogen");
} else if (compound.charAt(x)=='C'){
String Cvalue = String.valueOf(compound.charAt(x+1));
if (Character.isLetter(compound.charAt(x+1))) {
Cvalue = String.valueOf(1);
} else if (Character.isWhitespace(compound.charAt(x+1))){
Cvalue = String.valueOf(1);
}
System.out.println(Cvalue+" part/s Carbon");
}
} else if (Character.isWhitespace(compound.charAt(x+1))){
Ovalue = String.valueOf(1);
}
System.out.println(Ovalue+" part/s Oxygen");
}
}
}
}
java text
add a comment |
up vote
0
down vote
favorite
How would hashmap apply to this specific code, when an element can be anything within the periodic table if its from another source outside of the code.
public class element {
public static void main(String args)throws IOException{
Scanner kbd = new Scanner(System.in);
File file = new File("elements.txt");
Scanner read = new Scanner(file);
System.out.println("Enter a chemical compound: ");
String compound = kbd.nextLine();
compound = compound+" ";
System.out.println("Chemical compound of "+compound);
for (int x=0; x<compound.length();x++){
if (compound.charAt(x)=='H'){
String Hvalue = String.valueOf(compound.charAt(x+1));
if (Character.isLetter(compound.charAt(x+1))) {
Hvalue = String.valueOf(1);
} else if (Character.isWhitespace(compound.charAt(x+1))){
Hvalue = String.valueOf(1);
}
System.out.println(Hvalue+" part/s Hydrogen");
} else if (compound.charAt(x)=='C'){
String Cvalue = String.valueOf(compound.charAt(x+1));
if (Character.isLetter(compound.charAt(x+1))) {
Cvalue = String.valueOf(1);
} else if (Character.isWhitespace(compound.charAt(x+1))){
Cvalue = String.valueOf(1);
}
System.out.println(Cvalue+" part/s Carbon");
}
} else if (Character.isWhitespace(compound.charAt(x+1))){
Ovalue = String.valueOf(1);
}
System.out.println(Ovalue+" part/s Oxygen");
}
}
}
}
java text
1
Basic I/O
– Kevin Anderson
Nov 19 at 11:58
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
How would hashmap apply to this specific code, when an element can be anything within the periodic table if its from another source outside of the code.
public class element {
public static void main(String args)throws IOException{
Scanner kbd = new Scanner(System.in);
File file = new File("elements.txt");
Scanner read = new Scanner(file);
System.out.println("Enter a chemical compound: ");
String compound = kbd.nextLine();
compound = compound+" ";
System.out.println("Chemical compound of "+compound);
for (int x=0; x<compound.length();x++){
if (compound.charAt(x)=='H'){
String Hvalue = String.valueOf(compound.charAt(x+1));
if (Character.isLetter(compound.charAt(x+1))) {
Hvalue = String.valueOf(1);
} else if (Character.isWhitespace(compound.charAt(x+1))){
Hvalue = String.valueOf(1);
}
System.out.println(Hvalue+" part/s Hydrogen");
} else if (compound.charAt(x)=='C'){
String Cvalue = String.valueOf(compound.charAt(x+1));
if (Character.isLetter(compound.charAt(x+1))) {
Cvalue = String.valueOf(1);
} else if (Character.isWhitespace(compound.charAt(x+1))){
Cvalue = String.valueOf(1);
}
System.out.println(Cvalue+" part/s Carbon");
}
} else if (Character.isWhitespace(compound.charAt(x+1))){
Ovalue = String.valueOf(1);
}
System.out.println(Ovalue+" part/s Oxygen");
}
}
}
}
java text
How would hashmap apply to this specific code, when an element can be anything within the periodic table if its from another source outside of the code.
public class element {
public static void main(String args)throws IOException{
Scanner kbd = new Scanner(System.in);
File file = new File("elements.txt");
Scanner read = new Scanner(file);
System.out.println("Enter a chemical compound: ");
String compound = kbd.nextLine();
compound = compound+" ";
System.out.println("Chemical compound of "+compound);
for (int x=0; x<compound.length();x++){
if (compound.charAt(x)=='H'){
String Hvalue = String.valueOf(compound.charAt(x+1));
if (Character.isLetter(compound.charAt(x+1))) {
Hvalue = String.valueOf(1);
} else if (Character.isWhitespace(compound.charAt(x+1))){
Hvalue = String.valueOf(1);
}
System.out.println(Hvalue+" part/s Hydrogen");
} else if (compound.charAt(x)=='C'){
String Cvalue = String.valueOf(compound.charAt(x+1));
if (Character.isLetter(compound.charAt(x+1))) {
Cvalue = String.valueOf(1);
} else if (Character.isWhitespace(compound.charAt(x+1))){
Cvalue = String.valueOf(1);
}
System.out.println(Cvalue+" part/s Carbon");
}
} else if (Character.isWhitespace(compound.charAt(x+1))){
Ovalue = String.valueOf(1);
}
System.out.println(Ovalue+" part/s Oxygen");
}
}
}
}
java text
java text
edited Nov 19 at 17:22
asked Nov 19 at 11:44
Joey Deguzman
265
265
1
Basic I/O
– Kevin Anderson
Nov 19 at 11:58
add a comment |
1
Basic I/O
– Kevin Anderson
Nov 19 at 11:58
1
1
Basic I/O
– Kevin Anderson
Nov 19 at 11:58
Basic I/O
– Kevin Anderson
Nov 19 at 11:58
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
Assuming the format of the file will remain constant, below should work (based on JDK 8):
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Element {
public static void main(String args) throws IOException {
Scanner kbd = new Scanner(System.in);
Map<String, Map<String, Double>> elementsMap = new HashMap<>();
File file = new File("elements.txt");
Scanner read = new Scanner(file);
while (read.hasNext()) {
elementsMap.putIfAbsent(read.next(), Collections.singletonMap(read.next(), Double.valueOf(read.next())));
}
System.out.println("Enter a chemical compound: ");
String compound = kbd.nextLine();
compound = compound + " ";
System.out.println("Chemical compound of " + compound);
double compoundWeight = 0.0;
for (int x = 0; x < compound.length(); x++) {
String elementKey = String.valueOf(compound.charAt(x));
if (elementsMap.containsKey(elementKey)) {
Map<String, Double> element = elementsMap.get(elementKey);
int noOfElements = 0;
while (String.valueOf(compound.charAt(x + 1)).matches("([1-9])")) {
noOfElements = ((noOfElements * 10) + Integer.parseInt(String.valueOf(compound.charAt(x + 1))));
x++;
}
noOfElements = noOfElements > 0 ? noOfElements : 1;
System.out.println(noOfElements + " part/s " + element.keySet().iterator().next());
compoundWeight += (element.values().iterator().next() * noOfElements);
}
}
System.out.println("Atomic mass of " + compound + " is : " + compoundWeight);
}
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Assuming the format of the file will remain constant, below should work (based on JDK 8):
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Element {
public static void main(String args) throws IOException {
Scanner kbd = new Scanner(System.in);
Map<String, Map<String, Double>> elementsMap = new HashMap<>();
File file = new File("elements.txt");
Scanner read = new Scanner(file);
while (read.hasNext()) {
elementsMap.putIfAbsent(read.next(), Collections.singletonMap(read.next(), Double.valueOf(read.next())));
}
System.out.println("Enter a chemical compound: ");
String compound = kbd.nextLine();
compound = compound + " ";
System.out.println("Chemical compound of " + compound);
double compoundWeight = 0.0;
for (int x = 0; x < compound.length(); x++) {
String elementKey = String.valueOf(compound.charAt(x));
if (elementsMap.containsKey(elementKey)) {
Map<String, Double> element = elementsMap.get(elementKey);
int noOfElements = 0;
while (String.valueOf(compound.charAt(x + 1)).matches("([1-9])")) {
noOfElements = ((noOfElements * 10) + Integer.parseInt(String.valueOf(compound.charAt(x + 1))));
x++;
}
noOfElements = noOfElements > 0 ? noOfElements : 1;
System.out.println(noOfElements + " part/s " + element.keySet().iterator().next());
compoundWeight += (element.values().iterator().next() * noOfElements);
}
}
System.out.println("Atomic mass of " + compound + " is : " + compoundWeight);
}
}
add a comment |
up vote
0
down vote
accepted
Assuming the format of the file will remain constant, below should work (based on JDK 8):
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Element {
public static void main(String args) throws IOException {
Scanner kbd = new Scanner(System.in);
Map<String, Map<String, Double>> elementsMap = new HashMap<>();
File file = new File("elements.txt");
Scanner read = new Scanner(file);
while (read.hasNext()) {
elementsMap.putIfAbsent(read.next(), Collections.singletonMap(read.next(), Double.valueOf(read.next())));
}
System.out.println("Enter a chemical compound: ");
String compound = kbd.nextLine();
compound = compound + " ";
System.out.println("Chemical compound of " + compound);
double compoundWeight = 0.0;
for (int x = 0; x < compound.length(); x++) {
String elementKey = String.valueOf(compound.charAt(x));
if (elementsMap.containsKey(elementKey)) {
Map<String, Double> element = elementsMap.get(elementKey);
int noOfElements = 0;
while (String.valueOf(compound.charAt(x + 1)).matches("([1-9])")) {
noOfElements = ((noOfElements * 10) + Integer.parseInt(String.valueOf(compound.charAt(x + 1))));
x++;
}
noOfElements = noOfElements > 0 ? noOfElements : 1;
System.out.println(noOfElements + " part/s " + element.keySet().iterator().next());
compoundWeight += (element.values().iterator().next() * noOfElements);
}
}
System.out.println("Atomic mass of " + compound + " is : " + compoundWeight);
}
}
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Assuming the format of the file will remain constant, below should work (based on JDK 8):
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Element {
public static void main(String args) throws IOException {
Scanner kbd = new Scanner(System.in);
Map<String, Map<String, Double>> elementsMap = new HashMap<>();
File file = new File("elements.txt");
Scanner read = new Scanner(file);
while (read.hasNext()) {
elementsMap.putIfAbsent(read.next(), Collections.singletonMap(read.next(), Double.valueOf(read.next())));
}
System.out.println("Enter a chemical compound: ");
String compound = kbd.nextLine();
compound = compound + " ";
System.out.println("Chemical compound of " + compound);
double compoundWeight = 0.0;
for (int x = 0; x < compound.length(); x++) {
String elementKey = String.valueOf(compound.charAt(x));
if (elementsMap.containsKey(elementKey)) {
Map<String, Double> element = elementsMap.get(elementKey);
int noOfElements = 0;
while (String.valueOf(compound.charAt(x + 1)).matches("([1-9])")) {
noOfElements = ((noOfElements * 10) + Integer.parseInt(String.valueOf(compound.charAt(x + 1))));
x++;
}
noOfElements = noOfElements > 0 ? noOfElements : 1;
System.out.println(noOfElements + " part/s " + element.keySet().iterator().next());
compoundWeight += (element.values().iterator().next() * noOfElements);
}
}
System.out.println("Atomic mass of " + compound + " is : " + compoundWeight);
}
}
Assuming the format of the file will remain constant, below should work (based on JDK 8):
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Element {
public static void main(String args) throws IOException {
Scanner kbd = new Scanner(System.in);
Map<String, Map<String, Double>> elementsMap = new HashMap<>();
File file = new File("elements.txt");
Scanner read = new Scanner(file);
while (read.hasNext()) {
elementsMap.putIfAbsent(read.next(), Collections.singletonMap(read.next(), Double.valueOf(read.next())));
}
System.out.println("Enter a chemical compound: ");
String compound = kbd.nextLine();
compound = compound + " ";
System.out.println("Chemical compound of " + compound);
double compoundWeight = 0.0;
for (int x = 0; x < compound.length(); x++) {
String elementKey = String.valueOf(compound.charAt(x));
if (elementsMap.containsKey(elementKey)) {
Map<String, Double> element = elementsMap.get(elementKey);
int noOfElements = 0;
while (String.valueOf(compound.charAt(x + 1)).matches("([1-9])")) {
noOfElements = ((noOfElements * 10) + Integer.parseInt(String.valueOf(compound.charAt(x + 1))));
x++;
}
noOfElements = noOfElements > 0 ? noOfElements : 1;
System.out.println(noOfElements + " part/s " + element.keySet().iterator().next());
compoundWeight += (element.values().iterator().next() * noOfElements);
}
}
System.out.println("Atomic mass of " + compound + " is : " + compoundWeight);
}
}
answered Nov 19 at 14:54
user3392782
743
743
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373933%2fhow-would-i-use-hashmap%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Basic I/O
– Kevin Anderson
Nov 19 at 11:58