C
C#•12mo ago
Dinny

input file error

when reading numbers in my input file, i convert them to integers and assign specific numbers to a variable such as
uint x = Convert.ToUInt32(nextItem[0]);
uint y = Convert.ToUInt32(nextItem[1]);
uint z = Convert.ToUInt32(reader.ReadLine());
uint x = Convert.ToUInt32(nextItem[0]);
uint y = Convert.ToUInt32(nextItem[1]);
uint z = Convert.ToUInt32(reader.ReadLine());
but i keep getting an error saying "'Input string was not in a correct format.'" i am unsure how to fix this because the input file is automatically a string, but converting it to an int is causing an error. I normally convert this way and have not had an issue
94 Replies
Kiel
Kiel•12mo ago
two things: 1. it's better practice to use uint.(Try)Parse than Convert methods - those are older mostly reserved for legacy purposes 2. it doesn't hurt to separate the assignment and conversion so you can more easily see what the "input" is - if it's not the right format, it's not the right format, so you may need to find out why
Dinny
DinnyOP•12mo ago
1. i understand, i will try. my prof taught us to code that way, so we've been using that in out coding. 2. could u elaborate please? :333
Kiel
Kiel•12mo ago
string xString = nextItem[0];
uint x = Convert.ToUInt32(xString);
string yString = nextItem[1];
uint y = Convert.ToUInt32(yString);
string zString = reader.ReadLine();
uint z = Convert.ToUInt32(zString);
string xString = nextItem[0];
uint x = Convert.ToUInt32(xString);
string yString = nextItem[1];
uint y = Convert.ToUInt32(yString);
string zString = reader.ReadLine();
uint z = Convert.ToUInt32(zString);
assuming all your inputs are strings then when debugging you can inspect the values of xString, yString, zString
Dinny
DinnyOP•12mo ago
ok lemme try,plz hold
ZacharyPatten
ZacharyPatten•12mo ago
Is this a school assignment? Are you required to read your input file using a StreamReader? You could probably make your life much easier by using JSON serialization & deserialization instead of coding your own parser
Dinny
DinnyOP•12mo ago
yes this is a school assignment my prof is very old school and encourages us to code this way :/// i just tried and got the same error I'll look up the try parse functuion function
ZacharyPatten
ZacharyPatten•12mo ago
don't just ignore the error, what does the error say?
Dinny
DinnyOP•12mo ago
im not that's why im doing try parse instead of convertto "input string was not in correct format"
ZacharyPatten
ZacharyPatten•12mo ago
and what does that error mean to you? what is it telling you?
Dinny
DinnyOP•12mo ago
i believe it's something to do on my part with the concersion because the input file is read as one big string in the past i've ive convertto to convert to an int but thats not working here used*
ZacharyPatten
ZacharyPatten•12mo ago
instead of "i bleive..." you should check put a breakpoint
Dinny
DinnyOP•12mo ago
well yeah that's why im here i've checked and ran into tjat error so now im trying the try parse
ZacharyPatten
ZacharyPatten•12mo ago
that likely won't help put a breakpoint
Dinny
DinnyOP•12mo ago
but he never taught us that :///
ZacharyPatten
ZacharyPatten•12mo ago
on the line that is throwing the error
Dinny
DinnyOP•12mo ago
i did and it gave me the error i told u :///
ZacharyPatten
ZacharyPatten•12mo ago
putting breakpoints and debugging is a vital skill you need to learn as a software developer so time to start learning it 🙂 I'll share an example
Dinny
DinnyOP•12mo ago
i am trying and it's hard :/// yes plz my tutor was teaching me a bit of how to debug , which has helped me a lot in c++ but i admit i am struggling more with c#
ZacharyPatten
ZacharyPatten•12mo ago
No description
ZacharyPatten
ZacharyPatten•12mo ago
see the red dot on the left side? that is a "breakpoint" you click there to place breakpoints
Dinny
DinnyOP•12mo ago
yes i know
ZacharyPatten
ZacharyPatten•12mo ago
and you can click break points to remove them
Dinny
DinnyOP•12mo ago
i used those ik how to dp that part by using those is where i foud my error bc i was debugging i came to that error if that makes sense im just now struggling to fix it
ZacharyPatten
ZacharyPatten•12mo ago
then when you run the code, it will stop at the break point and you can hover your mouse over variables and such to see their values
No description
ZacharyPatten
ZacharyPatten•12mo ago
in this case, I am trying to convert/parse myString into an int but the value of myString is "cat" and that isn't valid for an int so it will throw the same error you are seeing
Dinny
DinnyOP•12mo ago
ohh okay i see what ur saying
ZacharyPatten
ZacharyPatten•12mo ago
now, in your code, put a breakpoint and tell us what your string looks like that you are trying to convert into a Uint
Dinny
DinnyOP•12mo ago
the there's a header in my file thats "P3" that's suppose to stay a string otherwise the rest of the file is numbers i have a feeling it's trying to convert that one moment plz turns out my feeling was correct lemme try to fix plz hold i keep running into the same error it keeps trying to convert a string of words to an int but im not understanding how bc it should be moving on to the next line it's just staying on the first
ZacharyPatten
ZacharyPatten•12mo ago
it will move on to the next line when you call ReadLine() are you calling ReadLine() multiple times? can you share your code and maybe your input file?
Dinny
DinnyOP•12mo ago
yes one moment
string? p = reader.ReadLine();
uint x = Convert.ToUInt32(nextItem[0]);
uint y = Convert.ToUInt32(nextItem[1]);
uint z = Convert.ToUInt32(reader.ReadLine());


List<Pixel> pixels = new List<Pixel>((int)(x * y));
string? p = reader.ReadLine();
uint x = Convert.ToUInt32(nextItem[0]);
uint y = Convert.ToUInt32(nextItem[1]);
uint z = Convert.ToUInt32(reader.ReadLine());


List<Pixel> pixels = new List<Pixel>((int)(x * y));
ZacharyPatten
ZacharyPatten•12mo ago
that isn't your full code where are you defining nextItem?
Dinny
DinnyOP•12mo ago
oh i thought u meant the portion i was messing up on one moment
Dinny
DinnyOP•12mo ago
Dinny
DinnyOP•12mo ago
do u need my other files too or no @ZP ░▒▓█├■̶˾̶͞■┤█▓▒░
ZacharyPatten
ZacharyPatten•12mo ago
not yet. trying to get you an example
Dinny
DinnyOP•12mo ago
okay sorry, take ur time
ZacharyPatten
ZacharyPatten•12mo ago
static void Main()
{
List<Color> pixels = [];
using StreamReader reader = new("ScrambledFlag_1_1.ppm");
reader.ReadLine(); // skip "P3" header line
string sizeLine = reader.ReadLine()!; // read the "XXX YYY" size line
string[] sizeSplits = sizeLine.Split(' ', StringSplitOptions.RemoveEmptyEntries);
int size1 = int.Parse(sizeSplits[0]);
int size2 = int.Parse(sizeSplits[1]);
int length = size1 * size2;
for (int i = 0; i < length; i++)
{
int red1 = int.Parse(reader.ReadLine()!);
int blue1 = int.Parse(reader.ReadLine()!);
int green1 = int.Parse(reader.ReadLine()!);

pixels.Add(Color.FromArgb(red1, green1, blue1));
}
//UnscrambleMachine.Unscramble(pixels);
using StreamWriter writer = new("UnscrambledFlag.ppm", false);
//writer.WriteLine(p);
//writer.WriteLine(x + " " + y);
//writer.WriteLine(z);
//for (int i = 0; i < pixels.Count; i++)
//{
// writer.WriteLine(pixels[i].ToString());
//}
}
static void Main()
{
List<Color> pixels = [];
using StreamReader reader = new("ScrambledFlag_1_1.ppm");
reader.ReadLine(); // skip "P3" header line
string sizeLine = reader.ReadLine()!; // read the "XXX YYY" size line
string[] sizeSplits = sizeLine.Split(' ', StringSplitOptions.RemoveEmptyEntries);
int size1 = int.Parse(sizeSplits[0]);
int size2 = int.Parse(sizeSplits[1]);
int length = size1 * size2;
for (int i = 0; i < length; i++)
{
int red1 = int.Parse(reader.ReadLine()!);
int blue1 = int.Parse(reader.ReadLine()!);
int green1 = int.Parse(reader.ReadLine()!);

pixels.Add(Color.FromArgb(red1, green1, blue1));
}
//UnscrambleMachine.Unscramble(pixels);
using StreamWriter writer = new("UnscrambledFlag.ppm", false);
//writer.WriteLine(p);
//writer.WriteLine(x + " " + y);
//writer.WriteLine(z);
//for (int i = 0; i < pixels.Count; i++)
//{
// writer.WriteLine(pixels[i].ToString());
//}
}
there is an example that seems to work with the input file you provided you don't need to make both a file stream and a stream reader. you can just directly make the stream reader. also you want to use using in front of the stream reader. that is better than closing it later because it really needs to be in a try-catch and using "using" will do that for you you had multiple loops and things, but you didn't need som eof the extra stuff you had also, you can just change "Color" back to "Pixel" from my example. I didn't have your "Pixel" class so I just used System.Drawing.Color as an alternative
Dinny
DinnyOP•12mo ago
I have to keep my stream reader and writer the same because that how my prof wants us to do it
ZacharyPatten
ZacharyPatten•12mo ago
tell him to fuck off
Dinny
DinnyOP•12mo ago
LMAOOOO im sorry he's very old fashioned :////
ZacharyPatten
ZacharyPatten•12mo ago
seriously though. he shouldn't count off points if you use proper coding practices. if he did I would complain to your school abot that
Dinny
DinnyOP•12mo ago
i doubt he would, but for now, i'd rather stick to what he taught us because, as amazing of an example it is, there's a lot of new stuff i am not familiar with and I don't wanna confuse myself
ZacharyPatten
ZacharyPatten•12mo ago
does my example make sense?
Dinny
DinnyOP•12mo ago
im still reading it, plz hold what's the "!" for in the 7th line
ZacharyPatten
ZacharyPatten•12mo ago
that is a null forgiving operator you don't need it it tells the compiler to ignore a potential null value what version of C# are you using
Dinny
DinnyOP•12mo ago
visual studios is version 17.7.4 and my .NET version is 4.8.04084
ZacharyPatten
ZacharyPatten•12mo ago
you are using old stuff that is .NET Framework that is now legacy ".NET" is the new stuff ".NET Framework" is old and legacy
Dinny
DinnyOP•12mo ago
it is .NET framework sorry i left out the framework part i didn’t realize there was a difference
ZacharyPatten
ZacharyPatten•12mo ago
since this is a school assignment for an asshole of a teacher... you are probably stuck using the old shit... but I'm just trying to inform you that you really shouldnt' be using it anymore
Dinny
DinnyOP•12mo ago
😭😭😭 ur not the only one hoenstly i’ve gotten help multiple times and they’ve told me the same thing a lot of what he teaches isn’t even in use anymore it suck’s but he’s the only c# prof my college has :/
ZacharyPatten
ZacharyPatten•12mo ago
how goes on understanding my example? do you need me to convert it back to .NET Framework? what can I do to help you understand it?
Dinny
DinnyOP•12mo ago
yes plz, could u do that i’m trying to compare it to my own code i think i get the gist but i just wanna make sure i apologize if this is such a hassle
ZacharyPatten
ZacharyPatten•12mo ago
I coded it using .NET 8, which came out last month and has several features you don't have in that older version One sec I'll back port it
Dinny
DinnyOP•12mo ago
tysm
ZacharyPatten
ZacharyPatten•12mo ago
class Pixel
{
public int Red { get; set; }
public int Green { get; set; }
public int Blue { get; set; }

public Pixel(int red, int green, int blue)
{
Red = red;
Green = green;
Blue = blue;
}
}

static void Main()
{
List<Pixel> pixels = new List<Pixel>();
using (StreamReader reader = new StreamReader("ScrambledFlag_1_1.ppm"))
{
reader.ReadLine(); // skip "P3" header line
string sizeLine = reader.ReadLine(); // read the "XXX YYY" size line
string[] sizeSplits = sizeLine.Split(' ');
int size1 = int.Parse(sizeSplits[0]);
int size2 = int.Parse(sizeSplits[1]);
int length = size1 * size2;
for (int i = 0; i < length; i++)
{
int r = int.Parse(reader.ReadLine());
int g = int.Parse(reader.ReadLine());
int b = int.Parse(reader.ReadLine());
pixels.Add(new Pixel(r, g, b));
}
}
//UnscrambleMachine.Unscramble(pixels);
using (StreamWriter writer = new StreamWriter("UnscrambledFlag.ppm", false))
{
//writer.WriteLine(p);
//writer.WriteLine(x + " " + y);
//writer.WriteLine(z);
//for (int i = 0; i < pixels.Count; i++)
//{
// writer.WriteLine(pixels[i].ToString());
//}
}
}
class Pixel
{
public int Red { get; set; }
public int Green { get; set; }
public int Blue { get; set; }

public Pixel(int red, int green, int blue)
{
Red = red;
Green = green;
Blue = blue;
}
}

static void Main()
{
List<Pixel> pixels = new List<Pixel>();
using (StreamReader reader = new StreamReader("ScrambledFlag_1_1.ppm"))
{
reader.ReadLine(); // skip "P3" header line
string sizeLine = reader.ReadLine(); // read the "XXX YYY" size line
string[] sizeSplits = sizeLine.Split(' ');
int size1 = int.Parse(sizeSplits[0]);
int size2 = int.Parse(sizeSplits[1]);
int length = size1 * size2;
for (int i = 0; i < length; i++)
{
int r = int.Parse(reader.ReadLine());
int g = int.Parse(reader.ReadLine());
int b = int.Parse(reader.ReadLine());
pixels.Add(new Pixel(r, g, b));
}
}
//UnscrambleMachine.Unscramble(pixels);
using (StreamWriter writer = new StreamWriter("UnscrambledFlag.ppm", false))
{
//writer.WriteLine(p);
//writer.WriteLine(x + " " + y);
//writer.WriteLine(z);
//for (int i = 0; i < pixels.Count; i++)
//{
// writer.WriteLine(pixels[i].ToString());
//}
}
}
that compiles in .NET Framework you can remove the class Pixel {...} I just added that since I don't have your class
ZacharyPatten
ZacharyPatten•12mo ago
No description
ZacharyPatten
ZacharyPatten•12mo ago
if I put a break point after the using for the stream reader, you can see that the list of pixels has 1215000 items in it because 1350 * 900 = 1215000 (1350 and 900 are the sizes listed in the file on line #2) If you need to go back to the code your teacher wanted for the stream reader/writer... the reason why I moved them around is because in general you should only keep files open while you need them the way you were doing it, your file was open the entire time the program is running but you should only keep the input file open while you are reading it (the first half of your app) and you should only keep the output file open while you are writing to it (the 2nd part of your app) keeping files open for longer than necessary causes issues
Dinny
DinnyOP•12mo ago
ok lemme read up and make some adjustments one moment plz
ZacharyPatten
ZacharyPatten•12mo ago
heads up... I may have swapped green and blue compared to your code. in general most graphics libraries tend to use RGB, not RBG feel free to use RBG if necessary, but that is why I changed it 😛
Dinny
DinnyOP•12mo ago
waitso for the "skipping header" part, i have
string p = reader.ReadLine();
string p = reader.ReadLine();
because my prof said we had to read it to let our program know we will be working with pixels and pictures i gotta get rid of that ? or just get rid of the left half
ZacharyPatten
ZacharyPatten•12mo ago
you just need to skip past the first line of the file. the first line of the file you provided has "P3" which doesn't matter for us every time you call ReadLine it will progress the reader to the next line of the file one of the problems you had in your code is that you had two loops and that probably wasn't necessary:
while (!reader.EndOfStream)
{
...
for (int i = 0; i < pixels.Count; i++)
{
while (!reader.EndOfStream)
{
...
for (int i = 0; i < pixels.Count; i++)
{
you only really need one loop to loop through all the ints in the file
Dinny
DinnyOP•12mo ago
but my prof does the "while !reader.EndOfStream" so we put that in our code im so sorry this is so lik extra 😭 idk y he teaches that way honestly i feel like i give ppl a stroke whenever they see how i have to code lmao
ZacharyPatten
ZacharyPatten•12mo ago
using while (!reader.EndOfStream) is fine there are often multiple ways to do any task in code but what I'm getting at is that you only need one loop not two
Dinny
DinnyOP•12mo ago
oh i have three loops the while loop to read the entire input file, the first for loop to read the pixels and add them to the list, and the last for loop to display the list in the input file
ZacharyPatten
ZacharyPatten•12mo ago
Yes but you are misunderstanding loops when you put one loop inside another loop it is a "nested loop" you were nesting loops when at least for the output part of your code you should do that loop after the input loop finishes Did you try out my code? Did you get it to read in the entire input file?
Dinny
DinnyOP•12mo ago
im still tweaking parts of my code to what you've shown me one moment ok that part is solved, but i am not getting that same error in my override ToString() method one sec
Dinny
DinnyOP•12mo ago
Dinny
DinnyOP•12mo ago
this is my pixel file the ToString() method at the bottom, I get the same error from earlier
ZacharyPatten
ZacharyPatten•12mo ago
that is ugly... I'm assuming your teacher provided that code?
Dinny
DinnyOP•12mo ago
i call this method at the end when displaying my pixel list
for (int i = 0; i < length; i++)
{
writer.WriteLine(pixels[i].ToString());
}
for (int i = 0; i < length; i++)
{
writer.WriteLine(pixels[i].ToString());
}
YES LMAOOO
ZacharyPatten
ZacharyPatten•12mo ago
we don't make Get and Set methods in C#... that is a Java thing...
Dinny
DinnyOP•12mo ago
apparently no one uses doc strings either ??
ZacharyPatten
ZacharyPatten•12mo ago
C# has "properties" so we don't need getter and setter methods
Dinny
DinnyOP•12mo ago
he also teaches java :/// according to him we do :///
ZacharyPatten
ZacharyPatten•12mo ago
he should not be teaching C#...
Dinny
DinnyOP•12mo ago
i feel like imma have to unlearn everything he's taught when i transfer to a 4 year uni :////
ZacharyPatten
ZacharyPatten•12mo ago
can you elaborate on what error you are currently seeing? if the error is happening in the output, it should be a different error
Dinny
DinnyOP•12mo ago
the same one from earlier, 'Input string was not in a correct format.'"
ZacharyPatten
ZacharyPatten•12mo ago
can you get a screenshot of where the error is being thrown?
Dinny
DinnyOP•12mo ago
yes plz hold
Dinny
DinnyOP•12mo ago
No description
ZacharyPatten
ZacharyPatten•12mo ago
o... you have ")" when you need "}" take a closer look 😉
Dinny
DinnyOP•12mo ago
-__- i need a cig after this😭 not actua;;y but oml it really be the littlest things i swear
ZacharyPatten
ZacharyPatten•12mo ago
you will get used to finding stuff like that it never stops 😛 you will always make little typos and crap while coding but as you improve you will get better and catching it
Dinny
DinnyOP•12mo ago
i remeber i spent an error when i simply needed to make something a word lowercase instead of capitilized 😭 ok the code compiled imma check my output file so it compiled but this def is NOT the dutch flag LMAOOO i gott tweak my unscrambler file
ZacharyPatten
ZacharyPatten•12mo ago
lol.... it sounds like your original issue of how to handle the input and output is resolved?
Dinny
DinnyOP•12mo ago
yes it is ! tysm like real kitty really**
ZacharyPatten
ZacharyPatten•12mo ago
do you have any questions about the code?
Dinny
DinnyOP•12mo ago
like i’m truly grateful for ur help uhhh i honestly might 💀💀💀 just gimme one second i did learn a lot from u tho !
Dinny
DinnyOP•12mo ago
my unscramble file literally looks great, i did it with my school tutor
No description
Dinny
DinnyOP•12mo ago
idk how it's ending up like this'it should be solid red, then white, then blue
ZacharyPatten
ZacharyPatten•12mo ago
I will need more info in order to help. did your teacher provide you some instructions explaining what about the input file is "scrambled" and therefore needs to be unscrambled? also can you share the code for your unscrambling method? --- I'll have to go offline for the night soon. I would recommend you close this help post since the original issue of handling the input and output is resolved (so other discord users don't have to skim this entire chat), but open a new help post for your unscrambling logic not working correctly. You will need to provide a lot of extra infor for us to help though.
Dinny
DinnyOP•12mo ago
i understand ty ! i will
Want results from more Discord servers?
Add your server