Remove the last digit from a String - java

I'm creating a Java calculator (only buttons - like T9 keys) in an Android XML Application, and I want to fix the "Cancel" button.
When I click it, I want to cancel the last digit from a String that contains only numbers (or '.', because you can also add '.' to write decimal numbers).
There is a problem: if the user writes an int number (34) and I convert it to double, it becomes 34.0 and, if I cancel the last digit, it cancels the 0.
I also tried with String.substring(start,end), but it doesn't work...
Have you got any suggestions about the cancel handle? Thank you!
This is the function that cancels the last digit.
/* tv -> TextView
- tvResult contains what the user writes and the result when he clicks "=", - tvCalc contains the calc that the user is entering.
For example, if the user writes with the 0-9 buttons and the operator of the operations, in tvCalc there will be "34+50=", and in tvResult "84" */
public void handleCancel(View v) {
//tv -> TextView, tvResult contains what the user writes and the result when he clicks "=", tvCalc contains the calc that the user is entering: for example if user writes with the 0-9 buttons and the operator of the operations, in tvCalc there will be "34+50=", and in tvResult "84"
if (tvResult.length() == 0) {
errorToast = Toast.makeText(MainActivity.this, "Error! There's nothing to cancel!", Toast.LENGTH_LONG);
errorToast.show();
} else {
tvResult.setText(tvResult.toString().substring(0, tvResult.toString().length() - 1))
if (tvCalc.toString().contains("=")) {
tvCalc.setText(tvResult.toString());
operand1 = tvResult.toString();
} else {
tvCalc.setText(tvCalc.toString().substring(0, tvCalc.toString().length() - 1));
if (operator == "") operand1 = tvResult.toString();
else {
operand2 = tvResult.toString();
try {
int conv = Integer.parseInt(tvCalcolo.toString());
operazione = "";
} catch(Exception e) {}
}
}
}
}
The function that adds the choice of the user:
public void userChoice(View v)
{
Button clicked=(Button)findViewById(v.getId());
String choice=clicked.getText().toString();
try
{
int number=Integer.parseInt(choice);
if (operator=="")
{
operand1+=choice;
if (tvCalc.toString().contains("=")) tvCalc.setText(operand1);
} else operand2+=choice;
} catch (Exception e)
{
if (choice.equals('.'))
{
if (operator=="") operand1+=choice;
else operand2+=choice;
} else
{
if (operand2!="")
{
handleEqual(v);
operand1=tvResult.toString();
}
operator=choice;
tvCalc.append(operator);
tvResult.setText("");
return;
}
}
tvCalc.append(choice);
tvResult.append(choice);
}
I edited the function "handleCancel" thanks to Cardinal System:
public void handleCancel (View v)
{
double d;
try
{
d=Double.parseDouble(tvRisultato.toString());
} catch (Exception e)
{
bCanc.setText("Error"); //It's the button 'Cancel'
}
String newVal;
if (d%1==0)
{
int val=(int)d;
newVal=String.valueOf(val);
newVal=newVal.substring(0,newVal.length()-1);
} else
{
newVal=String.valueOf(d);
newVal=newVal.substring(0,newVal.length()-1);
if (newVal.endsWith("."))
{
newVal=newVal.substring(0,newVal.length()-1);
}
}
tvResult.setText(newVal);
if ((tvCalc.toString().contains("+")||tvCalc.toString().contains("-")||tvCalc.toString().contains("*")||tvCalc.toString().contains("/")||tvCalc.toString().contains("^"))&&tvCalc.toString().contains("="))
{
tvCalc.setText(tvResult.toString());
operand1=tvResult.toString();
} else if ((tvCalc.toString().contains("+")||tvCalc.toString().contains("-")||tvCalc.toString().contains("*")||tvCalc.toString().contains("/")||tvCalc.toString().contains("^"))&&!tvCalc.toString().contains("="))
{
if (tvCalc.toString().endsWith("+")||tvCalc.toString().endsWith("-")||tvCalc.toString().endsWith("*")||tvCalc.toString().endsWith("/")||tvCalc.toString().endsWith("^")) operator="";
tvCalc.setText(tvCalc.toString().substring(0,tvCalc.toString().length()-1));
}
}

See if this helps you:
// Let's make it a double so we have something to work with.
double d = Double.parseDouble(tvResult.getText().toString());
String newText;
if (d % 1 == 0) { // See if the number is a whole number
int i = (int) d; // If it is, cast it to an int to get rid of the decimal
newText = String.valueOf(i); // Parse it to a string so we can clip off the end
newText = newText.substring(0, newText.length() - 1); // Clip off the end
} else {
// If it's not a whole number, just parse it to a string.
newText = String.valueOf(d);
newText = newText.substring(0, newText.length() - 1); // Clip off the end
if (newText.endsWith(".")) {
// If the number we clipped off was a tenth, clip off the decimal
newText = newText.substring(0, newText.length() - 1);
}
}
tvResult.setText(newText);

Looks like you could fulfill your requirements at least in two ways:
pattern searching symbols to be removed (something like (.0|\d|+|-|*|/) ) - would not recommend
remembering last input string before new digit/operation is added. F.e. on button . pressed you would remember your current input to calculator to variable and in case you need to cancel, you simply set current input line to that variable
Hope my suggestions were helpfull.

Related

how to handle java null values

Im developing a small app and I wants to get the total when I click the button total . But if there are null values the code code dosen't work .So additionally following code was added .
int QtyOfChickenBurger;
if ((textField.getText().equals(null))) {
QtyOfChickenBurger = Integer.parseInt(textField.getText())*0;
} else {
QtyOfChickenBurger = Integer.parseInt(textField.getText()) * 70;
}
But still my application don't output the total when the textField is empty. So please help me to fix this.This is the full code.
JButton bttotal = new JButton("Total");
bttotal.setBounds(21, 37, 145, 25);
bttotal.setFont(new Font("Thoma", Font.PLAIN, 12));
bttotal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg) {
int QtyOfChickenBurger;
if ((textField.getText().equals(null))) {
QtyOfChickenBurger = Integer.parseInt(textField.getText())*0;
} else {
QtyOfChickenBurger = Integer.parseInt(textField.getText()) * 70;
}
int QtyOfChickenBurgerMeal = Integer.parseInt(textField_2.getText()) * 120;
int QtyOfCheeseBurger = Integer.parseInt(textField_3.getText()) * 340;
int QtyOfDrinks = Integer.parseInt(enterQTY.getText());
int spriteCost = Integer.parseInt(enterQTY.getText()) * 55;
int cokaColaCost = Integer.parseInt(enterQTY.getText()) * 60;
int pepsiCost = Integer.parseInt(enterQTY.getText()) * 40;
int lemonJuceCost = Integer.parseInt(enterQTY.getText()) * 35;
int sum = (QtyOfChickenBurger + QtyOfChickenBurgerMeal + QtyOfCheeseBurger);
lblDisplayCostOfMeals.setText(Integer.toString(sum));
String x = String.valueOf(comboBox.getSelectedItem());
if (x == "Sprite") {
lblDisplayCostOfDrinks.setText(Integer.toString(spriteCost));
} else if (x == "Select the drink") {
lblDisplayCostOfDrinks.setText("0");
} else if (x == "Coka Cola") {
lblDisplayCostOfDrinks.setText(Integer.toString(cokaColaCost));
} else if (x == "Pepsi") {
lblDisplayCostOfDrinks.setText(Integer.toString(pepsiCost));
} else if (x == "Lemon juce") {
lblDisplayCostOfDrinks.setText(Integer.toString(lemonJuceCost));
}
}
});
This code is wrong
if ((textField.getText().equals(null))) {
QtyOfChickenBurger = Integer.parseInt(textField.getText())*0;
}
if the textField.getText() is null then you can not parse the null into Integer. In addition, you multiply 0 with a number for what? => just set it to 0.
Change it to
if (textField.getText()==null || textField.getText().equals("")){
QtyOfChickenBurger = 0;
}
I presume you are using Swing JTextField which is a JTextComponent.
getText is returning a String therefore, you will need to check if its null or empty, before parsing it to int later on.
There are few ways of checking it, !textField.getText().isEmpty() and textField.getText() != null
Personally, i would use Commons Lang library StringUtils and just check if the string isBlank(textField.getText())
Additionally you should validate the input, as well You can use the mentioned library and use StringUtils.isNumeric() in your if statement.
First of all textField.getText().equals(null) will never work this only throws a NullPointerException better use textField.getText()==null.
Because the user can enter anything in the TextField you need to validate the input or create a try-catch-block.
The best way would to create a help method to parse the numbers, for example:
public static int readInteger(String text, int defaultValue) {
if(text == null)
return defaultValue;
try {
return Integer.parseInt(text);
} catch (NumberFormatException nfe) {
return defaultValue;
}
}
By the way don't compare Strings with x == "Pepsi" only when you want to check if the String is null.
Read: How do I compare strings in Java?
I am assuming that user can write what he want in that textfield so u should also take care when textfield have a value like 'asd';
String QtyOfChickenBurgerAsString =textField.getText();
Integer QtyOfChickenBurger=null;
try{
QtyOfChickenBurger = Integer.valueOf(QtyOfChickenBurgerAsString);
}catch(Exception ex){
System.out.println(" this is not a number ...do whatever u wanna do");
}
if (QtyOfChickenBurger!=null){
System.out.println("integer value of field text "+QtyOfChickenBurger);
}
I suggest create variables with starting letter in lower case ,and when you compare using equals use the constant first.Also you could find a better solution by using components which accept only numbers in their text.
As of java 9, the Object class has a convenience method for handling null values and it is found in java.util.
public static String processEmployee(String fullName){
// if fullName is null then second argument is returned,otherwise fullName is
String name = Objects.requireNonNullElse(fullName, "empty");
// the processing goes here ...
return name;
}

adding two decimals together giving truncated result when any value other than first value is not a whole number

I am attempting to create a calculator app, when I try to add 2 decimals together, I do not get the results I anticipated.
examples:
user inputs [1.1 + 1.1] and [1.1 + 1] or 2.1 is returned.
It is as if as if second value is being truncated, I am not entirely sure where this would be occurring
user inputs[2.1 + 2] and [2.1 + 2] or 4.1 is returned
user inputs [2 + 2.1] and [2 + 2] or 4 is returned
I have not used the debugger, though I have placed print line statements through out the code and I have not been able to see the issue.
Code:
information
I only attached a single number (image button) as all numbers 1-9 have identical code
variables that are not instantiated in code below have been instantiated as "" if they are a string and 0.0 if they are a double
I am using an array list called values which takes in strings, and using double.parsedouble() to convert it to a double, this occurs in the equals method
please let me know if their is anything I need to add to make this question easier to understand, and any help is appreciated
thank you!
1 button
one_button_ET.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (plus_selected) {
update_selected_buttons(); //for boolean variables and to change mathematical function button image
screen_value = "";
updateScreen("1");
System.out.println(screen_value);
values.add(screen_value);
} else {
updateScreen("1");
System.out.println(screen_value);
}
}
});
addition button
addition_button_ET.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!plus_selected) {
plus_selected = true;
addition_button_ET.setImageResource(R.drawable.addition_selected_500_500);
if(Double.parseDouble(screen_value) == current_value){
}
else
values.add(screen_value);//
} else {
plus_selected = false;
addition_button_ET.setImageResource(R.drawable.addition_button_500_500);
}
}
});
update screen
private void updateScreen(String value) { //being used for only one_button
//to reset title text
if (screen_value.equals(initial_screen_value)) {
screen_value = "0.0";
screen_TV.setTextSize(50);
}
//clears default value (0.0), allowing user to enter in a number
if (screen_value.equals("0.0")) {
screen_value = value;
screen_TV.setText(screen_value);
} else {
screen_value += value;
System.out.println(screen_value);
screen_TV.setText(screen_value);
}
}
clear button (clear button calls this method, and that is the only parameter it has)
private void resetScreen() {
screen_value = "0.0";
//values.clear();
screen_TV.setTextSize(50);
screen_TV.setText(screen_value);
}
equals button
equals_button_ET.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
current_value = 0;
sum = 0;
System.out.println(values);
for(int i = 0; i<values.size();i++) {
sum += Double.parseDouble(values.get(i));
}
current_value = sum;
screen_value = "" + sum;
screen_TV.setText(screen_value);
}
});

Using a running total for calculator

I made a calculator on android. I get results for only two numbers (1+1), but I would like to be able to do more then one calculation at a time instead of hitting enter every time I need a new answer (1+1-2).
Something like this: 2 + 2 (new operator pressed) 4 + 3 (equals is pressed) 7
But I'm not sure how to implement this on my code.
This is the button code for every operator:
btnPlus.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
perform();
operation = "+";
}
}); // end btnPlus
This is my calculation method:
private void calculate() {
if (operation == null) {
numberInput.setText(null);
} else if (operation.equals("+")){
numResult = (secondNum + firstNum);
numberInput.setText(String.valueOf(numResult));
} else if (operation.equals("-")) {
numResult = (secondNum - firstNum);
numberInput.setText(String.valueOf(numResult));
} else if (operation.equals("/")) {
numResult = (secondNum / firstNum);
numberInput.setText(String.valueOf(numResult));
} else if (operation.equals("*")) {
numResult = (secondNum * firstNum);
numberInput.setText(String.valueOf(numResult));
} // end if statement
}
Also, perform if needed:
private void perform() {
str = "";
secondNum = firstNum;
}
A quick way to solve your problem given your current code is to use textChangeListener/TextWatcher(assuming you have an EditText or TextView) to detect every time an operator is entered then call the operate function. It would look something like this.
myEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
// if 2nd operator is detected
// call calculate
// append 2nd operator
}
});
You could do as i suggested in the comment, and just make a list for every operation, and then calculate it every time the current data changes (there may be difffrent ways to "know" if some of the result is final depending on how you can delete from the app.
SOmething like this is what i would do, and then just everytime a button is pressed call the logic for adding the input, and then calculate it and write it out
public class Calc {
private List<String> inputs = new ArrayList<>();
public String calculate(List<String> input){
String result = "";
for(String current : input){
switch (current) {
case "(":
// do stuff
case "+":
//do stuff
default:
// do stuff
}
}
return result;
}
btnPlus.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// add to list, and call calculate
}
}); // end btnPlus
}

Android - How to only allow a certain number of decimal places

Do you know of any method to make sure users can only enter figures with a maximum number of decimals.
I'm not sure how to address this problem. In the MS SQL database I'm going to send data from my app I've got columns with this type decimal(8,3)
Now considering the data type of the column that's finally going to store the value I want to validate in Android, I've considered these two cases:
If the user enters a number with no decimals, the maximum number of digits must be 8
If the user enters a number with decimals, the maximum number of digits must be 8 (including the digits to the right of the decimal point)
Now I'm sure about the first case, but not so much about the second. Is it right to keep the number of maximum digits fixed(for example, always 8)? Or should I consider allowing a maximum of 8 digits to the left and 3 to the right of the decimal point?
Either way this is what I've been trying in Android:
mQuantityEditText.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
String str = mQuantityEditText.getText().toString();
DecimalFormat format = (DecimalFormat) DecimalFormat
.getInstance();
DecimalFormatSymbols symbols = format.getDecimalFormatSymbols();
char sep = symbols.getDecimalSeparator();
int indexOFdec = str.indexOf(sep);
if (indexOFdec >= 0) {
if (str.substring(indexOFdec, str.length() - 1).length() > 3) {
s.replace(0, s.length(),
str.substring(0, str.length() - 1));
}
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
});
Even though, the above code handles the maximum number of decimal places. It does not limit the total number of digits allowed in the EditText.
Do you think you could help me improve my code so that it handles both the maximum number of decimal places and the total number of digits allowed in a EditText (considering both numbers to the left and to the right of the decimal point)
EDIT
Well, now I'm trying what João Sousa suggested and here's what I've tried:
1) I defined a class that implements InputFilter
public class NumberInputFilter implements InputFilter {
private Pattern mPattern;
public NumberInputFilter(int precision, int scale) {
String pattern="^\\-?(\\d{0," + (precision-scale) + "}|\\d{0," + (precision-scale) + "}\\.\\d{0," + scale + "})$";
this.mPattern=Pattern.compile(pattern);
}
#Override
public CharSequence filter(CharSequence source, int start, int end, Spanned destination, int destinationStart, int destinationEnd) {
if (end > start) {
// adding: filter
// build the resulting text
String destinationString = destination.toString();
String resultingTxt = destinationString.substring(0, destinationStart) + source.subSequence(start, end) + destinationString.substring(destinationEnd);
// return null to accept the input or empty to reject it
return resultingTxt.matches(this.mPattern.toString()) ? null : "";
}
// removing: always accept
return null;
}
}
2) Tried to use the class like this :
mQuantityEditText.setFilters(new InputFilter[] { new NumberInputFilter(8,3)} );
I would go for a filter in the edit text itself with the power of regex. First the regex expression:
^\-?(\d{0,5}|\d{0,5}\.\d{0,3})$
Maybe there are multiple ways to improve this expression, but this does trick.
And now just set an input filter in the edittext, like this:
final String regex = "^\-?(\d{0,5}|\d{0,5}\.\d{0,3})$";
((EditText)rootView.findViewById(R.id.editText1)).setFilters(new InputFilter[] {
new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start, int end, Spanned destination, int destinationStart, int destinationEnd) {
if (end > start) {
// adding: filter
// build the resulting text
String destinationString = destination.toString();
String resultingTxt = destinationString.substring(0, destinationStart) + source.subSequence(start, end) + destinationString.substring(destinationEnd);
// return null to accept the input or empty to reject it
return resultingTxt.matches(regex) ? null : "";
}
// removing: always accept
return null;
}
}
});
Btw, I just tested this code and what it does is:
The user can enter a maximum of 8 digits;
As soon as the user enters a '.', the maximum decimal digits allowed are 8.
Did I correctly understand the problem you described?
-- EDIT
Ok, I was almost there. From what I understand, decimal(8,3) means at most 8 digits including digits to the left or right of the decimal point, ranging from -99999.999 to 99999.999.
At least that's what I understand from this sentence Standard SQL requires that DECIMAL(5,2) be able to store any value with five digits and two decimals, so values that can be stored in the salary column range from -999.99 to 999.99. Even though it's from the MySQL documentation the MSSQL docs seem to do the same.
I have answser for you, me also suffered lot in this kind of situation.:D :P
I have implemented this for maximum of 4 digits to the left and 2 to the right of the decimal point ex: 4444.99
so small changes need to implement what i did:
Need to do following changes
1) copy CustomTextWatcher.java to track input of editText.
import java.text.NumberFormat;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
public class CustomTextWatcher implements TextWatcher {
private NumberFormat nf = NumberFormat.getNumberInstance();
private EditText et;
private String tmp = "";
private int moveCaretTo;
private static final int INTEGER_CONSTRAINT = 4;
private static final int FRACTION_CONSTRAINT = 2;
private static final int MAX_LENGTH = INTEGER_CONSTRAINT
+ FRACTION_CONSTRAINT + 1;
public CustomTextWatcher(EditText et) {
this.et = et;
nf.setMaximumIntegerDigits(INTEGER_CONSTRAINT);
nf.setMaximumFractionDigits(FRACTION_CONSTRAINT);
nf.setGroupingUsed(false);
}
public int countOccurrences(String str, char c) {
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == c) {
count++;
}
}
return count;
}
#Override
public void afterTextChanged(Editable s) {
et.removeTextChangedListener(this); // remove to prevent stackoverflow
String ss = s.toString();
int len = ss.length();
int dots = countOccurrences(ss, '.');
boolean shouldParse = dots <= 1
&& (dots == 0 ? len != (INTEGER_CONSTRAINT + 1)
: len < (MAX_LENGTH + 1));
if (shouldParse) {
if (len > 1 && ss.lastIndexOf(".") != len - 1) {
try {
if (ss.contains(".")) {
String[] integerFractionStrings = ss.split("\\.");
Log.v("Check SS ", ss);
Log.v("second string", "Found"
+ integerFractionStrings.length);
if (integerFractionStrings.length > 1) {
Log.v("integerFractionStrings",
integerFractionStrings[1]);
if (integerFractionStrings[1].length() == 1
&& integerFractionStrings[1].charAt(0) == '0') {
et.setText(ss);
Log.v("second string", "size 1");
} else {
Log.v("second string", "> 1");
Double d = Double.parseDouble(ss);
if (d != null) {
et.setText(nf.format(d));
}
}
}
} else {
Log.v("First string", "No dot");
Double d = Double.parseDouble(ss);
if (d != null) {
et.setText(nf.format(d));
}
}
} catch (NumberFormatException e) {
}
}
} else {
Log.v("second string", "size 1");
et.setText(tmp);
}
et.addTextChangedListener(this); // reset listener
// tried to fix caret positioning after key type:
if (et.getText().toString().length() > 0) {
if (dots == 0 && len >= INTEGER_CONSTRAINT
&& moveCaretTo > INTEGER_CONSTRAINT) {
moveCaretTo = INTEGER_CONSTRAINT;
} else if (dots > 0 && len >= (MAX_LENGTH)
&& moveCaretTo > (MAX_LENGTH)) {
moveCaretTo = MAX_LENGTH;
}
try {
et.setSelection(et.getText().toString().length());
// et.setSelection(moveCaretTo); <- almost had it :))
} catch (Exception e) {
}
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
moveCaretTo = et.getSelectionEnd();
tmp = s.toString();
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int length = et.getText().toString().length();
if (length > 0) {
moveCaretTo = start + count - before;
}
}
}
2) set this class to check your editText by following.
EditText review_food_Price;
review_food_Price = (EditText) findViewById(R.id.food_Price);
review_food_Price.setRawInputType(InputType.TYPE_CLASS_NUMBER
| InputType.TYPE_NUMBER_FLAG_DECIMAL);
review_food_Price.addTextChangedListener(new CustomTextWatcher(
review_food_Price));
Hope you can convert my code according to your need.
The problem that you describe is precisely what a Masked EditText is meant to be used for. :)

For loop error, how do i fix this?

for(int x = 0;x<14;x++){
day[x]= theSheet.changeLetters(day[x]);
}
public String changeLetters(String entering){
if(entering.equalsIgnoreCase("a")){
entering = "10";
} else {
if(entering.equalsIgnoreCase("b")){
entering = "11";
} else {
if(entering.equalsIgnoreCase("c")){
entering = "12";
} else {
if(entering.equalsIgnoreCase("d")){
entering = "13";
} else {
if(entering.equalsIgnoreCase("e")){
entering = "14";
} else {
if(entering.equalsIgnoreCase("f")){
entering = "15";
} else {
if(entering.equalsIgnoreCase("g")){
entering = "16";
} else {
if(entering.equalsIgnoreCase("h")){
entering = "17";
}
}
}
}
}
}
}
}
return entering;
}
Says the error is here if(entering.equalsIgnoreCase("a")) and in the for loop I am using to run the method. I'm trying to change the letter put into the string into a number.
Can anyone explain to me where the error might be? I'm having trouble spotting the issue. it lets me enter the letters fine but it has an exception once it gets to this for loop and runs this method.
why don't you use
if (condition) {
// ...
} else if (condition2) {
// ...
} else if (condition3) {
// ...
}
// and so on
to make your code more readable. Your nested conditions are a mess. If you fix them, you might as well fix the error (if it's in the part of code you showed us).
Also add
System.out.println("Entering = '" + entering "'");
at the beginnig of your method to see if really receives what you are expecting it to receive.
Ok according to
Yes the code is being initialized in a for loop before that one using
for(int x =1;x<8;x++){ day[x-1] = JOptionPane.showInputDialog("Enter
hour pairs for day "+x +".\n Enter the first digit: "); day[x] =
JOptionPane.showInputDialog("Enter the second digit: "); } the letters
being put in the array are then changed into numbers using the for
loop posted earlier.
You have a logic error. You are overwriting previous entries and not filling the array up to 14 items. So items after 8 are left null, thus the NullPointerException.
Try this:
String[] day = new String[14];
for( int i = 0; i < 14; i+=2 ) {
day[i] = JOptionPane.showInputDialog("Enter hour pairs for day "+(i/2+1) +".\n Enter the first digit: ");
day[i+1] = JOptionPane.showInputDialog("Enter the second digit: ");
}
As a bonus, you can simplify the if-else code with:
public String changeLetters( String entering ) {
return String.valueOf(entering.toUpperCase().charAt(0) - 55);
}
As #jlordo already stated avoid nested if-statements with such a depth.
As an alternative you might use a switch statement in combination with an enum like this - although the following approach consists of more code it is more general and better suitable to be extended (e.g. using all letter of the alphabet and beyond) and it would make the code much more readable and more appropriate with respect to what you want to express.
Letter letter;
if (letter.equals("a")) letter = Letter.a;
if (letter.equals("A")) letter = Letter.A;
// and so on ...
switch (letter) {
case a : {
// do some code here ...
break;
}
case A : {
// do some code here ...
break;
}
// and so on ...
}
public enum Letter {
a (1),
A (2),
b (3),
B (4),
c (5),
C (6);
// and so on
private final int index;
Letter(int i) {
index = i;
}
public int getIndex () {
return index;
}
}
Note that if you're using Java 7 you can use the switch statement even without the enum since it accepts strings as well like this:
switch (entering) {
case "a" : {
// ...
}
// ...
}

Categories