0

Code compilation giving me strange errors!

1. I declared an array using: String[] input = new String[]; It gave me an error because I didn't precise the size although it is allowable. 2. Stange things happen with my for loops: for (int i = 0; etc.... It gave an error here of missing semicolon ; I am using this loop to divide a "sentence" into individual words in String array using substring() and charAt(). I am not sure what is happening. Can anyone help me? Is it possible that the problem is caused by pc input? (I share phone screen to pc). Thank youu

22nd Jan 2020, 4:01 PM
Josée Mallah
9 RĂ©ponses
0
Using Strings (when taking characters from one to another): 1. We cannot use charAt() to add chars to a string; it is only used to check characters not to append new ones. 2. We should use stringName.substring() to get substrings from the original string and use "+" to concatenate them to the existing string; we should add substrings instead of characters. This way Pig Latin is solved https://code.sololearn.com/ck8ZTQG5XWQc/?ref=app
24th Jan 2020, 2:30 PM
Josée Mallah
+ 1
Josée Mallah it would be better if you can link the code coach question and your code with your post. It would easy to read the problem statement and then make corrections to your code. You can complete this task within 4-5 lines but everyone have their own way to solve it so I appreciate it. But kindly try to do the above mentioned. Edit: Also you have used next() which will take only a single string or a word but the input might be a sentence where you need to use nextLine(); There are other errors as well.
22nd Jan 2020, 5:04 PM
Avinesh
Avinesh - avatar
+ 1
Avinesh I appreciate your answer. I first used nextLine then thought it may be waiting for ENTER in order to stop reading so I changed it to next. The code isn't complete now. I faced a similar problem with the Driver License too. I would post an update in case I still need help. Thank youu
23rd Jan 2020, 12:08 PM
Josée Mallah
0
~ swim ~ in Pig Latin Code Coach import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner in = new Scanner(System.in); String input = in.next(); int n = input.length(); int first = 0; int last = 0; String[] words = new String[l]; int j = 0; for (int i = 0; i < n: i++) { if (input.charAt(i) == ' ') { last = i; j++; words[j] = input.substring(begin, last); begin = i + 1; } } j++; words[j] = input.substring(begin); for (int i = 0; i < words.length; i++) System.out.println(words[i]); } }
22nd Jan 2020, 4:18 PM
Josée Mallah
0
~ swim ~ You are right This is what I call vision errors due to screen casting. Haha. Yes it is a shame for Sanner, too. And I forgot that I named the length n not l. Please do not judge according to these But I am still finding even crazier errors.
22nd Jan 2020, 4:26 PM
Josée Mallah
0
For example, what is the problem here? Exception because of Scanner or woth some change to the code, I find that the array isn't gettong filled: Helpppp https://code.sololearn.com/cncMwKGyCOkp/?ref=app
23rd Jan 2020, 3:17 PM
Josée Mallah
0
Hi Josee, Look at this code, you can copy pasted it works. I also add some notes. public class Program { public static void main(String[] args) { //It is better start as easy as you can use and then make it more complex //Use a simpel String mySentence = "This is a sentence"; String input = "This is a sentence"; int n = input.length(); //Scanner in = new Scanner(System.in); //String input = in.next(); //int n = input.length(); int begin = 0; int last = 0; //String[] words = new String[l]; String[] words = new String[5]; int j = 0; //Try to use a name that means your intent not just j, something like, "wordPosition" //The problem with array like this is that they dont expand dynamically so you will have to give a predefined size, which in this case, so you can chose 4 to match the sentence in this example. for (int i = 0; i < n; i++) { if (input.charAt(i) == ' ') { last = i; j++; words[j] = input.substring(begin, last); begin = i + 1; } } j++; //words[j] = input.substring(begin); for (int i = 0; i < words.length; i++) System.out.println(words[i]); } } Note: There is another issue in your program in your if statement, you only add a word if you find a blank space. you need to count for the last word in the sentence since most of the time you don't add a space at the end of a sentence, this will make your program miss to add the last word. You can also use split from java in the future but for learning you are doing great. https://code.sololearn.com/cY6zY3qy9WSc/#java
27th Jan 2020, 4:02 AM
Arturo Santos Pardo
Arturo Santos Pardo - avatar