7 Replies - 800 Views - Last Post: 06 November 2012 - 02:19 PM Rate Topic: -----

#1 Dalderup  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 06-November 12

Problem with if statement :)

Posted 06 November 2012 - 11:08 AM

Hey I am new to java (level: Noob) and I am stuck with my BMI/BMR/FAT calculator (making it for fun & to learn java better)

Here's a screenshot and code is below: http://i.imgur.com/oVtq8.png
When I enter 'b' then Java executes object 'a'.
I've been looking around at this forum but can't find something simular. Sorry. :$
import java.io.*;
import java.util.Scanner;

public class Calculator {
	public static void main(String args[]) throws IOException{
		
		BufferedReader MeBuff =  new BufferedReader(new InputStreamReader( System.in ));
		char letter1 = 'A';
		char letter2 = 'B';
		char letter3 = 'C';
        
		AlleenBMI BMIObject = new AlleenBMI();
		
		System.out.println("Wilt u:");
		System.out.println("A: BMI berekenen");
		System.out.println("B: BMR berekenen");
		System.out.println("C: Vet percentage berekenen");
	    String input = MeBuff.readLine();
	    
	    if (letter1 == 'A'){
	    	BMIObject.AlleenBMI();
	    }else if (letter2 == 'B'){
	    	System.out.println("Test");
	    }
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with if statement :)

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Problem with if statement :)

Posted 06 November 2012 - 11:15 AM

Yes because letter1 == 'A' and this is what you are testing
you never extracted the first letter of input. This is what you should be testing
Was This Post Helpful? 0
  • +
  • -

#3 Dalderup  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 06-November 12

Re: Problem with if statement :)

Posted 06 November 2012 - 11:23 AM

View Postpbl, on 06 November 2012 - 11:15 AM, said:

Yes because letter1 == 'A' and this is what you are testing
you never extracted the first letter of input. This is what you should be testing


ooh.. -_- I understand. :$ how can I write this?
I am teaching Java myself so it's kinda hard to find solutions myself :P

I tried:

char A;
char B;
char C;

if (char == 'A'){
	BMIObject.AlleenBMI();
}else if (char == B)/>{
	System.out.println("Test");



Gives me an error that I can't put 'char' in an if statement ;s
while I've seen other people theirs script with "(char == B )" in it. If I am not wrong...


error I got:
Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
	Syntax error on token "char", invalid Expression
	Syntax error on token "char", invalid Expression

	at Calculator.main(Calculator.java:20)



Srry for being a noob :$ haha
Was This Post Helpful? 0
  • +
  • -

#4 kellzor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-May 12

Re: Problem with if statement :)

Posted 06 November 2012 - 12:06 PM

You should compare the input to the char value.

your first try you declared letter1 as A in the code, then in the if statement wrote if letter1 == 'A' (which it does since you declared it so) meaning it will always choose the first one.

if you ask the user to input a choice A B or C, than you need to compare that input to those choices.
if (input == letter1)
-or-
if (input == 'A')
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Problem with if statement :)

Posted 06 November 2012 - 12:39 PM

    String input = MeBuff.readLine();  
    char letter = input.charAt(0);            
    
     if (letter == letter1){     // 'A'  
             BMIObject.AlleenBMI();  
     }else if (letter == letter2){   // 'B'  


Was This Post Helpful? 1
  • +
  • -

#6 Dalderup  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 06-November 12

Re: Problem with if statement :)

Posted 06 November 2012 - 12:47 PM

View Postkellzor, on 06 November 2012 - 12:06 PM, said:

You should compare the input to the char value.

your first try you declared letter1 as A in the code, then in the if statement wrote if letter1 == 'A' (which it does since you declared it so) meaning it will always choose the first one.

if you ask the user to input a choice A B or C, than you need to compare that input to those choices.
if (input == letter1)
-or-
if (input == 'A')


Hey thanks for reply! I am still getting an error. Not knowing what I'm doing wrong.
I've copied
'import java.io.*;'
'throws IOException'
'BufferedReader MeBuff = new BufferedReader(new InputStreamReader( System.in ));'
'String input = MeBuff.readLine();'

From someone else, because I didn't knew how to use user iput (letters/words) and use them in an If statement
I've learned so far how to get/use user input numbers onley,
but I didn't knew how to do it with words so maybe this above is all wrong...?

import java.io.*;
import java.util.Scanner;

public class Calculator {
	public static void main(String args[]) throws IOException{
		
		BufferedReader MeBuff =  new BufferedReader(new InputStreamReader( System.in ));
		char letter1 = 'A';
		char letter2 = 'B';
		char letter3 = 'C';
        
		AlleenBMI BMIObject = new AlleenBMI();
		
		System.out.println("Wilt u:");
		System.out.println("A: BMI berekenen");
		System.out.println("B: BMR berekenen");
		System.out.println("C: Vet percentage berekenen");
	    String input = MeBuff.readLine();
	    
	    if (input == letter1){
	    	BMIObject.AlleenBMI();
	    }else if (input == letter2){
	    	System.out.println("Test");
	    }
}
}



Error code:
Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
	Incompatible operand types String and char
	Incompatible operand types String and char

	at Calculator.main(Calculator.java:20)


Was This Post Helpful? 0
  • +
  • -

#7 Dalderup  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 06-November 12

Re: Problem with if statement :)

Posted 06 November 2012 - 12:56 PM

View Postpbl, on 06 November 2012 - 12:39 PM, said:

    String input = MeBuff.readLine();  
    char letter = input.charAt(0);            
    
     if (letter == letter1){     // 'A'  
             BMIObject.AlleenBMI();  
     }else if (letter == letter2){   // 'B'  



Wow thanks alot! it works :D

Can you explain me real quick what "charAt(0);" does?
Thank you so much :)
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Problem with if statement :)

Posted 06 November 2012 - 02:19 PM

The charAt() function returns the char at position N of a String

http://docs.oracle.c...tml#charAt(int)

String str = "Hamburger";

str.charAt(0) would return 'H'
str.charAt(1) would return 'a'
str.charAt(2) would return 'm'
str.charAt(3) would return 'b'
str.charAt(4) would return 'u'
str.charAt(5) would return 'r'
...

basically you want the first character of the String inputted by the user
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1