Author |
Message |
wtd
|
Posted: Thu May 04, 2006 10:47 pm Post subject: (No subject) |
|
|
Ah, probably should have been:
code: | public class CharTest {
public static void main(String[] args) {
String foo = "foo";
char bar = foo.charAt(0);
System.out.println(bar);
}
} |
I was thinking the String class had a bit of syntactic sugar that it doesn't. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
cool dude
|
Posted: Fri May 05, 2006 3:04 pm Post subject: (No subject) |
|
|
k this is wat i did but the only problem now is it says constant expression required
code: |
//converts string into char in order to use in switch cases
String lettera = "A";
char a = lettera.charAt(0);
String letters = "S";
char s = letters.charAt(0);
String letterm = "M";
char m = letterm.charAt(0);
String letterd = "D";
char d = letterd.charAt(0);
switch(operation2){
case a:
sum = inum1 + inum2;
System.out.println("the sum of the numbers is " + sum);
break;
case s:
subtraction = inum1 - inum2;
System.out.println("The subtracted numbers is equal to " + subtraction);
break;
case "M":
product = inum1 * inum2;
System.out.println("The product of the numbers is " + product);
break;
case "D":
quotient = inum1/inum2;
System.out.println("The quotient is " + quotient);
break;
} |
|
|
|
|
|
|
wtd
|
Posted: Fri May 05, 2006 3:13 pm Post subject: (No subject) |
|
|
Character constants can be represented in Java with single quotes. |
|
|
|
|
|
cool dude
|
|
|
|
|
wtd
|
Posted: Fri May 05, 2006 4:28 pm Post subject: (No subject) |
|
|
Let's clean up your indenting a bit.
code: | import java.io.*;
public class Calculator {
public static void main(String[] args) {
String snum1; //stores the first number
String snum2; //stores the second number
String operation; //stores the operation user chose
int inum1; //stores the converted integer number
int inum2; //stores the converted integer number
int sum; //stores the sum of the numbers
int subtraction; //stores the subtracted value
int product; //stores the product of the numbers
int quotient; //stores the quotient of the numbers
char operation2; //stores the operation as char type
try {
BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
//asks for input from user - 2 numbers and operation
System.out.println("Enter first number");
snum1 = in.readLine();
System.out.println("Enter second number");
snum2 = in.readLine();
System.out.println ("Enter the operation you want");
System.out.println ("A - Addition");
System.out.println("S - Subtraction");
System.out.println ("M - Multiplication");
System.out.println("D - Division");
operation = in.readLine();
operation2 = operation.charAt(0);
//converts the string numbers to integer
inum2 = Integer.parseInt(snum2);
inum1 = Integer.parseInt(snum1);
switch (operation2) {
case 'A':
sum = inum1 + inum2;
System.out.println("the sum of the numbers is " + sum);
break;
case 'S':
subtraction = inum1 - inum2;
System.out.println("The subtracted numbers is equal to " +
subtraction);
break;
case 'M':
product = inum1 * inum2;
System.out.println("The product of the numbers is " +
product);
break;
case 'D':
quotient = inum1/inum2;
System.out.println("The quotient is " + quotient);
break;
}
}
catch (NumberFormatException nfe) {
System.out.println("can't convert: ");
}
catch (IOException f) {
System.out.println("error reading input");
}
}
} |
Now, a few important things about exception handling for you to realize:
- You can have more than one try { ... } catch { ... } construct in a method. You can even have one inside another.
- Variables declared inside a try block are local to that block. They do not exist outside of it. This is known as "scoping" and is vitally important to understand.
|
|
|
|
|
|
cool dude
|
Posted: Fri May 05, 2006 4:39 pm Post subject: (No subject) |
|
|
k so to get this clear. first do u only have one try{...} block and then inside it have as many catch{...} blocks as u need? or do you have a try{...} and catch{...} block for every time u need it? also if i have variables inside the try block does that mean there is no way of getting there values outside of that try block? |
|
|
|
|
|
wtd
|
Posted: Fri May 05, 2006 4:48 pm Post subject: (No subject) |
|
|
Well, the catch blocks always accompany a try block. That is the relationship between them. The try block tries to execute some code, and the catch blocks catch specific classes of exceptions (NumberFormatException for instance).
COnsider the following psuedocode:
code: | try {
try {
}
catch {
}
}
catch () {
}
catch () {
}
try {
}
catch {
} |
And you're correct. A variable declared inside a try block does not exist outside of that block. It also does not exist within the associated catch blocks.
If you need to get at a variable from outside of that block, then you must declare it outside of that block. Consider carefully the scope your variables actually need. |
|
|
|
|
|
cool dude
|
Posted: Fri May 05, 2006 5:10 pm Post subject: (No subject) |
|
|
i'm trying to put a try and catch block after every input. i.e. get first number so i have a try and catch block around that checking to see if its valid and then the same thing with the second number and the operation but then when i get to my switch statement it says the variable has not been initialized. so how can i have a try and catch block around each input if its going to say at my switch statement that the variable is not initialized? the only way it works is by me having one try block and at the end have the catch blocks |
|
|
|
|
|
Sponsor Sponsor
|
|
|
|