How to program calculator array to not include sentinel?


Task is to create a basic calculator using double value and sentinel. I cannot figure out how to get the array to avoid counting the sentinel. To me, it looks like I've put in safeguards such as only looping if the value is >=0, but it still counts, lists, and even adds the sentinel number to the final result. I've tried changing the while statement to !=-1 with no change in the results. I'm a complete Java newbie (obviously), and have tried many options, but can't find the right one. 


package arraypractice;


import java.util.ArrayList;  

import java.util.Scanner;


public class CashRegister 



```
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    {
    final int SENTINEL=-1;
    double priceFromUser=0;
    double total=0;
    double average=0;
    double count=0;
    boolean done=false;
    String trash="";
    String lists="";
    ArrayList<Double> list = new ArrayList<>();
    Scanner in = new Scanner(System.in);

    //Prompt user to enter a price, or -1 when finished entering prices.

    while (!done)
    {


    while (priceFromUser >= 0)
    {
        System.out.println("Please enter item price, or enter -1 to quit.");

        if(in.hasNextDouble())//if the input is a double
        {
            priceFromUser=in.nextDouble(); //read value from user
            list.add(priceFromUser); //add valid price to the list
        }
        else                

        {
            trash=in.nextLine(); //throw away the input 
            System.out.println("You did not enter a price.");

        }



    //add valid input to list


    //total the prices added by user, and keep track of how many were added

        {
             total += priceFromUser;
             count++;
        }

    }

    //print the array list of entries, the total cost, and the average cost.

    list.forEach(System.out::println);

    System.out.println("The total price of the items is $" + total +". The average cost per item is: $" + (total/count));
    done=true;

}
```

**Output is:** 


run-single:  

Please enter item price, or enter -1 to quit.  

25  

Please enter item price, or enter -1 to quit.  

25  

Please enter item price, or enter -1 to quit.  

25  

Please enter item price, or enter -1 to quit.  

25  

Please enter item price, or enter -1 to quit.  

-1  

25.0  

25.0  

25.0  

25.0  

-1.0  

The total price of the items is $99.0. The average cost per item is: $19.8
I will preface this by saying that I don't know Java at all. I touched upon it in school 20+ years ago, but I've been doing PHP ever since.


However, on line 27, you say that if the input passed in is a double, execute this code block *blah*.


The user enters -1.


Is -1 not a double? It is a valid double, and therefore the code block executes, setting the priceFromUser to be -1 and adding it to the list.


Then, when the loop starts back up again on line 23, priceFromUser is now -1, so the loop breaks and prints the tally.


Also, note you're definining this constant SENTINEL variable, but you're not actually using it anywhere in your code.


Perhaps do something like this instead, changing the while loop into a do-while loop:



```
System.out.println("Please enter item price to start, since there needs to be at least 1 item:");

do
{

        // Keep asking for a price until the user enters a valid price
        while (!in.hasNextDouble())               
        {
                trash=in.nextLine(); //throw away the input 
                System.out.println("You did not enter a valid price. Please try again:");
        }

        priceFromUser=in.nextDouble(); //read value from user
        list.add(priceFromUser); //add valid price to the list            

          System.out.println("Please enter another item price, or enter -1 to quit.");

} while (priceFromUser != SENTINEL)
```

Note this isn't valid code because I'm not a Java programmer, but hopefully it helps you out a bit.
Check if any of the values in the cells are null.  

If they are you need to modify your code to check for null conditions before using `ToString()`. you can use lazy evaluation so if the first expression returns true it will not check the second expression. just change the ‘or’ in your statement to ‘orelse’ so it would be:

“string.isnullorempty(message) orelse message.contains(”!")" Turns out If I tweked each method 



```
foreach (var dataColumn in readFields.Select(column => new DataColumn(columntypeof(string)) { AllowDBNull = true, DefaultValue = string.Empty }))
{
    csvData.Columns.Add(dataColumn);
}

```

and in the second Method I changed it to use an index rather than column name



```
for (var count = 0; count < table.Columns.Count; count++)
{
    sqlBulkCopy.ColumnMappings.Add(count, count);
}

``` matlab does not need a |return| statement to return a value. to return a value you simply need to list it as an output argument of the function:

 function [output1,output2,...] = fun\_name(input1,input2,...)

you have specified an output argument |rad|, but then have totally ignored this inside your code an never allocated any value to it. you should try something like this:

 function rad = radial(n,m);
 rad = ... whatever rad is;
 end

of course this is explained quite clearly in the introductory tutorials:

<http://www.mathworks.com/help/matlab/getting-started-with-matlab.html>

and also the <http://www.mathworks.com/help/matlab/ref/function.html |function|> documentation:

<http://www.mathworks.com/help/matlab/ref/function.html>

read the documentation. it tells you how matlab works.

if you want to store all of the values of |rad| in a loop then you can use indexing. Should be able to do something like this:


UPDATE table\_name  

SET new\_column = (calculated value)  

WHERE new\_column is NULL The problem is that your pair's closure must return two different types of objects: int if you ask for "con", and the next closure on the line if you ask for "crd".


Here's one possible implementation:


Use a speaker so you can come back.



```
public static Func<string, dynamic> Pair(int x, Func<string, dynamic> y)
{
    Func<string, dynamic> pair =
           (a) =>
           {
               if (a == "con") return x;
               if (a == "crd") return y;
               throw new Exception();
           };

    return pair;
}

public static void Print(Func<string, dynamic> pair)
{
    while (true)
    {
        var next = pair("crd");
        Console.WriteLine((next == null ? "-":"+") +"> " + pair("con"));
        if (next != null)
        {
            Console.WriteLine("|");
            pair = (x) => next(x);
            continue;
        }
        break;
    }
}

        

        
        
        
 (adsbygoogle = window.adsbygoogle || []).push({});
 
        

    
```


  




 (adsbygoogle = window.adsbygoogle || []).push({});
 
  

Test:



```
var pair = Pair(1,
            Pair(2,
                  Pair(3,
                        Pair(4, null))));

Print(pair);

        

        
        
        
 (adsbygoogle = window.adsbygoogle || []).push({});
 
        

    
```

output:



```
+> 1
|
+> 2
|
+> 3
|
-> 4

        

        
        
        
 (adsbygoogle = window.adsbygoogle || []).push({});
 
        

    
``` Hello,  
  
Put in new batteries and try again.  
  
If new batteries did not solve the problem, the next thing to check would be the battery terminal.  
  
If
 the battery terminal is rusty, the battery power won't contact and the 
calculator won't turn on. So, use a flat small screw drive to scratch 
the battery terminal so that there would be contact.  
  
If the above
 is not the problem, the next to check would be the wire connecting the 
battery terminal to the the calculator's main board. There is a wire 
between the battery terminal and the calculator board. When the battery 
is inserted, the power from the battery goes from the terminal to the 
board through this wire. There is a possibility that this wire is cut 
in-between or is loosing contact. In this case, you need to get a small 
screw driver and carefully disassemble the calculator in order to check 
the wire.  
  
If the wire is intact, use a light brush to brush the calculator board as there might be dust on the board.  
  
If non of the above helps, then it means the calculator is dead and needs a replacement.  
  
Regards. c = input()  

t = len(c)  

temp = ""  

for i in range(0,len(c)):  

    if t<3:  

        break  

    else:  

        if i in (0,1,t-2,t-1):  

            temp = temp+c[i]  

        else:  

            continue  

print(temp) not sure i understood. are you having problems with this code?

if yes, i think you’re using the wrong comparison operator (not = but ==). The existing solutions based on `findall` are fine for non-overlapping matches (and no doubt optimal except maybe for HUGE number of matches), although alternatives such as `sum(1 for m in re.finditer(thepattern, thestring))` (to avoid ever materializing the list when all you care about is the count) are also quite possible. Somewhat idiosyncratic would be using `subn` and ignoring the resulting string...:



```
def countnonoverlappingrematches(pattern, thestring):
  return re.subn(pattern, '', thestring)[1]

```

the only real advantage of this latter idea would come if you only cared to count (say) up to 100 matches; then, `re.subn(pattern, '', thestring, 100)[1]` might be practical (returning 100 whether there are 100 matches, or 1000, or even larger numbers).


Counting **overlapping** matches requires you to write more code, because the built-in functions in question are all focused on NON-overlapping matches. There's also a problem of definition, e.g, with pattern being `'a+'` and thestring being `'aa'`, would you consider this to be just one match, or three (the first `a`, the second one, both of them), or...?


Assuming for example that you want possibly-overlapping matches *starting at distinct spots in the string* (which then would give TWO matches for the example in the previous paragraph):



```
def countoverlappingdistinct(pattern, thestring):
  total = 0
  start = 0
  there = re.compile(pattern)
  while True:
    mo = there.search(thestring, start)
    if mo is None: return total
    total += 1
    start = 1 + mo.start()

```

Note that you do have to compile the pattern into a RE object in this case: function `re.search` does not accept a `start` argument (starting position for the search) the way *method* `search` does, so you'd have to be slicing thestring as you go -- definitely more effort than just having the next search start at the next possible distinct starting point, which is what I'm doing in this function.