Author | Message |
---|---|
wcwir
Posts: 75
|
Posted 12:33 Mar 07, 2019 |
I made a mistake on the assignment writeup -- I asked you to submit "TestTutor.java" for the Typing Tutor program. Let's stick with the "TestTutor,java" name even though it doesn't describe what the program does. Some of you are coming to me with problems caused by using .next().charAt(0) to read input; that was useful for HW2, but now we are working with Strings, so it is better to read input using .next() or .nextLine(), which will give you the whole string. |
lvaldova
Posts: 12
|
Posted 11:46 Mar 08, 2019 |
For Echo.java: When I run the program it crashes if the user input is less than 3. I tried to fix it by having the first if statement be with Boolean expression (userInput.length() < 3) and have it display "Your entry must be..." and exit the program, but it doesn't work and I get the following error message: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 What am I doing wrong and how can I fix this? Last edited by lvaldova at
11:49 Mar 08, 2019.
|
wcwir
Posts: 75
|
Posted 12:13 Mar 08, 2019 |
You are going about it the right way -- if the input String is too short, don't process it any further. But remember: if your input string is at least 3 characters long, the largest index it is guaranteed to have is 2 -- not 3 -- because we start counting from 0. If you are trying to access index 3 in a String that is 3 characters long, you will get StringIndexOutOfBounds exception. In your case, it seems you are trying to access index -1, which does not exist in any String. Why? What is on line 20 in your Echo program? |
wcwir
Posts: 75
|
Posted 12:14 Mar 08, 2019 |
By popular demand, deadline extended by two days. |
lvaldova
Posts: 12
|
Posted 12:19 Mar 08, 2019 |
I changed the boolean expression to (userInput.length() < 2) but still get the same error message. Line 20 is: String lastThree = userInput.substring(userInput.length() - 3); |
wcwir
Posts: 75
|
Posted 12:42 Mar 08, 2019 |
You should be processing all Strings that are at least 3 characters long, so you are fixing the wrong thing. Are you sure that line 20 is performed ONLY IF your String is at least 3 characters long? Because if instead you have if (userInput.length() < 3) { String lastThree = userInput.substring(userInput.length() - 3); } then you will get a StringIndexOutOfBoundsException for sure, since userInput.length() - 3 is guaranteed to be less than 0. |
lvaldova
Posts: 12
|
Posted 12:55 Mar 08, 2019 |
No, line 20 is before any if-else statements. |
jcolora5
Posts: 6
|
Posted 12:58 Mar 08, 2019 |
my code accept number(this is weird because I only have strings in my code) but I do not know how to code the if statement when a number is input print "your number must be a letter" do you have any hit or location in the book where I can find a hit? |
wcwir
Posts: 75
|
Posted 13:06 Mar 08, 2019 |
Ah! that's why. That operation should only be performed after you validate the input. You first need to check: is the String at least 3 characters long? If yes, then you can subtract 3 from its length and you'll get at least 0, which is a valid index for this String. But if you don't check, and your program encounters a String that is less than 3 characters long, when you subtract 3 you will get a value less than 0 as the index, and that will result in an exception. |
lvaldova
Posts: 12
|
Posted 13:06 Mar 08, 2019 |
There are methods that check if a character is a letter, digit, uppercase etc.... |
wcwir
Posts: 75
|
Posted 13:18 Mar 08, 2019 |
Your code should accept the input as a String, not a number -- so your Scanner should read using the next() method (if you want to read just one word without white space before or after it) or the nextLine() method (if you want to read all the words on the line including all the white space before, after, and between the words). Does that answer your question? (In general, for how to user Scanner you can check the Scanner API here: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html. And you can check either Chapter 4 of our textbook, or the Java String API: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html)
|
wcwir
Posts: 75
|
Posted 16:35 Mar 09, 2019 |
Hey everyone -- I snuck a peek at some of your submissions and noticed some of you are using the matches() method in the String class for the PhoneValidator. This is a much easier way of solving the problem -- and it is also against the requirements of the assignment. Note that the assignments says not to use regular expressions, and regular expressions are exactly what makes the matches() method so convenient and powerful. In general: you should solve the problems using the techniques we studied in class and/or that are in the chapters we have covered so far. The point of the assignments isn't just to turn in code that solves the problem (as it might be if this were a task you were assigned at work) but to learn to the basics. When I see submissions with syntax or methods we have not covered, it makes I think that instead of studying and trying to solve the problem yourself you are googling around for solutions! That may or may not be plagiarism depending on how you've used the code you've seen and whether or not you have acknowledged the source in the comments, but it is not the best way to learn or to prepare for the midterm. Last edited by wcwir at
16:40 Mar 09, 2019.
|