Java Basic Problems

Java: How to Take input of Float type variables and then add it??

In my last post I have shared about  How to add Integer numbers in Java.

Here I have shown how to add and work with float type variables in java.

In the given example I have taken two float type variable as input and then add it.

 float variable java

Code Example

import java.io.*;

class FloatDemo

{

public static void main(String args[])

{

float floatNumber=0.0f;

float floatNumber1=0.0f;

float sumOf=0.0f;

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

try{

System.out.println(“\n”);

System.out.println(“************************************”);

System.out.println(“\n”);

System.out.println(“Enter 1st Floating point input:”);

floatNumber=Float.valueOf(br.readLine()).floatValue();

System.out.println(“\n”);

System.out.println(“Enter 2nd Floating point input:”);

floatNumber1=Float.valueOf(br.readLine()).floatValue();

System.out.println(“\n”);

sumOf=floatNumber+floatNumber1;

System.out.println(“After Addition of the 2 float  number =”+sumOf);

System.out.println(“\n”);

System.out.println(“************************************”);

}

catch(Exception e)

{

Exception newEx=new Exception(“Error at:”+new java.util.Date()+””,e);

newEx.printStackTrace();

}

}

}

Java Basic Problems

Java: How to Know in which line Java Exception Occurred?

There is  a very simple way to know where and which line exception occurs in your java code, and it is  very useful tool for diagnosing an Exception.printStackTrace()  is that simple tool which will help you to diagnose your code in an easy way

It  will tell you what happened  and  where (in which line )  in the code this exception  happened

 printStackTrace()  is  a method of the Throwable class. All Exceptions are a subclass of that class.

It’s a great debugging tool.

Here is the code example with output to show the function of  printStackTrace()

import java.io.*;

class numerator1

{

static String input;

static int intInput;

                        public void myNumerator()

                        {

                                                            try

                                                            {

            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

            System.out.println(“\n”);

                                                            System.out.println(“Enter the Numeric:”);

                                                            System.out.println(“\n”);

                                                            input=br.readLine();

                                                            intInput=Integer.parseInt(input);

                                                            System.out.println(“\n”);

                                                            System.out.println(“You  have Entered:”+intInput);

                                                            System.out.println(“\n”);

                                                            intInput=intInput/0;

      //It will cause an error as I have divided the input number by zero(0)

      //java.lang.ArithmeticException: / by zero  (Check the Output)        

                                                            System.out.println(“\n”);

                                                            }

                                                            catch(Exception e)

                                                            {

                                                            //System.out.println(“Error:”+e.getMessage());

                                    Exception newEx = new Exception(“Error at:”+new java.util.Date()+””,e);

                                    newEx.printStackTrace();

                                                            }

                        }

public static void main(String args[])

{

numerator1 x=new numerator1();

x.myNumerator();

}

}OUTPUT:

Java Basic Problems

Java: RunTime Input from User Example

I have discussed about how to take input from user in runtime from command line here.

Here I will try to give a simple example regarding this.

In this example, our program will take total student number and total subject as input, then it will take input of each subject number. After calculating the average number of total subject, if any student gets less than 40 it will show that student is failed and if any student gets more than 40 that student is passed.

Here is the given Code:

import java.io.*;

class student

{

public static void main(String args[])

{

String inputa;

int num_std;        // Total Number of Student

int sub_std;        // Total Subject of Student

System.out.println(“************************************”);

System.out.println(“\n”);

System.out.println(“Enter the Total Number of Students:”);

System.out.println(“\n”);

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

try{

inputa=br.readLine();

num_std=Integer.parseInt(inputa);

System.out.println(“Enter the Total Subject of Student:”);

System.out.println(“\n”);

inputa=br.readLine();

sub_std= Integer.parseInt(inputa);

int i=0;

int j=0;

int z=0;

System.out.println(“\n”);

for (i=1;i<=num_std;i++)

{

System.out.println(“Enter Student  :” +i+” :Subject Number:”);

System.out.println(“\n”);

for (j=1;j<=sub_std;j++)

{

inputa= br.readLine();

z= z+Integer.parseInt(inputa);

if(j==sub_std)

{

System.out.println(“Student:”+i+”Total Number:”+z);

int om=(z)/sub_std;

if(om<40)

{

System.out.println(“Student:”+i+”fails”);

System.out.println(“Student  :”+i+” :  Average Number:”+om);

om=0;

z=0;

}

else

{

System.out.println(“Student  :”+i+” :  passed”);

System.out.println(“Student  :”+i+” :  Average Number:”+om);

om=0;

z=0;

}

}

} //End of student  subject loop

} //End of student num loop

System.out.println(“\n”);

System.out.println(“************************************”);

}

catch(IOException e)

{}

}

}

OUTPUT:

Java Basic Problems

Java: How to take Run time input from user

Suppose you are doing your coding in java (let you are running your program from command prompt of your desktop) and you need to take input from user in command line or command prompt while  user  run (actually runtime input) your program.

Now I will show you how to take user input from command prompt or command line in java.

Here I have used BufferedReader  and readLine()  to take input from user in command line/ command prompt while the program was running or  simply in run time.

In the example below  br.readLine() takes  input as String, in the next line we have parsed or convert that String into integer.

Code:

import java.io.*;

public class CmdLineInput

{

CmdLineInput()

{

String inputa;

int num_std;

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

try{

System.out.println(“Enter the number of Students:”);

inputa=br.readLine();

num_std=Integer.parseInt(inputa);

System.out.println(“Number of Students:”+num_std);

   }

 catch(Exception e){}

   }

public static void main(String args[])

{

CmdLineInput takeinput=new CmdLineInput();

}

}

OUTPUT: