Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2008
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    JAVA BASICS (Part II)

    Code:
    int num1;  //declares variable type (int,char,double,float)
    			System.out.println("Enter a number"); //prints out the String "Enter a number"
    			num1=Integer.parseInt(input.readLine());
    			   //takes the inputted text directly above it and stores it as a data type 
    			      
    			      //[num1=Integer.parseInt(input.readLine());]
    			      //num1 --->(the variable) 
    			      //Integer.parseInt---> (data type)  
    			      //Double.parseDouble , Character.parsechar (other datatypes)
    			      //(input.readLine());    /**ask what for**/
    			   
    			   
    			if(num1<100 && num1>80)
    				System.out.println("You inputted a number between 100-80");
    			if(num1>79  && num1<50);
    				System.out.println("You inputted a number between 79-50");
    			if(num1>49  && num1<0);
    				System.out.println("You inputted a number between 49-0");
    			
    			//[IF STATEMENT]---Remember when using &&, ||
    			//[CORRECT]--------(num1>100 && num1<80) 
    			//[WRONG]----------(num1>100) && (num1<80)
    Code:
    System.out.println("Enter a number");
    Whats the difference between System.out.println ,System.out.print and System.out.printf. I also heard some System.out.print +some characters
    can you please tell me there functions or share to me a website with details regarding these statement/code?

    Code:
    num1=Integer.parseInt(input.readLine());


    Integer.parseInt(input.readLine());
    - what does this code mean , where do you use it ect, I tried Character.parseChar (input.readLine()); but there was an error in the code.

    Can you please site the other possible functions in the code ex: Double.parseDouble(input.readLine())


    num1 is the variable your assigning to store the data directly after a statement?.


    Code:
    if(num1<100 && num1>80)
    				System.out.println("You inputted a number between 100-80");
    			if(num1>79  && num1<50);
    				System.out.println("You inputted a number between 79-50");
    			if(num1>49  && num1<0);
    				System.out.println("You inputted a number between 49-0");
    when i try to use if-else if-else or if-else if-else if the code doesnt work. How will the code work using if-else if-else or if-else if-else if. I only know how to use the if-if-if statement

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Quote Originally Posted by jpnv8 View Post
    Code:
    if(num1<100 && num1>80)
    				System.out.println("You inputted a number between 100-80");
    			if(num1>79  && num1<50);
    				System.out.println("You inputted a number between 79-50");
    			if(num1>49  && num1<0);
    				System.out.println("You inputted a number between 49-0");
    the semicolon after the if statement means that the then part is empty for that if statement

    since you posted in the js forum
    Code:
    var num;
    num = prompt("Enter a number");
    try {
      num = parseInt(num);
      if ( num >= 80 && num < 100 ) {
        alert("You inputted a number between 100-80");
      } else if ( num >= 50 && num < 79 ) {
        alert("You inputted a number between 79-50");
      } else if ( num >= 0 && num < 49 ) {
        alert("You inputted a number between 49-0");
      }
    } catch (e) {
      alert("not a valid number");
    }
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    Quote Originally Posted by jpnv8 View Post
    Code:
    int num1;  //declares variable type (int,char,double,float)
    			System.out.println("Enter a number"); //prints out the String "Enter a number"
    			num1=Integer.parseInt(input.readLine());
    			   //takes the inputted text directly above it and stores it as a data type 
    			      
    			      //[num1=Integer.parseInt(input.readLine());]
    			      //num1 --->(the variable) 
    			      //Integer.parseInt---> (data type)  
    			      //Double.parseDouble , Character.parsechar (other datatypes)
    			      //(input.readLine());    /**ask what for**/
    			   
    			   
    			if(num1<100 && num1>80)
    				System.out.println("You inputted a number between 100-80");
    			if(num1>79  && num1<50);
    				System.out.println("You inputted a number between 79-50");
    			if(num1>49  && num1<0);
    				System.out.println("You inputted a number between 49-0");
    			
    			//[IF STATEMENT]---Remember when using &&, ||
    			//[CORRECT]--------(num1>100 && num1<80) 
    			//[WRONG]----------(num1>100) && (num1<80)
    Code:
    System.out.println("Enter a number");
    Whats the difference between System.out.println ,System.out.print and System.out.printf. I also heard some System.out.print +some characters
    can you please tell me there functions or share to me a website with details regarding these statement/code?
    System.out.println means that you are printing a string to system.out and at the end a new line (or an enter key press) will be placed at the end.

    System.out.print means that you are printing a string to system.out and at the end nothing will be appended. No new line, no nothing!

    System.out.printf is used if you want a different way to output a string to System.out. Basically this is a legacy c use which many people still use today for it's ease of use. It allows you to do things such as:

    Code:
    System.out.printf("%d is the number of things I love about java", 10);
    Which prints:
    10 is the number of things I love about java
    Code:
    num1=Integer.parseInt(input.readLine());


    Integer.parseInt(input.readLine());
    - what does this code mean , where do you use it ect, I tried Character.parseChar (input.readLine()); but there was an error in the code.

    Can you please site the other possible functions in the code ex: Double.parseDouble(input.readLine())

    Integer.parseInt(intput.readLine());

    So we take in input from the keyboard, and then instead of storing it in a string variable we place it right into a function that creates an int for us. That means that if the user inputs something other then an int that it will throw an error (try it out yourself).

    Character.parseChar doesn't make sense in Java. Since we can just create a character from a String and vice versa using the String class there is no need to parse it to a particular data type.

    Double.parseDouble(input.readLine()); does the same thing as Integer, but only into a Double. This will work with Integer, Float, and Double.

    You want to use these whenever you know that the input from the keyboard is numeric. Or if you know that your String is a number and you need the number.


    num1 is the variable your assigning to store the data directly after a statement?.


    Code:
    if(num1<100 && num1>80)
    				System.out.println("You inputted a number between 100-80");
    			if(num1>79  && num1<50);
    				System.out.println("You inputted a number between 79-50");
    			if(num1>49  && num1<0);
    				System.out.println("You inputted a number between 49-0");
    when i try to use if-else if-else or if-else if-else if the code doesnt work. How will the code work using if-else if-else or if-else if-else if. I only know how to use the if-if-if statement
    You've placed semicolons at the end of your if statements. Doing this means that you don't want anything to happen after the if statement is true.

    What you want is this:

    Code:
    if(num1<100 && num1>80)
    				System.out.println("You inputted a number between 100-80");
    			if(num1>79  && num1<50)
    				System.out.println("You inputted a number between 79-50");
    			if(num1>49  && num1<0)
    				System.out.println("You inputted a number between 49-0");
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • Users who have thanked Aradon for this post:

    jpnv8 (03-28-2008)

  • #4
    New Coder
    Join Date
    Mar 2008
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks bro

  • #5
    Regular Coder
    Join Date
    Jan 2008
    Location
    Willow Grove, PA
    Posts
    169
    Thanks
    1
    Thanked 27 Times in 27 Posts
    Hey, jpnv8. There is a "thank user for this helpful post" button that you can use to show your thanks.


  •  

    Posting Permissions

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