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");
}
}

}


}










share|improve this question




















  • 1




    Basic I/O
    – Kevin Anderson
    Nov 19 at 11:58















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");
}
}

}


}










share|improve this question




















  • 1




    Basic I/O
    – Kevin Anderson
    Nov 19 at 11:58













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");
}
}

}


}










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












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);

}
}





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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);

    }
    }





    share|improve this answer

























      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);

      }
      }





      share|improve this answer























        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);

        }
        }





        share|improve this answer












        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);

        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 14:54









        user3392782

        743




        743






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen