Jump to content

Really Quick Q

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
MichaelNQ

MichaelNQ

    Newbie

  • Members
  • PipPip
  • 11 posts
I am using Monodevelop on OS X to build and run a C# file. I don't know C# but it looks very similar to Java so I have the jist.

My problem is, the code looks fine, compiles fine, but has a run time error.

I'm not sure if it is the code or if I am missing and important package or what.

Here the code:


using System;


class InputIntro

{

    public static void Main()

    {

        string input = "";

        int intTest = 0;

        float floatTest = 0;


        Console.WriteLine("Type in an integer:");

        input = Console.ReadLine();

        intTest = int.Parse(input);


        Console.WriteLine("Type in a decimal:");

        input = Console.ReadLine();

        floatTest = float.Parse(input);


        Console.WriteLine("int: {0}  float: {1}", intTest, floatTest);

    }

}



The runtime error is:

Quote

Type in an integer:

Unhandled Exception: System.ArgumentNullException: Argument cannot be null.
Parameter name: s
at System.Int32.Parse (System.String s) [0x00000]
at InputIntro.Main () [0x0001e] in /Users/Home/Desktop/InputIntro.cs:13

The application was terminated by a signal: SIGHUP



Thanks

#2
njr1489

njr1489

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
Your code does not need a string variable. It could be simply written like so:

int intTest = int.Parse(Console.ReadLine());
Console.WriteLine(intTest);

What I did there was declare the int value you specified (intTest), then initialize it to int.Parse(); The argument inside the Parse method is Console.ReadLine(), meaning it will convert the next line the user enters. Try the same for your float variable.

So you know, I ran this on Visual Studio 2008 on Windows XP, it works fine on my side.

#3
Hignar

Hignar

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 420 posts
You could also use
[highlight="C#"]
int intTest = Convert.ToInt32(Console.ReadLine());
float floatTest = (float)Convert.ToDouble(Console.ReadLine());
[/highlight]

Parse is probably the better choice though, especially with the awkward float conversion.
If there's a new way, I'll be the first in line.

But, it better work this time.

#4
ArekBulski

ArekBulski

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,376 posts
You are both right, but how does that fix the exception? You changed the look of code, but not behavior. :o

Maybe this exception is thrown if you press enter without writing a number in the console? After all, it was thrown by the Int32.Parse() method. As njr says, code works.

#5
njr1489

njr1489

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
As far as the error is concerned, I tested the code in the original post. Apparently, that code does work in VS2008. I assume that the problem may have to do with MonoDevelop?

Anyway, I still recommend either Hignar's or my code style for writing user input.

#6
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
You appear to have found a bug in mono, gratz.
Anyways, you should also have that in a try...catch. Ex:

        try
        {
            Console.WriteLine("Type in an integer:");
            intTest = int.Parse(Console.ReadLine());
        }
        catch (FormatException)
        {
            Console.WriteLine("Not a number");
        }


#7
Hignar

Hignar

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 420 posts

ArekBulski said:

You are both right, but how does that fix the exception? You changed the look of code, but not behavior. :o

Maybe this exception is thrown if you press enter without writing a number in the console? After all, it was thrown by the Int32.Parse() method. As njr says, code works.

Your right, I missed that.

I don't know if this works in VS or not but pressing return or entering any non-integer value causes the exception.

TryParse is what OP should be using.

        int intTest = 0;

   	float floatTest = 0;


        Console.WriteLine("Type in an integer:");

        while (!int.TryParse(Console.ReadLine(),out intTest))

        {

	     	Console.WriteLine("Invalid input.  Enter an integer:");

	 }

        Console.WriteLine("Type in a decimal:");

        while (!float.TryParse(Console.ReadLine(), out floatTest))

	 { 

	       Console.WriteLine("Invalid input.  Enter a decimal:");

	 }


        Console.WriteLine("int: {0}  float: {1}", intTest, floatTest);


If there's a new way, I'll be the first in line.

But, it better work this time.