Advanced Calculator java script problems - calculator

I am trying to make a calculator in which many options are there to do, but I am having some problems when I am trying to make the input continuous.
Help me The code is given below. The error is showing, "variable input might have not been initialized, although i have initialized it. Have a look:
import java.util.*;
class Calculator
{
public static void main(String Args[])
{
int a,func,input;double digit;
System.out.println("THIS IS HOW CALCULATOR WORKS");
do
{
System.out.println("\n\n\n1. Add\n2. Subtract\n3. Divide\n4. Multiply\n5. Square\n6. Square root\n7. Cube\n8. Cube root\n9. Continue\n10. Exit");
System.out.print("\nEnter the serial no. of the operation you would like to perform : ");
Scanner sc=new Scanner(System.in);
a=sc.nextInt();
if(a==1)
{
System.out.println("Enter the number you want to add");
input=sc.nextInt();
System.out.println("Enter number to be added");
func=sc.nextInt();
digit= input+func;
System.out.println("Final Answer:"+digit);
}
if(a==2)
{
System.out.println("Enter the number you want to subtract");
input=sc.nextInt();
System.out.println("Enter number to be subtracted");
func=sc.nextInt();
digit= input-func;
System.out.println("Final Answer:"+digit);
}
if(a==3)
{
System.out.println("Enter the number you want to divide");
input=sc.nextInt();
System.out.println("Enter number to be divide");
func=sc.nextInt();
digit= input/func;
System.out.println("Final Answer:"+digit);
}
if(a==4)
{
System.out.println("Enter the number you want to multiply");
input=sc.nextInt();
System.out.println("Enter number to be multiplied");
func=sc.nextInt();
digit= input*func;
System.out.println("Final Answer:"+digit);
}
if(a==5)
{
System.out.println("Enter the number you want to square");
input=sc.nextInt();
digit= Math.pow(input,2);
System.out.println("Final Answer:"+digit);
}
if(a==6)
{
System.out.println("Enter the number you want to square root");
input=sc.nextInt();
digit=Math.sqrt(input);
System.out.println("Final Answer:"+digit);
}
if(a==7)
{
System.out.println("Enter the number you want to cube");
input=sc.nextInt();
digit= Math.pow(input,3 );
System.out.println("Final Answer:"+digit);
}
if(a==8)
{
System.out.println("Enter the number you want to cube root");
input=sc.nextInt();
digit= Math.cbrt(input);
System.out.println("Final Answer:"+digit);
}
if(a==9)
{
System.out.println("\n\n\n1. Add\n2. Subtract\n3. Divide\n4. Multiply\n5. Square\n6. Square root\n7. Cube\n8. Cube root\n9. Exit");
System.out.print("\nEnter the serial no. of the operation you would like to perform : ");
int double digit;
int a=sc.nextInt();
if(a==1)
{
System.out.println("Enter the number you want to add");
input=sc.nextInt();
System.out.println("Enter number to be added");
func=sc.nextInt();
digit= input+func;
System.out.println("Final Answer:"+digit);
}
if(a==2)
{
System.out.println("Enter number to be subtracted");
func=sc.nextInt();
digit= input-func;
System.out.println("Final Answer:"+digit);
}
if(a==3)
{
System.out.println("Enter number to be divide");
func=sc.nextInt();
digit= input/func;
System.out.println("Final Answer:"+digit);
}
if(a==4)
{
System.out.println("Enter number to be multiplied");
func=sc.nextInt();
digit= input*func;
System.out.println("Final Answer:"+digit);
}
if(a==5)
{
digit= Math.pow(input,2);
System.out.println("Final Answer:"+digit);
}
if(a==6)
{
digit=Math.sqrt(input);
System.out.println("Final Answer:"+digit);
}
if(a==7)
{
digit= Math.pow(input,3 );
System.out.println("Final Answer:"+digit);
}
if(a==8)
{
digit= Math.cbrt(input);
System.out.println("Final Answer:"+digit);
}
if(a==9)
{
System.out.println("Thank you for using calculator");
}
}
if(a==10)
{
System.out.println("Thank you for using the calculator");
break;
}
}while(a!=9);
}
}

Yes, but your statements initializing input are all inside if blocks. what if a is none of the above? you have to provide a default value for input. or else, it will not have one if a is anything but 1-9.

Related

How to get the decimal places of a number in Flutter/Dart

I wondered if there is a function to provide clean solution to getting the amount of decimal places or checking if a number has decimal places in Dart/Flutter?
Here is how I've done it:
static int getDecimalPlaces(var number) {
int decimals = 0;
List<String> substr = number.toString().split('.');
if (substr.length > 0) decimals = int.tryParse(substr[1]);
return decimals;
}
static bool hasDecimalPlaces(var number) {
String <List> substr = number.toString().split('.');
return (substr.length > 1);
}
You should check if it contains it at all.
This returns how many decimals you got:
getNumberOfDecimals(String number) {
if (number.contains('.')) {
return number.substring(number.indexOf('.')+1).length;
}
return 0;
}
A bit more elegant answer. You split the string at the decimal point and check the length of the 2nd string.
getNumberOfDecimals(String number) {
if (number.contains('.')) {
return number.split(".")[1].length;
}
return 0;
}

How to convert timestamp format while using codeigniter

I am using-
echo $post->entry_time;
this will echo a proper timestamp like - 2015-12-09 13:47:46.
i want to format it with 16 December 2015 with time.
In below i have created function for get UTCmills to Date , just add function in helper.php and you can use in view, controller and model,
/*
#return convert UTC milisec to date format
#param $milisec , $formate
#explain 1 $milisec convert to date
#explain 2 $formate convert as per date format
*/
function UTCmillsToDate($milisec, $formate = "Y-M-D") {
$mil = $milisec;
$seconds = $mil / 1000;
if ($formate == "YMD") {
return date("Ymd", $seconds);
} else if ($formate == "Y-M-D") {
return date("Y-m-d", $seconds);
} else if ($formate == "d/m/Y") {
return date("d/m/Y", $seconds);
} else if ($formate == "Y-m-d H:i:s") {
return date("Y-m-d H:i:s", $seconds);
}else if ($formate == "d-m-Y H:i:s") {
return date("d-m-Y H:i:s", $seconds);
}
else if ($formate == "s") {
return date("r", $seconds);
} else {
return date("d-m-Y", $seconds);
}
}
Example :
1 ==> UTCmillsToDate('1449750420')
Output: 2015-10-12
2 => UTCmillsToDate('1449750420','Y-m-d H:i:s')
Output: 2015-10-12 12:27:00

swift 1.2 if loop to switch statement

I have the following If-Statment and I was wondering how this could be achieved with a switch statement?
I am trying to represent an integer value within an array as a string (e.g. 1 == "Jan")
func assigningMonthName([Data]) {
for i in dataset.arrayOfDataStructures {
if (i.month) == 1 {
println("Jan")
}
else if (i.month) == 2 {
print("Feb")
}
else if (i.month) == 3 {
print("March")
}
else if (i.month) == 4 {
print("April")
}
else if (i.month) == 5 {
print("May")
}
else if (i.month) == 6 {
print("June")
}
else if (i.month) == 7 {
print("July")
}
else if (i.month) == 8 {
print("August")
}
else if (i.month) == 9 {
print("September")
}
else if (i.month) == 10 {
print("October")
}
else if (i.month) == 11 {
print("November")
}
else if (i.month) == 12 {
print("December")
}
else {
println("Error assigning month name")
}
}
}
any answers would be appreciated :)
While you can use a switch, this is essentially just another way to write if-else so there is no big improvement to your code:
switch i.month {
case 1:
print("Jan")
case 2:
print("Feb")
...
}
What about using an array?
let monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "Sept", "October", "November", "December"]
print(monthNames[i.month - 1])
The system actually already contains month names, they are even localized:
let monthNames = NSDateFormatter().monthSymbols;
print(monthNames[i.month - 1])
Try this:
switch i.month {
case 1:
print("Jan")
case 2:
print("Feb")
...
default:
print("default value")
}

Magento sorting in cateory listing page

I got the following code here:app\code\core\Mage\Catalog\Block\Product\List\Toolbar.php and i got the following function.
Can we add more code here to solve the issue.:--
Products should sort like:-
Products Name: Prices
A: $200
BC: $200
B: $100
BD: $100
CD: $100
C: $99
public function setCollection($collection)
{
$this->_collection = $collection;
$this->_collection->setCurPage($this->getCurrentPage());
// we need to set pagination only if passed value integer and more that 0
$limit = (int)$this->getLimit();
if ($limit) {
$this->_collection->setPageSize($limit);
}
/* if ($this->getCurrentOrder()) {
$this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
} */
if ($this->getCurrentOrder()) {
if(($this->getCurrentOrder())=='price'){
$this->_collection->setOrder('entity_id','asc');
}
else {
$this->_collection->setOrder($this->getCurrentOrder(),$this->getCurrentDirection());
}
}
return $this;
}
I want to display products listing of category from high to
low price if prices of all the products are same then all the products will be shown alphabetically by default on page load.
Yes i got the answer:
public function setCollection($collection)
{
$this->_collection = $collection;
$this->_collection->setCurPage($this->getCurrentPage());
// we need to set pagination only if passed value integer and more that 0
$limit = (int)$this->getLimit();
if ($limit) {
$this->_collection->setPageSize($limit);
}
if ($this->getCurrentOrder()) {
$this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
}
if ($this->getCurrentOrder()) {
if($this->getCurrentOrder()=='price')
{
$this->_collection->setOrder('price','asc');
$this->_collection->setOrder('name','asc');
}
}
else {
$this->_collection->setOrder($this->getCurrentOrder(),$this->getCurrentDirection());
}
return $this;
}

Time format in Quartz.net tables

The Start_time field in Database table(dbo.QRTZ_TRIGGERS) shows this value 635371706123133677
What format is this and how can it be converted to human readable format
This is the from the StdAdoDelegate.cs class:
AddCommandParameter(cmd, "triggerStartTime", GetDbDateTimeValue(trigger.StartTimeUtc));
And GetDbTimeValue is
public virtual object GetDbDateTimeValue(DateTimeOffset? dateTimeValue)
{
if (dateTimeValue != null)
{
return dateTimeValue.Value.UtcTicks;
}
return null;
}
So basically DateTimeOffset.UtcTicks
And this is the code used to convert that value back:
public virtual DateTimeOffset? GetDateTimeFromDbValue(object columnValue)
{
if (columnValue != null && columnValue != DBNull.Value)
{
var ticks = Convert.ToInt64(columnValue, CultureInfo.CurrentCulture);
if (ticks > 0)
{
return new DateTimeOffset(ticks, TimeSpan.Zero);
}
}
return null;
}