{"id":454,"date":"2018-04-11T19:26:54","date_gmt":"2018-04-11T19:26:54","guid":{"rendered":"http:\/\/loop.cs.mtu.edu\/?p=454"},"modified":"2023-10-31T00:27:05","modified_gmt":"2023-10-31T00:27:05","slug":"sentinel-values","status":"publish","type":"post","link":"https:\/\/loop.cs.mtu.edu\/index.php\/2018\/04\/11\/sentinel-values\/","title":{"rendered":"Sentinel Values"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">\u201cHalt! \u00a0Who goes there?\u201d \u00a0The sentinel on duty at the castle was wide awake. \u00a0Our sentinel values don\u2019t carry weapons or defend ramparts, but they perform useful guarding tasks in our programs. \u00a0In computer science, a <\/span><b>sentinel<\/b><span style=\"font-weight: 400;\"> value is a value that would not normally occur in input and serves as a marker of the end of input. \u00a0Occasionally they can also be in a location in an array, not at the end of the data, but representing when a condition has been met.\u00a0 Two examples are discussed below.<\/span><\/p>\n<p><!--more--><\/p>\n<h2><strong>Census: \u00a0Who is the youngest in the castle?<\/strong><\/h2>\n<p><span style=\"font-weight: 400;\">A very common example is reading a stream of numerical input, either from user input or a file. \u00a0Often the input will be a non-negative number such as age, height, or weight. In such a case, the logical sentinel value is -1 because you cannot have an age, height, or weight of -1. <\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">int youngest = Integer.MAX_VALUE;\nScanner scan = new Scanner(System.in);\nSystem.out.println(\"Please enter a list of ages (integers), ending in -1\");\ndo {\n  int age = scan.nextInt();\n  if (age == -1) {\n    break; \n  }\n  if (age &lt; youngest) {\n    youngest = age;\n  }\n} while (scan.hasNext());\nif (youngest != Integer.MAX_VALUE) {\n  System.out.println(\"The youngest person is \" + youngest + \" years old!\");\n} else {\n  System.out.println(\"The castle is empty!\");\n}<\/pre>\n<p>Here we have a program to find the youngest person in the castle.\u00a0 The user inputs the ages separated by spaces or new lines, terminating in our sentinel value of -1.\u00a0 These are read in by a do-while loop.\u00a0 Here, we check for -1 in the loop body, using the keyword <strong>break<\/strong> to exit the loop if that is our current age.\u00a0 \u00a0 We start by setting youngest to Integer.MAX so that any valid age will be less than the current youngest.\u00a0 \u00a0Next, we print out a message for the user to enter input.\u00a0 Then in the loop, we check if the new age is less than our current youngest, updating the value if this is true.\u00a0 Finally, below the loop, we check whether the value of youngest is still <code>Integer.MAX_VALUE<\/code>.\u00a0 If not, we print out the age, otherwise, we print a message indicating the castle is empty.<\/p>\n<p>You can find the full code <span style=\"color: #008000;\"><a style=\"color: #008000;\" href=\"http:\/\/loop.cs.mtu.edu\/wp-content\/uploads\/2018\/04\/CastleYoungest.txt\">CastleYoungest.txt<\/a>\u00a0<span style=\"color: #000000;\">and <span style=\"color: #ff6600;\"><a style=\"color: #ff6600;\" href=\"http:\/\/loop.cs.mtu.edu\/wp-content\/uploads\/2018\/04\/CastleYoungest.java\">CastleYoungest.java<\/a><\/span><\/span><\/span>.<\/p>\n<p><span style=\"font-weight: 400;\">We could have also used a non-number input such as a single letter instead of -1. \u00a0Either way, the sentinel designates the end of input.<\/span><\/p>\n<h2><strong>Names: \u00a0Who sits at the round table and is a knight?<\/strong><\/h2>\n<p><span style=\"font-weight: 400;\">Another common situation in which a sentinel value is used is in a list full of names or other String objects. \u00a0A good sentinel here can be an extra new line, a tab, a specified string, or a numerical value. In the example below we\u2019ll look at using a sentinel that is a special string.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">  public static void main(String[] args) {\n    try (Scanner scan = new Scanner(new File(\"RoundTableSentinelString.txt\"))) {\n      String s = \"\";\n      while (scan.hasNext() &amp;&amp; !s.equals(\"END\")) {\n        s = scan.nextLine();\n        if (!s.equals(\"END\")) {\n          System.out.println(s);\n        }\n        \n      }\n    } catch (FileNotFoundException e) {\n      e.printStackTrace();\n    }\n\n  }\n<\/pre>\n<p>This is a pretty simple layout.\u00a0 We open up a file with a scanner inside a try-catch block like usual for standard <a href=\"http:\/\/loop.cs.mtu.edu\/index.php\/2018\/03\/07\/files-that-make-you-smile\/\"> <span style=\"color: blue;\">file I\/O<\/span><\/a> and then enter our loop.\u00a0 Like other uses of sentinels for reading in values, we check for the presence of the sentinel value in the loop condition.\u00a0 Here, we loop until we reach the end of the file or the word &#8220;END&#8221;.\u00a0 \u00a0Then we quit and exit the program.<\/p>\n<p>When printed using test file <a href=\"http:\/\/loop.cs.mtu.edu\/wp-content\/uploads\/2018\/04\/RoundTableSentinelString.txt\"><span style=\"color: blue;\">RoundTableSentinelString.txt<\/span><\/a>, the output should look like this:<\/p>\n<pre>King Arthur Pendragon\nSir Kay the seneschal\nSir Gwain\nSir Bedivere\nSir Lancelot\nSir Galahad\nSir Mordred\nSir Percival\nSir Tristan\nSir Ector<\/pre>\n<p>The whole class can be viewed <span style=\"color: #008000;\"><a style=\"color: #008000;\" href=\"http:\/\/loop.cs.mtu.edu\/wp-content\/uploads\/2018\/04\/SentinelString.txt\">SentinelString.txt<\/a>\u00a0<span style=\"color: #000000;\">and<span style=\"color: #ff6600;\">\u00a0<a style=\"color: #ff6600;\" href=\"http:\/\/loop.cs.mtu.edu\/wp-content\/uploads\/2018\/04\/SentinelString.java\">SentinelString.java<\/a><\/span><\/span><\/span>\u00a0 Try changing the location of the sentinel &#8220;END&#8221; in the test file to see how it works.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u201cHalt! \u00a0Who goes there?\u201d \u00a0The sentinel on duty at the castle was wide awake. \u00a0Our sentinel values don\u2019t carry weapons or defend ramparts, but they perform useful guarding tasks in our programs. \u00a0In computer science, a sentinel value is a value that would not normally occur in input and serves as a marker of the &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/loop.cs.mtu.edu\/index.php\/2018\/04\/11\/sentinel-values\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Sentinel Values&#8221;<\/span><\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,31],"tags":[25,36,45],"class_list":["post-454","post","type-post","status-publish","format-standard","hentry","category-easy","category-java","tag-cs1121","tag-cs1122","tag-sentinel-values"],"_links":{"self":[{"href":"https:\/\/loop.cs.mtu.edu\/index.php\/wp-json\/wp\/v2\/posts\/454","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/loop.cs.mtu.edu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/loop.cs.mtu.edu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/loop.cs.mtu.edu\/index.php\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/loop.cs.mtu.edu\/index.php\/wp-json\/wp\/v2\/comments?post=454"}],"version-history":[{"count":7,"href":"https:\/\/loop.cs.mtu.edu\/index.php\/wp-json\/wp\/v2\/posts\/454\/revisions"}],"predecessor-version":[{"id":520,"href":"https:\/\/loop.cs.mtu.edu\/index.php\/wp-json\/wp\/v2\/posts\/454\/revisions\/520"}],"wp:attachment":[{"href":"https:\/\/loop.cs.mtu.edu\/index.php\/wp-json\/wp\/v2\/media?parent=454"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/loop.cs.mtu.edu\/index.php\/wp-json\/wp\/v2\/categories?post=454"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/loop.cs.mtu.edu\/index.php\/wp-json\/wp\/v2\/tags?post=454"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}