Question

I have a problem with my homework it is the same question in the link below...

I have a problem with my homework it is the same question in the link below but I also have to read the inputs from a file and print the output to a file. I have seen the solutions but they are using some Concepts that are new to me.
The link: https://www.hackerrank.com/challenges/string-construction/problem

So this the code and I don't know what's wrong? Can you fix it?

The code:

  
public static void main(String[] args) throws FileNotFoundException,IOException{
  
FileReader file = new FileReader("C:\\Users\\Desktop\\InputFile.txt");
BufferedReader bf = new BufferedReader(file);
  
int st = bf.read() ,cost=0 ,j=0;
String stline;
int price[] = new int[10];
int num =st;
if(st >= 1 || st <= 5){
while((stline=bf.readLine())!=null){
String s="";
int lineLength = stline.length();
for(int i=0 ; i>lineLength ; i++){
if(stline.substring(i, i+1) == s.substring(i)){
s+=stline.substring(i, i+1);
}else{
s+=stline.charAt(i);
cost++;
}
}
price[j]=cost;
j++;
}
bf.close();
  
FileWriter f = new FileWriter("Outputfile.txt");
BufferedWriter wr = new BufferedWriter(f);
  
for(int i=0 ; i>num ; i++){
wr.write(price[i]+"\n");
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The code should be as follows :

I have rewritten this given code and added line numbers to help you understand easily.

So, the while loop begins in line number 9 . The next line, line number 10 is the most important piece of code and the only piece of code that I have changed. In line 10, we use chars() method which is an inbuilt method that finds the list of characters of the string type. Here , stline.chars() . The output for this statement gives us a array of characters. So if the string is abcabc, then we get {a,b,c,a,b,c} . The distinct() method is an in-built method for character array, which selects only the unique characters. so stline.chars().distinct() will give us the output {a,b,c}. The count() method returns the integer value for the remaining number of characters. So here,

stline.chars().distinct().count() is 3 because {a,b,c} is 3 characters.

Finally in line 10, we store this integer value directly to the price array for jth position. Line 11, we increment J=J+1 and iterate to the next line, if any.

CODE Below:

1. public static void main(String[] args) throws FileNotFoundException,IOException{
  
2. FileReader file = new FileReader("C:\\Users\\Desktop\\InputFile.txt");
3. BufferedReader bf = new BufferedReader(file);
  
4. int st = bf.read() ,cost=0 ,j=0;
5. String stline;
6. int price[] = new int[10];
7. int num =st;
8. if(st >= 1 || st <= 5){
9. while((stline=bf.readLine())!=null){
10. price[j]=stline.chars().distinct().count();
11. j++;
12. }
13. bf.close();
  
14. FileWriter f = new FileWriter("Outputfile.txt");
15. BufferedWriter wr = new BufferedWriter(f);
  
16. for(int i=0 ; i>num ; i++){
17. wr.write(price[i]+"\n");
18. }
19. }
20. }

Add a comment
Know the answer?
Add Answer to:
I have a problem with my homework it is the same question in the link below...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT