...

View Full Version : JAVA BASICS (Part II)



jpnv8
03-25-2008, 02:28 AM
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)


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?



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?.



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

shyam
03-25-2008, 08:35 AM
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 ;)

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

Aradon
03-25-2008, 07:55 PM
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)


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:



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






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?.



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:



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

jpnv8
03-27-2008, 02:44 AM
thanks bro :D

sobrien79
03-27-2008, 05:55 PM
Hey, jpnv8. There is a "thank user for this helpful post" button that you can use to show your thanks.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum