Συμμετέχει από: 17 Δεκ 07Δημοσιεύσεις: 3134Τόπος: ΘεσσαλονίκηΤώρα Βλέπω: Naruto,One piece,toriko,hunterXhunter,buddy complex,space dandy και πολλά ακόμα
Γεια χαρά παίδες. Έχω ένα πρόβλημα σε αυτή την άσκηση που έχω για project και με βγάζει error στο split.
Η εκφώνηση μας λέει το εξείς:
Spoiler:
Παράθεση:
Write an application that reads a text file. The program should read the entire file and, at the end, display a count of how many words occurred in the file that were 1, 2, 3, 4 or 5 letters long and how many words were longer than 5 letters.
For example, if the text file contains the lines:
This is a test of the fifth
exercise for week 10 in the module
Programming Principles
The output from the program would be;
The number of words of length 1 is 1.
The number of words of length 2 is 4.
The number of words of length 3 is 3.
The number of words of length 4 is 3.
The number of words of length 5 is 1.
The number of words of length greater than 5 is 4.
Punctuation characters are not treated as special characters for this exercise.
Hint.
You might find the Split method from the string class useful for this exercise. This method takes as a parameter a character array containing a set of separation characters. It returns a string array containing the components of the line split at the separation characters.
For example,
string[] stringBits = myString.Split(new char[] { ',' });
If myString contains the following before this line of code:
aaaaa,bbbbb,cc,dddd
Then the string array stringBits will contain the following after the line has been executed:
stringBits[0] aaaaa
stringBits[1] bbbbb
stringBits[2] cc
stringBits[3] dddd
και ο κώδικας ως τώρα έιναι αυτός:
Spoiler:
Παράθεση:
using System;
using System.IO;
namespace Exercise_10._5
{
class Program
{
static void Main()
{
string linereader;
int length1=0,length2=0,length3=0,length4=0,length5=0,length6=0;
StreamReader filereader=null;
filereader = new StreamReader("File_text.txt");
string[] splited_words = filereader.Split(new char[] { ' ' });
linereader = filereader.ReadLine();
for (int i=1;i<6;i++){
if (splited_words[i].Length==1){
length1 = length1 + 1;
}
else if (splited_words[i].Length == 2)
{
length2 = length2 = 1;
}
else if (splited_words[i].Length == 3)
{
length3 = length3 + 1;
}
else if (splited_words[i].Length == 4)
{
length4 = length4 + 1;
}
else if (splited_words[i].Length == 5)
{
length5 = length5 + 1;
}
else
{
length6 = length6 + 1;
}
}
Console.Write("The number of words of length 1 is ");
Console.WriteLine(length1);
Console.Write("The number of words of length 2 is ");
Console.WriteLine(length2);
Console.Write("The number of words of length 3 is ");
Console.WriteLine(length3);
Console.Write("The number of words of length 4 is ");
Console.WriteLine(length4);
Console.Write("The number of words of length 5 is ");
Console.WriteLine(length5);
Console.Write("The number of words of length greater than 5 is ");
Console.WriteLine(length6);
filereader.Close();
}
}
}
και το μοναδικό error είναι στο split και με λέει
Παράθεση:
Error 1 'System.IO.StreamReader' does not contain a definition for 'Split' and no extension method 'Split' accepting a first argument of type 'System.IO.StreamReader' could be found (are you missing a using directive or an assembly reference?)
_________________ oruka karu ota tatoyito, koro noreo koros taka. urame, nikume, soste mini kouiki oni mai. nigete, nigete sein shiomi tsokumai. soste itsu kara, oreta nazi omote oreno mai koi
η μεθοδος Split δουλεύει σε αντικείμενα τύπου String. Από ότι βλέπω την χρησιμοποιείς στο filereader όπου πριν το δηλώνεις σαν τύπου StreamReader.
Μηπως ηθελες να βαλεις την γραμη
linereader = filereader.ReadLine();
πρώτα και μετα να εφαρμωσεις την Split στο linereader?
Συμμετέχει από: 17 Δεκ 07Δημοσιεύσεις: 3134Τόπος: ΘεσσαλονίκηΤώρα Βλέπω: Naruto,One piece,toriko,hunterXhunter,buddy complex,space dandy και πολλά ακόμα
το δοκίμασα και πάλι το ίδιο πρόβλημα έχει
_________________ oruka karu ota tatoyito, koro noreo koros taka. urame, nikume, soste mini kouiki oni mai. nigete, nigete sein shiomi tsokumai. soste itsu kara, oreta nazi omote oreno mai koi
Συμμετέχει από: 17 Δεκ 07Δημοσιεύσεις: 3134Τόπος: ΘεσσαλονίκηΤώρα Βλέπω: Naruto,One piece,toriko,hunterXhunter,buddy complex,space dandy και πολλά ακόμα
yup το δοκίμασα. απλά μου βγάζει σε Μπλέ γραμί το Split και μου γραφει στα errors
Παράθεση:
Error 1 'System.IO.StreamReader' does not contain a definition for 'Split' and no extension method 'Split' accepting a first argument of type 'System.IO.StreamReader' could be found (are you missing a using directive or an assembly reference?)
_________________ oruka karu ota tatoyito, koro noreo koros taka. urame, nikume, soste mini kouiki oni mai. nigete, nigete sein shiomi tsokumai. soste itsu kara, oreta nazi omote oreno mai koi
Παράξενο. Αυτό το error στο βγαζει με όλα τα String η μονο με αυτο.
Κανε δοκιμη.
Ακριβος κάτω από το main πρόσθεσε αυτό
Spoiler:
string test = ("I can watch anime all day");
string[] splited_words = test.Split(new char[] { ' ' });
for (int i = 0; i < splited_words.Length; i++)
{
Console.WriteLine(splited_words[i]);
}
Aν σου υπογραμίζει το Split ξανα μάλον υππάρχει πρόβλημα με τα libraries. Αλλιώς βεβαιώσου ότι το χρησιμοποιείς πανω σε αντικείμενο που είναι String.
Συμμετέχει από: 17 Δεκ 07Δημοσιεύσεις: 3134Τόπος: ΘεσσαλονίκηΤώρα Βλέπω: Naruto,One piece,toriko,hunterXhunter,buddy complex,space dandy και πολλά ακόμα
αυτό δουλέβει μια χαρα. απλά είναι το πως θα βγάλουμε το το πόσα γράμματα έχει η κάθε λέξη για να μοπούν και αυτά σε ένα array. οπότε είναι σαν να κάνω split και τα γράμματα.
πάντως thx για την διευκρίνηση με το split σε array θα χρειαστεί παρακάτω
_________________ oruka karu ota tatoyito, koro noreo koros taka. urame, nikume, soste mini kouiki oni mai. nigete, nigete sein shiomi tsokumai. soste itsu kara, oreta nazi omote oreno mai koi
Συμμετέχει από: 17 Δεκ 07Δημοσιεύσεις: 3134Τόπος: ΘεσσαλονίκηΤώρα Βλέπω: Naruto,One piece,toriko,hunterXhunter,buddy complex,space dandy και πολλά ακόμα
θα το τσεκάρω και να το δω
_________________ oruka karu ota tatoyito, koro noreo koros taka. urame, nikume, soste mini kouiki oni mai. nigete, nigete sein shiomi tsokumai. soste itsu kara, oreta nazi omote oreno mai koi
Δεν μπορείτε να δημοσιεύσετε νέο Θέμα σ' αυτή τη Δ.Συζήτηση Δεν μπορείτε να απαντήσετε στα Θέματα αυτής της Δ.Συζήτησης Δεν μπορείτε να επεξεργασθείτε τις δημοσιεύσεις σας σ' αυτή τη Δ.Συζήτηση Δεν μπορείτε να διαγράψετε τις δημοσιεύσεις σας σ' αυτή τη Δ.Συζήτηση Δεν έχετε δικαίωμα ψήφου στα δημοψηφίσματα αυτής της Δ.Συζήτησης