funny little program has a error.help

Java programming topics
Post Reply
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

funny little program has a error.help

Post by Face » Wed Aug 11, 2010 12:06 am

This funny program made by me & it has no errors.but I need to develop it.I try to get that mobile number with the first ZERO.but it has error..

Code: Select all

import java.util.Scanner;

/*
*created by Gayan Bandara
*date-2010.Aug.10 night
*this is too check the friendship
*Think the phone number is 0712346578
*/

class do_you_know_gayan{
	
	public static void main(String args[]){
		Scanner gayan = new Scanner(System.in);
		double phone;
		System.out.println("This programm will check the friendship with Gayan & you..");
		System.out.println("Get ready.....Now");
		
		System.out.println("Enter Gayan's mobile number :-");
		
		phone = gayan.nextDouble();
		
		if (phone ==0712345678){
			System.out.println("you know him");
			}else{ 
				System.out.println("you don't know him");
				}
		
		}
	}
/*
 * tested Gayan Bandara.
 */
This got a error like this..

Code: Select all

>javac do_you_know_gayan.java
do_you_know_gayan.java:22: integer number too large: 0712345678
		if (phone ==0712345678){
		            ^
1 error
>Exit code: 1
I think that if I change y variable type..will it correct?

Then I changed it like this..

Code: Select all

import java.util.Scanner;

/*
*created by Gayan Bandara
*date-2010.Aug.10 night
*this is too check the friendship
*Think the phone number is 0712346578
*/

class do_you_know_gayan{
	
	public static void main(String args[]){
		Scanner gayan = new Scanner(System.in);
		double phone;
		System.out.println("This programm will check the friendship with Gayan & you..");
		System.out.println("Get ready.....Now");
		
		System.out.println("Enter Gayan's mobile number with out first '0'of the phone number:-");
		
		phone = gayan.nextDouble();
		//trying to get the full number like this 0712345678..but :(
		if (phone ==712345678){
			System.out.println("you know him");
			}else{ 
				System.out.println("you don't know him");
				}
		
		}
	}
/*
 * tested Gayan Bandara.
 */
User avatar
Herath
Major
Major
Posts: 417
Joined: Thu Aug 05, 2010 7:09 pm

Re: funny little program has a error.help

Post by Herath » Wed Aug 11, 2010 12:34 am

It seems to me that using string comparisons is the best way to achieve this. I am including the code.

Code: Select all

    import java.util.Scanner;

    /*
    *created by Gayan Bandara
    *date-2010.Aug.10 night
    *this is too check the friendship
    *Think the phone number is 0712346578
    */

    class do_you_know_gayan{

       public static void main(String args[]){
          Scanner gayan = new Scanner(System.in);
          String phone;
          System.out.println("This programm will check the friendship with Gayan & you..");
          System.out.println("Get ready.....Now");

          System.out.println("Enter Gayan's mobile number :-");

          phone = gayan.next();
          phone.trim();
           
          if (phone.equals("0712345678")){
             System.out.println("you know him");
             }else{
                System.out.println("you don't know him");
                }

          }
       }
    /*
    * tested Gayan Bandara.
    */
Notes:
1. The trim() method of String class is used to remove the leading and trailing whitespaces in the input string.
http://download.oracle.com/javase/6/doc ... tml#trim()
2.The equals(Object anObject) method is used to compare the "0712345678" string object and the phone string object. Or you could use phone.compareTo("0712345678")==0 in the if condition expression.

http://download.oracle.com/javase/6/doc ... tring.html

Refering Javadocs also helps a lot. I use the reference documents a lot. They are the best sources. 8-)
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: funny little program has a error.help

Post by Face » Wed Aug 11, 2010 7:15 pm

Thanks for the help Bro...

it is great help from you again & again.I am trying with some PDFs to learn.The links you gave me is to much complicated for such a beginner like me.But I am keep reading & reading..

your code works...wow.
User avatar
Herath
Major
Major
Posts: 417
Joined: Thu Aug 05, 2010 7:09 pm

Re: funny little program has a error.help

Post by Herath » Wed Aug 11, 2010 7:48 pm

One remark though, seems like trimming is not necessary. Scanner reads without the leading and trailing spaces. :)
Post Reply

Return to “Java Programming”