6

So, I'm new to programming, and I was trying to make a basic mole (chemistry) calculator just for fun. I didn't find this question. If it was answered please send me the link.

This is the formula: n = N / Na where n = mole and Na = 6.022E23

The first part of the code throws an error. Just trying to get one, dividing Na by my given N, and even with 6.022 instead of 6.022E23 I'm getting 1000.0 as an answer.

Scanner in = new Scanner(System.in);
double Na = 6.022;

System.out.print("What do you want to know? Mol(0) or N(1)? ");
int first = in.nextInt();
if (first == 0){
    System.out.print("Insert N: ");
    double N = in.nextDouble();
    double mol = N/Na;
    System.out.print("There are " + mol + " mol in that sample.");
}
else if (first == 1){
    System.out.print("Insert mol: ");
    double mol = in.nextDouble();
    double N = mol*Na;
    System.out.print("There are " + N + " molecules, atoms or ions in that sample.");   
}

Output 0:

What do you want to know? Mol(0) or N(1)? 0  
Insert N: 6.022  
There are 1000.0 mol in that sample.  

Output 1:

What do you want to know? Mol(0) or N(1)? 1  
Insert mol: 1  
There are 6.022 molecules, atoms or ions in that sample.  

Thanks in advance.

OGHaza
  • 4,795
  • 7
  • 23
  • 29
EnderEgg
  • 315
  • 1
  • 5
  • 13
  • 6
    Looks like a `Locale` issue. What `Locale` are you using? – Keppil Feb 22 '14 at 10:27
  • 1
    @Keppil that's what I would expect. In the American system a dot is interpreted as a separator for thousands, not as the decimal dot. – Vincent van der Weele Feb 22 '14 at 10:31
  • @Keppil I don't know. New to this, never heard that term. I think I understand what you mean now. No, that ins't a problem. I can work with dots and commas, all the same. but 1000 isn't 1 anywhere. – EnderEgg Feb 22 '14 at 10:32
  • 1
    http://stackoverflow.com/questions/5929120/nextdouble-doesnt-allow – Bumptious Q Bangwhistle Feb 22 '14 at 10:34
  • @Bumptious Q Bangwhistle Thanks! It works now :D – EnderEgg Feb 22 '14 at 10:38
  • 2
    @Heuster: *"In the American system a dot is interpreted as a separator for thousands, not as the decimal dot."* No, it isn't, at least not in the United States. `.` is a decimal separator, `,` is a thousands separator. I don't know what they do in Central or South America. – T.J. Crowder Feb 22 '14 at 10:57
  • @T.J.Crowder you're right, it's actually the other way around... wasn't awake yet, I guess ;-) – Vincent van der Weele Feb 22 '14 at 11:29

1 Answers1

5

Since you are writing a small code, the simplistic suggestion to a new coder I could make is hard code it so it is:

double mol = N/6.022;

this gives you what you want. The error is because it is seeing it as 6022 instead of 6.022 since it is recognizing the . as a thousands indicator. Hope this helps.

If you do not want to hardcode the value, add:

in.useLocale(Locale.US);

right below where you declare your scanner. Both of these solutions should fix your problem.

Lain
  • 2,166
  • 4
  • 23
  • 47
  • Using Bumptious Q Bangwhistle link, and adding in.useLocale(Locale.US); solved the problem. I wanted to keep the Na to future references though. Thanks for the time. Indeed. That edit is answering everything! – EnderEgg Feb 22 '14 at 10:39