Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-21-2011, 12:40 AM   PM User | #1
ronron629
New to the CF scene

 
Join Date: Apr 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
ronron629 is an unknown quantity at this point
Java Scanner output missing a number

Hi guys,
I am new to java, I am having trouble on outputting the num2 on the screen. I could not see any errors indicated the problem on the program(Eclipse). However, the program just miss scan the num2. I was trying to sort the number and output them from smallest to largest.
Any suggestion would be helpful and appreciated!

----------------------------------Code
import java.util.Scanner;
public class Demo{
public static void main(String[] args){

System.out.println("Please input three songs' BPMs");
int S,M,L;
Scanner keyboard =new Scanner(System.in);

int num1 = keyboard.nextInt();
int num2 = keyboard.nextInt();
int num3 = keyboard.nextInt();


boolean isAllSame = (num1 == num2) && (num2 == num3) && (num1 == num3);
boolean isDI = (num1 != num2) && (num2 != num3) && (num1 != num3);
boolean isTS = (num1 != (num2 = num3)) || ((num1=num2) !=num3) || ((num1 = num3) != num2);

boolean isCMatch = Math.abs(num2 - num3) < 10 && Math.abs(num2 - num1) < 10;
boolean isCM2 = Math.abs(num3 - num1) < 10 && Math.abs(num3-num2) <10;
boolean isCM3 = Math.abs(num1 - num2) < 10 && Math.abs(num1-num3) <10;

boolean isOkMatch = Math.abs(num2 - num3) >=10 && Math.abs(num2 - num3)<=30;
boolean isOM2 = Math.abs(num3 - num1) >=10 && Math.abs(num3 - num1)<=30;
boolean isOM3 = Math.abs(num1 - num2) >=10 && Math.abs(num1 - num2)<=30;

boolean isMismatch = Math.abs(num2 - num3) >=31;
boolean isMM2 = Math.abs(num3 - num1) >=31;
boolean isMM3 = Math.abs(num1 - num2) >=31;

//small to largest
if (num1<num2 && num1<num3 && num2<num3){
S = num1;
M =num2;
L =num3;}
else if (num2<num1 &&num2<num3&&num1<num3){
S = num2;
M = num1;
L = num3;}
else if (num3<num1 &&num3<num2 && num1<num2){
S =num3;
M =num1;
L =num2;}
else{
S = num2;
M = num3;
L = num1;

System.out.println("Sorted BPMs are: "+S +","+M +","+ L);
}
if (isAllSame)
System.out.println("Three songs have same BPMs.");
else if (isDI && isCMatch){
N(); CM(); }
else if (isDI && isCM3){
N(); CM(); }
else if (isDI && isCM2){
N(); CM(); }
else if (isTS && isCMatch){
SBMP(); CM(); }
else if (isTS && isCM2){
SBMP(); CM(); }
else if (isTS && isCM3){
SBMP(); CM(); }

//When two song BPM larger than 10. order from all different to two same.

else if (isDI && isOkMatch){
N(); OK(); }
else if (isDI && isOM3){
N(); OK(); }
else if (isDI && isOM2){
N(); OK(); }
else if (isTS && isOM2){
SBMP(); OK();}
else if (isTS && isOM3){
SBMP(); OK();}
else if (isTS && isOkMatch){
SBMP(); OK();}

//When some BPMs are larger than 30, Order from all different to some are same.

else if (isDI && isMismatch){
N(); PM(); }
else if (isDI && isMM3){
N(); PM(); }
else if (isDI && isMM2){
N(); PM(); }
else if ( isTS && isMM2){
SBMP(); PM();}
else if ( isTS && isMismatch){
SBMP(); PM();}
else if ( isTS && isMM3){
SBMP(); PM();}

}
public static void SBMP(){
System.out.println("\nSome are the same BMP");
}
public static void N(){
System.out.println("\nNone share the same BMP");
}
public static void CM(){
System.out.println("All are within 10 BPMs of eath other");}
public static void OK(){
System.out.println("All are within 10 to 30 BPMs of each other.");}
public static void PM(){
System.out.println("At least two songs are Possible Match");}

}


------------------------------------Output
Please input three songs' BPMs
15
19
50
Sorted BPMs are: 50,50,15

None share the same BMP
At least two songs are Possible Match
ronron629 is offline   Reply With Quote
Old 04-21-2011, 01:01 AM   PM User | #2
Gox
Regular Coder

 
Gox's Avatar
 
Join Date: May 2006
Location: Ontario, Canada
Posts: 392
Thanks: 2
Thanked 20 Times in 20 Posts
Gox will become famous soon enough
I didn't bother debugging the whole thing, just enough to find the first bug.

Code:
boolean isTS = (num1 != (num2 = num3)) || ((num1=num2) !=num3) || ((num1 = num3) != num2);
Using input 10, 20, 30:
(num1 != (num2 = num3))...translates to - Assign num3 to num2 and check that it's not equal to num1...so to use numbers it becomes
10 != 30 ---> which evaluates to true.

Now, because this evaluates to true I don't believe Java evaluates the ||'s since Java uses short-circuit evaluation. Thus, I believe your num1=num2 and num1=num3 have the same issue I just outlined.

Since you stated you're using Eclispe; Eclipse has a Debugger built in that can be a life saver once you know how to use it. I ran your code and watched when your varibles changed and found the above quite easily. In comparision it would have taken me much longer to find that bug if I tried to simply read the code and look for errors.

Last edited by Gox; 04-21-2011 at 01:15 AM.. Reason: Posted solution doesn't work...taking it out.
Gox is offline   Reply With Quote
Old 04-21-2011, 08:10 PM   PM User | #3
ronron629
New to the CF scene

 
Join Date: Apr 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
ronron629 is an unknown quantity at this point
how do I solve this problem, if the software doesn't recognize the || ?
ronron629 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:24 PM.


Advertisement
Log in to turn off these ads.