Computer Science Canada

Creating a Multiplication Program

Author:  slider203 [ Tue Feb 16, 2010 7:53 pm ]
Post subject:  Creating a Multiplication Program

Hello I am trying to create a multiplication program where the user enters a number then is multiplied by 1 - 12
heres what I have so far

Quote:

//Import the input/output library.
import java.io.*;
class InputPractice1
{
public static void main (String args [])
//Prevents the program from crashing if incorrect input is entered.
throws java.io.IOException
{
// Allows input from the keyboard.
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
String name = "number";

System.out.println ("Please enter a number:");
//Waits for user input.
name = br.readLine();

System.out.println (number + "x 1 = " + number * 1); <-----THIS LINE KEEPS GIVING ME PROBLEMS
}
}


I keep getting an error that says
2 errors found:
File: H:\ICS\nEW\InputPractice1.java [line: 33]
Error: cannot find symbol
symbol : variable number
location: class InputPractice1

File: H:\ICS\nEW\InputPractice1.java [line: 33]
Error: cannot find symbol
symbol : variable number
location: class InputPractice1

been trying but I cant get it to work. Please take a look any help is appreciated

Author:  TerranceN [ Tue Feb 16, 2010 8:25 pm ]
Post subject:  RE:Creating a Multiplication Program

Where did you declare number? You need to create a variable called number and assign the integer value of name to it.

Also "throws java.io.IOException" will still cause it to crash if there is an exception.

Hope that helps.

Author:  DtY [ Tue Feb 16, 2010 8:26 pm ]
Post subject:  RE:Creating a Multiplication Program

You are trying to multiply a string by a number. Imagine, what is "hello"*3? It doesn't make sense[1].

To do this, you'll have to either read as an integer (instead of a string), or convert the string to an integer after reading.

[1] Some languages will actually give you "hellohellohello" for this


: