Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2011
    Thanked 0 Times in 0 Posts

    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!

    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;}
    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");}


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

    None share the same BMP
    At least two songs are Possible Match

  2. AdSlot6
  3. #2
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Ontario, Canada
    Thanked 20 Times in 20 Posts
    I didn't bother debugging the whole thing, just enough to find the first bug.

    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.

  4. #3
    New to the CF scene
    Join Date
    Apr 2011
    Thanked 0 Times in 0 Posts
    how do I solve this problem, if the software doesn't recognize the || ?


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts