while-loop

String index out of bounds? (Java, substring loop)

与世无争的帅哥 提交于 2019-12-02 00:57:11
问题 This program I'm making for a COSC course isn't compiling right, I keep getting the error: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 2 at java.lang.String.substring(String.java:1765) at VowelCount.main(VowelCount.java:13) Here's my code: import java.util.Scanner; public class VowelCount { public static void main(String[] args) { int a = 0, e = 0, i = 0, o = 0, u = 0, count = 0; String input, letter; Scanner scan = new Scanner (System.in);

Unreachable statement error using while loop in java [duplicate]

Deadly 提交于 2019-12-02 00:56:03
Possible Duplicate: Why is this code giving an “Unreachable Statement” error? This seems very easy question, I found this question in one book. If anyone help me to figure out why I'm getting error. do { System.out.print("inside do"); } while (false); while (false) { // error System.out.print("inside while"); } System.out.print("outside"); I thought, and according to me, output should be inside dooutside . But, it is showing Compiler Error : Unreachable Statement . Then, I tried to figure out, why, it is showing Compilation error : Unreachable Statement* . So, I change the above code like this

String index out of bounds? (Java, substring loop)

邮差的信 提交于 2019-12-02 00:43:49
This program I'm making for a COSC course isn't compiling right, I keep getting the error: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 2 at java.lang.String.substring(String.java:1765) at VowelCount.main(VowelCount.java:13) Here's my code: import java.util.Scanner; public class VowelCount { public static void main(String[] args) { int a = 0, e = 0, i = 0, o = 0, u = 0, count = 0; String input, letter; Scanner scan = new Scanner (System.in); System.out.println ("Please enter a string: "); input = scan.nextLine(); while (count <= input.length(

How do you create a random range, but exclude a specific number?

江枫思渺然 提交于 2019-12-02 00:26:52
I have the following code: while True: if var_p1 != "0": break else: import random var_p1 = random.randint(-5,5) I want the loop to repeat until var_p1 equals anything but zero. However, I get zero all the time. What am I doing wrong? Answering the question in the title, not the real problem (which was answered by Daniel Roseman): How do you create a random range, but exclude a specific number? Using random.choice : import random allowed_values = list(range(-5, 5+1)) allowed_values.remove(0) # can be anything in {-5, ..., 5} \ {0}: random_value = random.choice(allowed_values) "0" != 0 . You

Basic Java: While loop for basic quiz?

♀尐吖头ヾ 提交于 2019-12-02 00:24:44
I started learning Java last night, and I'm trying to make my first code without it being dictated to me! It is a simple quiz question that asks the user "Do you think my dog is cute?" If they answer "Yes," the dog Woofs and smiles. If they say "no" He growls at them and frowns. If a different answer is given, "What? Please try again" is printed. I have no issue getting this working so far, but now I am trying to make it so that when an unrecognised answer is put in, the question is asked again, and this repeats until they answer either "Yes" or "No." Here is what I have tried: import java

What is the difference between while (x = false) and while (!x) in Java?

拜拜、爱过 提交于 2019-12-02 00:13:04
问题 Sorry, I'm new to Java, so this question might be unclear. I have been recently dealing with enclosing a try and catch statement in a while loop, because I wanted to make sure that getting input was enclosed from the rest of the program. I have come across a problem where using an exclamation mark (!) in front of a variable in the while conditions (e.g. while (!done)) instead of using = false (e.g. while (done = false)) changes the way my program runs. The former (!done) results in the try

Usage of defined with Filehandle and while Loop

自闭症网瘾萝莉.ら 提交于 2019-12-02 00:12:51
问题 While reading a book on advanced Perl programming (1) , I came across this code: while (defined($s = <>)) { ... Is there any special reason for using defined here? The documentation for perlop says: In these loop constructs, the assigned value (whether assignment is automatic or explicit) is then tested to see whether it is defined. The defined test avoids problems where line has a string value that would be treated as false by Perl, for example a "" or a "0" with no trailing newline. If you

loop for file location until file exists in bash

为君一笑 提交于 2019-12-02 00:01:58
I have created this function: function promptFile() { while true; do read -p "Please provide a full path [q to quit]: " file if [ $file == q ]; then echo "Exiting.." return 1 fi if [ ! -f $file ]; then echo "File does not exist, please try again" else echo $file break fi done } To prompt a user for file location, ask again if file does not exist, and save the output to a variable if it does, the function is called: tempLoc=$(promptFile) if [ !tempLoc ]; then fileLocation=$tempLoc fi Everything works well unless someone write a bad file location, then the echo is not shown until someone clicks

Loop Issue Counting Year In PHP

那年仲夏 提交于 2019-12-01 23:42:58
I have created a loop which will display date 2004 to 2014 in a formatted way. But the problem is, it is showing 204 instead of 2004 and continue this till 209.. So, how to show those year in formatted way, like 2004,2005,2006 etc. Here is the code i have created, tell me where to fix: <?php $yr = 4; while ($yr <= 14) { $x = 1; while ($x <= 31) { echo "$x Jan 20$yr<br>"; $x++; } $x = 1; while ($x <= 31) { echo "$x Feb 20$yr<br>"; $x++; } $x = 1; while ($x <= 31) { echo "$x Mar 20$yr<br>"; $x++; } $x = 1; while ($x <= 31) { echo "$x Apr 20$yr<br>"; $x++; } $x = 1; while ($x <= 31) { echo "$x

for or while loops in matlab

喜欢而已 提交于 2019-12-01 23:37:52
I've just started using for loops in matlab in programming class and the basic stuff is doing me fine, However I've been asked to "Use loops to create a 3 x 5 matrix in which the value of each element is its row number to the power of its column number divided by the sum of its row number and column number for example the value of element (2,3) is (2^3 / 2+3) = 1.6 So what sort of looping do I need to use to enable me to start new lines to form a matrix? The command bsxfun is very helpful for such problems. It will do all the looping and preallocation for you. eg: bsxfun(@(x,y) x.^y./(x+y), (1