0

Hey,
Can anyone help me solve this, i am trying to read the value of Console.Readline into a varible declared as Double using vb 2008. As shown below:

Private Sub ReadQuantity()
        ' Read ask customer to input many liters 
        Private quantity As Double
        Console.Write("How many liters (only whole liters please ") "? ")
        Console.ReadLine()
        
       quantity = Double.Parse(Console.ReadLine())
        quantity = Convert.ToDouble(Console.ReadLine())

I tried both coversion function above but got FormatException errors. Is there a way around this or can i use a different approach.

5
Contributors
18
Replies
19
Views
9 Years
Discussion Span
Last Post by EDDYGATE
Featured Replies
  • 1

    [code=vb] Dim temp, quantity As Double Console.Write("How many liters (only whole liters please ) ? ") temp = Console.ReadLine() quantity = Double.Parse(temp)[/code] Read More

0

Hey,
Can anyone help me solve this, i am trying to read the value of Console.Readline into a varible declared as Double using vb 2008. As shown below:

Private Sub ReadQuantity()
' Read ask customer to input many liters
Private quantity As Double
Console.Write("How many liters (only whole liters please ") "? ")
Console.ReadLine()

quantity = Double.Parse(Console.ReadLine())
quantity = Convert.ToDouble(Console.ReadLine())

I tried both coversion function above but got FormatException errors. Is there a way around this or can i use a different approach.

Hi!

Guess this is a conversion from string to double... so can you check "." or ","

Regards

0

Thanks Dix,
But i'm not understanding what i should try,
and yes i am trying to do a conversion. Could you expand on your response please thanks.

0

Thanks Dix,
But i'm not understanding what i should try,
and yes i am trying to do a conversion. Could you expand on your response please thanks.

Can you tell me what is your input?
eg :

dim input as string = console.readline()

if input has a "." and throw formatException then replace the "." by a ","
and if input has "," and throw formatException then replace "," by a "."

Let me know if it works.

Regards

1
Dim temp, quantity As Double      
Console.Write("How many liters (only whole liters please ) ? ")
temp = Console.ReadLine()
quantity = Double.Parse(temp)
0

can you show me what is the content of "TEMP"?
just put a breakpoint after

temp = console.readline()

and paste the content of temp here.

Regards

0

My input is coming from:
Console.Write("How many liters (only whole liters please ") "? ")

I did not declare a string variable to store the input because i need it to be store as a Double for use in a calculation method. if that answer your ?
Do you know of a way i can Read an input line and some how change the returned value data type from String?

0

No...

what i meant is to retrieve your KEYBOARD INPUT

When you ask user to input how many liters

Just store the "CONSOLE.READLINE" in a STRING VARIABLE to check his content.

and then we can see why you have a formatexception when you did a convert.todouble.

Regards

0

Could the ToString method be use to slove this in anyway? I am not that experience in VB.Net

0

i just create a console project and put your code and it works...

0

Ok Dnx,
So did that, but how do i check if its "." or "," i could only deplay the value in the program at run time.

3dees

0

it worked???
Did you convert it to double as well?

0

yes, i didn't change anything in your code

maybe check what you enter... and regional settings?

0

ok Dnx
Tried it again and it worked, will have to check to understand why it didn't worked before.

Dim Liters As String = Console.ReadLine()
quantity = Convert.ToDouble(Liters)

Thanks very much Dnx.

0

No problem, glad i could help even if your code was good since the beginning ;)

Regards.

0

Hey sometimes it just takes a 2nd pair of eyezzzzzz + just this week load VB2008 so now sure if i have all the ms updates.

0

hay,
the below method is the method to use type casting and it will execute
u check it (if u want to cast as value use below or else use type cast(source,typename))
Dim a, quantity As Double
Console.Write("How many liters(only whole liters please )? ")
a = Console.ReadLine()
quantity = CType(a, Double)
Console.WriteLine(quantity)
reply pls.,,

0

I know of the "Convert "class in vb and i think you can try this

dim quantity as double
quantity=convert.toDouble(Console.Readline)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.