{"id":29201,"date":"2023-01-06T05:36:28","date_gmt":"2023-01-06T00:06:28","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-java-sentence-analysis-check\/"},"modified":"2023-01-06T05:36:28","modified_gmt":"2023-01-06T00:06:28","slug":"solved-java-sentence-analysis-check","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-java-sentence-analysis-check\/","title":{"rendered":"[Solved] Java Sentence Analysis Check"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-40294697\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"40294697\" data-parentid=\"40271294\" data-score=\"3\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Your code is confuse, but for what I understood, you want to analyse a certain phrase.<\/p>\n<p>First, the constructor of the class that handles the analyse must receive the <code>String<\/code> to analyse, since it is it&#8217;s job. (Every class has a job, the result it&#8217;s obtained from the classes methods).<\/p>\n<p>Second, there is a convention on Java naming, and for classes the names start with capital letter. Check this document from Oracle: <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.oracle.com\/technetwork\/java\/codeconventions-135099.html\">http:\/\/www.oracle.com\/technetwork\/java\/codeconventions-135099.html<\/a><\/p>\n<p>So, the class should be something like this:<\/p>\n<pre class=\"lang-java prettyprint-override\"><code>public class Odev1 {\n\n    private final String phase;\n\n    public Odev1( String phrase ) {\n        this.phrase = phrase;\n    }\n\n    \/\/Remaining code...\n}\n<\/code><\/pre>\n<p>As for the analyse itself there is several things that you should have in mind:<\/p>\n<p>First, you don&#8217;t have to to create an array of <code>char<\/code> from the <code>String<\/code>, you can just get the <code>char<\/code> directly with the <code>String<\/code>&#8216;s <code>charAt( int index )<\/code> method.<\/p>\n<p>Second, when you have a for-loop, you can use the same leters, since the variables are local and therefor they won&#8217;t be visible out of the for-loop<\/p>\n<p>Third, when you construct a loop, you must make sure that you can break out of the loop. As an example, you wrote:<\/p>\n<pre class=\"lang-java prettyprint-override\"><code>int n=0;\nfor(int a=0;a&lt;characters;a++) {\n    do{\n        n=n+1;\n    }while(array[a]!='0'); \n}\nwords=characters-n;\n<\/code><\/pre>\n<p>Just here you have an infinite loop. Why are you in a do-while looping over the same <code>char<\/code>? Follow this:<br \/>\n<code>int n = 0;<\/code> =&gt; <code>int a=0;<\/code> =&gt; <code>do<\/code> =&gt; <code>n = n + 1<\/code> -&gt; <code>n = 1<\/code> =&gt; condition <code>while<\/code> =&gt; <code>array[a]!='0'<\/code> -&gt; <code>true<\/code> =&gt; repeat <code>do-while<\/code> loop&#8230; Forever&#8230;<\/p>\n<p>Instead, you could iterate over the <code>String<\/code> and use <code>if-else<\/code> conditions to analyse it. Something like this:<\/p>\n<pre class=\"lang-java prettyprint-override\"><code>int n=0;\nboolean isWord = false; \/\/Use it to track the letters\nboolean isSpace = false;\nfor(int i=0;i&lt;characters;i++) {\n    char charToAnalyse = input.getCharAt( i );\n\n    if( charToAnalyse == ' ' ) { \/\/Taking the simple case where you don't have breaklines\n        isSpace = true;\n        isWord = false;\n    }\n    else {\n        isSpace = false;\n        if( !isWord ) {\n            isWord = true; \/\/once it is counted, we should avoid recount.\n            n++; \/\/Its is the same as n = n + 1;\n        }\n    }\n}\nwords=characters-n;\n<\/code><\/pre>\n<p>As a simpler way, you could use the <code>split(String regex)<\/code> method, but I gave the example above to help you understand. <\/p>\n<pre class=\"lang-java prettyprint-override\"><code>String[] wordsArray = input.split( \" \" );\nwords = wordsArray.lenght;\n<\/code><\/pre>\n<p>Forth, use <code>else-if<\/code> instead of constantly <code>if<\/code>&#8216;s if the inclusion of one means the exclusion of the remain. That is, in your code above you iterate over the letters to see if they are capital or not.<\/p>\n<p>Also, you don&#8217;t have to write a loop for each variable. Each character has several atributes, so just check them all in a single loop:<\/p>\n<pre class=\"lang-java prettyprint-override\"><code>int capitalletters = 0;\nint lowerletters = 0;\nint vowel = 0;\n\nfor(int i=0;i&lt;characters;i++) {\n    char charToAnalyse = input.charAt(i);\n\n    if (Character.isUpperCase( charToAnalyse )) { \n        capitalletters++; \n    }\n    else if (Character.isLowerCase( charToAnalyse  )){ \n        lowerletters++; \n    }\n    \/\/Imagine if there were many if statement and the condition \n    \/\/was already true in the first...\n\n    \/\/count vowels;\n    switch( charToAnalyse  ) {\n        case 'a':\n        case 'e':\n        case '\u0131':    \n        case 'i':\n        case 'o':\n        case '\u00f6':\n        case 'u':\n        case '\u00fc':\n            vowel++;\n        break; \/\/You can omit this one if it is at the end;\n    }\n\n}\n<\/code><\/pre>\n<p>In the end, to use the class <code>Odev1<\/code>, just do:<\/p>\n<pre class=\"lang-java prettyprint-override\"><code>Odev1 cumle = new Odev1( input );\ncumle.analyse();\n<\/code><\/pre>\n<p>I hope I have helped.<\/p>\n<p>Have a nice day. \ud83d\ude42<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">2<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Java Sentence Analysis Check <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Your code is confuse, but for what I understood, you want to analyse a certain phrase. First, the constructor of the class that handles the analyse must receive the String to analyse, since it is it&#8217;s job. (Every class has a job, the result it&#8217;s obtained from the classes methods). Second, there is a &#8230; <a title=\"[Solved] Java Sentence Analysis Check\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-java-sentence-analysis-check\/\" aria-label=\"More on [Solved] Java Sentence Analysis Check\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[323,362,1080],"class_list":["post-29201","post","type-post","status-publish","format-standard","hentry","category-solved","tag-java","tag-string","tag-text"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Java Sentence Analysis Check - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-java-sentence-analysis-check\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Java Sentence Analysis Check - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Your code is confuse, but for what I understood, you want to analyse a certain phrase. First, the constructor of the class that handles the analyse must receive the String to analyse, since it is it&#8217;s job. (Every class has a job, the result it&#8217;s obtained from the classes methods). Second, there is a ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-java-sentence-analysis-check\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-06T00:06:28+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-java-sentence-analysis-check\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-java-sentence-analysis-check\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Java Sentence Analysis Check\",\"datePublished\":\"2023-01-06T00:06:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-java-sentence-analysis-check\/\"},\"wordCount\":337,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"java\",\"string\",\"text\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-java-sentence-analysis-check\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-java-sentence-analysis-check\/\",\"name\":\"[Solved] Java Sentence Analysis Check - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2023-01-06T00:06:28+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-java-sentence-analysis-check\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-java-sentence-analysis-check\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-java-sentence-analysis-check\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Java Sentence Analysis Check\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Java Sentence Analysis Check - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-java-sentence-analysis-check\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Java Sentence Analysis Check - JassWeb","og_description":"[ad_1] Your code is confuse, but for what I understood, you want to analyse a certain phrase. First, the constructor of the class that handles the analyse must receive the String to analyse, since it is it&#8217;s job. (Every class has a job, the result it&#8217;s obtained from the classes methods). Second, there is a ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-java-sentence-analysis-check\/","og_site_name":"JassWeb","article_published_time":"2023-01-06T00:06:28+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-java-sentence-analysis-check\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-java-sentence-analysis-check\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Java Sentence Analysis Check","datePublished":"2023-01-06T00:06:28+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-java-sentence-analysis-check\/"},"wordCount":337,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["java","string","text"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-java-sentence-analysis-check\/","url":"https:\/\/jassweb.com\/solved\/solved-java-sentence-analysis-check\/","name":"[Solved] Java Sentence Analysis Check - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-01-06T00:06:28+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-java-sentence-analysis-check\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-java-sentence-analysis-check\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-java-sentence-analysis-check\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Java Sentence Analysis Check"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/29201","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=29201"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/29201\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=29201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=29201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=29201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}