Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Whats wrong with my code

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Whats wrong with my code

    Hello i am a beginner please tell me
    whats wrong with my code (red colored)
    it gives me an error for 0308 that "The literal 0308 of type int is out of range"
    this error is only with 0308 and 0307
    any other digit instead of 0308 and 0307 is working

    package Loops;
     
    import java.util.Scanner;
     
    public class Mobile_Network {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
    		Scanner num = new Scanner(System.in);
     
    		System.out.println("Enter Network Code");
     
    		int ncode = num.nextInt();
     
    		if (ncode > 0300 && ncode < [COLOR="#FF0000"]0308[/COLOR]){
    			System.out.println("Your Neywrok Sim is Jazz");
    		}
    		else if (ncode > 0321 && ncode < 0324){
    			System.out.println("Your Neywrok Sim is Warid");
    		}
    		else if (ncode > 0331 && ncode < 0337){
    			System.out.println("Your Neywrok Sim is Ufone");
    		}
    		else if (ncode > 0341 && ncode < 0347){
    			System.out.println("Your Neywrok Sim is Zong");
    		}
     
    	}
     
    }


    --- Update ---

    and how to use "or" condition here
    let say for explanation
    if (ncode == 0301 or 0302 or 0303) kind of that

    thanks in advance


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Whats wrong with my code

    The compiler treats numbers that begin with a 0 as being octal.
    The valid octal digits are 0 to 7. 0308 has an 8 that is not a valid octal digit.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Whats wrong with my code

    thanks
    and when i run the code after removing the error, it just ask for input, does not give any output

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Whats wrong with my code

    after removing the error
    How was the code changed? Were ALL the octal numbers rewritten to be decimal?

    One problem in the code is there needs to be an else at the end of the if/else if chain that prints out a message saying that the value entered was not processed by any of the preceding statements.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Whats wrong with my code

    yes the else at the end was a problem
    now it is only giving me the result of last else statment

    i also change only 0308 to 0310

    --- Update ---

    now its working fine after when i remove 0 from the start of each integer entry

    --- Update ---

    as for knowledge could you please tell me what is the problem with 0

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Whats wrong with my code

    what is the problem with 0
    0 means it is an octal number, not a decimal number
    010 octal = 8 decimal
    If the code enters a decimal number like 307 and compares it to the octal number 0307, they have different values.
    Execute this:
          System.out.println("0307="+0307); // prints int value
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    ansiaries (April 25th, 2014)

Similar Threads

  1. [SOLVED] Whats wrong with my code
    By ansiaries in forum Loops & Control Statements
    Replies: 2
    Last Post: March 27th, 2014, 12:16 PM
  2. Whats wrong with my Code?
    By broodler in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 12th, 2014, 04:13 PM
  3. [SOLVED] Im not sure whats wrong with my code
    By tomlisi92 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: January 25th, 2013, 10:08 PM
  4. help me with a code, whats wrong?
    By Heizzer10 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 17th, 2012, 11:30 AM
  5. whats wrong with my code.
    By jove in forum Object Oriented Programming
    Replies: 3
    Last Post: July 30th, 2011, 11:45 PM