/*
* Class Db
*
* Methods for entering doubles, floats, integers,
* long integers, strings, chars, Complexes and Phasors
* from the key board via the standard console window
*
* WRITTEN BY: Dr Michael Thomas Flanagan
*
* DATE: 19 June 2003
* MODIFIED: 25 June 2005, 27 June 2007, 21/22 July 2007, 10 September 2010, 31 Ocober 2010
*
* DOCUMENTATION:
* See Michael Thomas Flanagan's Java library on-line web pages:
* http://www.ee.ucl.ac.uk/~mflanaga/java/Db.html
* http://www.ee.ucl.ac.uk/~mflanaga/java/
*
* Copyright (c) 2003 - 2010 Michael Thomas Flanagan
*
* PERMISSION TO COPY:
*
* Permission to use, copy and modify this software and its documentation for NON-COMMERCIAL purposes is granted, without fee,
* provided that an acknowledgement to the author, Dr Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all copies
* and associated documentation or publications.
*
* Redistributions of the source code of this source code, or parts of the source codes, must retain the above copyright notice, this list of conditions
* and the following disclaimer and requires written permission from the Michael Thomas Flanagan:
*
* Redistribution in binary form of all or parts of this class must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution and requires written permission from the Michael Thomas Flanagan:
*
* Dr Michael Thomas Flanagan makes no representations about the suitability or fitness of the software for any or for a particular purpose.
* Dr Michael Thomas Flanagan shall not be liable for any damages suffered as a result of using, modifying or distributing this software
* or its derivatives.
*
***************************************************************************************/
package flanagan.io;
import java.io.*;
import java.math.*;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import flanagan.analysis.ErrorProp;
import flanagan.complex.Complex;
import flanagan.complex.ComplexErrorProp;
import flanagan.circuits.Phasor;
import flanagan.math.Fmath;
public class Db{
private static boolean inputTypeInfo = true; // = true: Db read input box includes a type needed message
// = false: Db read input box does not includes a type needed message
// Set input type info option
public static void setTypeInfoOption(int opt){
switch(opt){
case 1: Db.inputTypeInfo = true;
break;
case 2: Db.inputTypeInfo = false;
break;
default: throw new IllegalArgumentException("Option " + opt + " not recognised");
}
}
// Reads a double from a dialog box with a prompt message
// No default option
public static final synchronized double readDouble(String mess){
String line="";
double d=0.0D;
boolean finish = false;
System.out.flush();
String mess0 = "";
if(Db.inputTypeInfo)mess0 = "Input type: double\n";
while(!finish){
line = JOptionPane.showInputDialog(mess0+mess);
if(line!=null){
try{
d = Double.parseDouble(line.trim());
finish=true;
}catch(NumberFormatException e){
// Valid double not entered - dialog box recalled
}
}
}
return d;
}
// Reads a double from a dialog box with a prompt message and the return
// of a default option if the return key alone is pressed
public static final synchronized double readDouble(String mess, double dflt){
String line="";
double d=0.0D;
boolean finish = false;
System.out.flush();
String mess0 = "";
if(Db.inputTypeInfo)mess0 = "Input type: double\n";
mess = mess + "\n";
String dfltmess = dflt + "";
while(!finish){
line = JOptionPane.showInputDialog(mess0 + mess + " [default value = " + dflt + "] ", dfltmess);
if(line!=null){
if(line.equals("")){
d=dflt;
finish=true;
line=null;
}
else{
try{
d = Double.parseDouble(line.trim());
finish=true;
}catch(NumberFormatException e){
// Valid double not entered - dialog box recalled
}
}
}
}
return d;
}
// Reads a double from the dialog box
// No prompt message, No default option
public static final synchronized double readDouble(){
String line="";
String mess="Input type: double";
double d=0.0D;
boolean finish = false;
System.out.flush();
while(!finish){
line = JOptionPane.showInputDialog(mess);
if(line!=null){
try{
d = Double.parseDouble(line.trim());
finish=true;
}catch(NumberFormatException e){
// Valid double not entered - dialog box recalled
}
}
}
return d;
}
// Reads a double array from a dialog box with a prompt message
// No default option if the return key alone is pressed
public static final synchronized double[] readDoubleArray(String mess){
String line="";
double[] darray = null;
boolean finish = false;
System.out.flush();
String mess0 = "";
if(Db.inputTypeInfo)mess0 = "Input type: double[], each element separated by a comma\n";
mess = mess + "\n";
String dfltmess = " ";
int nElements = 0;
ArrayList al = new ArrayList();
while(!finish){
line = JOptionPane.showInputDialog(mess0 + mess, dfltmess);
if(line!=null){
if(line.equals("")){
line=null;
}
else{
int pos = -1;
String hold = null;
boolean test = true;
while(test){
pos = line.indexOf(',');
if(pos==-1){
hold = line.trim();
test = false;
finish=true;
}
else{
hold = (line.substring(0,pos)).trim();
line = line.substring(pos+1);
}
try{
al.add(Double.valueOf(hold));
}catch(NumberFormatException e){
// Valid double not entered - dialog box recalled
}
}
}
}
}
nElements = al.size();
darray = new double[nElements];
for(int i=0; i
Ads help maintain this website.