Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How do I read numbers in exponential format in Java?

0
Posted

How do I read numbers in exponential format in Java?

0

[*] The program below (written by Steve Chapel) uses StreamTokenizer to read data from the standard input and recognizes doubles in exponential format (e.g. -1.23e-45). import java.io.*; public class ReadExponential { public static void main(String argv[]) { DataInputStream in = new DataInputStream(System.in); StreamTokenizer st = new StreamTokenizer(in); try { while (st.nextToken() != StreamTokenizer.TT_EOF) { switch (st.ttype) { case StreamTokenizer.TT_NUMBER: double num = st.nval; int exp = 0; st.ordinaryChars(‘\0’, ‘ ‘); st.nextToken(); st.whitespaceChars(‘\0’, ‘ ‘); if (st.ttype == StreamTokenizer.TT_WORD && Character.toUpperCase(st.sval.charAt(0)) == ‘E’) { try { exp = Integer.parseInt(st.sval.substring(1)); } catch (NumberFormatException e) { st.pushBack(); } } else if (st.ttype < 0 || st.ttype > ‘ ‘) st.pushBack(); System.out.println(“Num ” + num * Math.pow(10, exp)); break; case StreamTokenizer.TT_WORD: System.out.println(“Word ” + st.sval); break; default: System.out.println(

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123