vendredi 24 février 2017

how to incorporate a switch statement in this program

I am trying to incorporate a switch statement in the following code. My program is supposed to create a box with the letter X based on user input (ie. user enters 4 a 4x4 box is created. Extra credit would be adding a do while loop that asks the user if they want to draw another loop. Thank you in advance!

import java.util.Scanner;


public class Squares {

public static void main (String[] args) {

    int boxSize = 0;

    Scanner input = new Scanner(System.in);

    do {
        System.out.println("Please enter a positive number equal or less than 15.");
        boxSize = input.nextInt();

        if(boxSize == -1){
            System.exit(0);
        }

        if(boxSize <2 || boxSize > 15){
            System.out.println("Please try again.");
            continue;
        }

        for (int col = 0; col < boxSize; col++){
            for (int row = 0; row < boxSize; row++){
                if (row == boxSize - 1){
                    System.out.print("X");
                    if (row == boxSize -1) {
                        System.out.println();
                    }
                } else {
                    System.out.print("X");
                    if (row == boxSize - 1){
                        System.out.println();
                         }
                     }
                 }  
             }      
    }
        while(true);
    }

    }

Java Variable counter increasing using loop statements

I need to write a program that finds the best time to meet using 4 inputs from 4 users. Each user will enter 1, 2, or 3 depending on the best time for them. 1 is 8:00-12:00, 2 is 12:00-18:00, and 3 is 18:00-23:00. At the end of the program, it will display the time that had the most inputs, or if two times had an equal amount of inputs, the earliest one. Here I'm not sure what to use really, at first I thought of using a very long series of if statements but that really doesn't work out here. I'm leaning toward using while statements but I don't know how to effectively use them and then take an input from the user and depending on that input (if it's 1, 2, or 3) adding it to a counter which will then be used to see which time had the most hits. Any help would be appreciated! Should I be using one variable for all of the inputs as well?

Scanner kbd=new Scanner(System.in);

    System.out.print("Meeting Times: \n"+"1) 8:00-12:00 2) 12:00-18:00 3) 18:00-23:00\n");
    System.out.print("\nChoose options 1, 2, or 3.\n");

    int p, morning=0, noon=0, evening=0;
    String first = "8:00-12:00", second="12:00-18:00",third="18:00-23:00";

    while (p>4){
        System.out.print("Enter the best time available for person 1.");
        p=kbd.nextInt();
        morning++;
    }kbd.close();

strpos returning true in foreach

it looks like my strpos is only checking the last item in my foreach loop. the first and third condition works properly but not the third. i'm not quite sure where i'm going wrong but any help would be amazing! also excuse the custom commands - it's a company's version of php.

 //get values from the tables 
foreach (sgapiGetValue(15) as $rowid => $rowarray){   
//loop through each row
  foreach ($rowarray as $optionsku => $reportingvalue){  
    $output .= sgapiPrint_R($reportingvalue) ."<br>";
    //type one 
    if (sgapiStrPos($reportingvalue,"C") === 0 && sgapiStrPos($reportingvalue,"D") === 0 || $year != "2016") {
      $output .= sgapiStrPos($reportingvalue,"D");
      sgapiSetValue(733,1);
      sgapiSetValue(735,"text for this box");
  } 
    //type two
  //  $array[] = $reportingvalue;
   // $output .= sgapiPrint_R($array);
    if (sgapiStrPos($reportingvalue,'C') !== false && sgapiStrPos($reportingvalue,'D') === false) {
    sgapiSetValue(733,2);  
      sgapiSetValue(735,"custom text");
        } 
    //type three
    if ($year == "2017" && sgapiStrPos($reportingvalue,"C") === false  && sgapiStrPos($reportingvalue,"D") === false) {
    sgapiSetValue(733,3);   
        } 
} 
}

THANK YOU! (:ᘌꇤ⁐ꃳ 三

Return empty instead of zero when summing range

I'm summing rows down a range using ARRAYFORMULA().

The number of rows in this range will vary over time, due to be linked to a data source that will grow. This is fine on the sheet because there's nothing else below. However, this means I can't limit the range on the array, leading to it summing empty rows at the bottom.

The complication I'm having is that where there's no data to be summed, the formula is returning 0 instead of an error or nothing. I'd like it to return nothing/empty.

Here's the formula for summing down the range by row:

=ARRAYFORMULA(SUMIF(IF(COLUMN(K2:AN),ROW(AN2:AN)),ROW(AN2:AN),K2:AN))

My typical approach to return empty is nested IF() statements, but this is incredibly bulky. There has to be a better way than:

=ARRAYFORMULA(IF(SUMIF(IF(...)...)<>0,SUMIF(IF(...)...)),"")

I've tried these lighter approaches to return empty instead, with no luck:

=ARRAYFORMULA(IFERROR(SUMIF(IF(...)...),""))

=ARRAYFORMULA(SUMIF(IF(ISNUMBER(...))...))

Using loops in R to identify unique cases varying on factor variable

I am still struggling with using if and while loops in real-world datasets. Below is a example dataset. My dataset includes customer IDs and where they purchase their coffee.

customers <- data.table(customer_id = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5), store = c("starbucks", "peets", "coffee bean", "drnk", "starbucks", "coffee bean", "peets", "coffee bean", "drnk", "starbucks"))

What I would like to do is create a loop function that allows me to identify customers who are going elsewhere for their coffee. In this dataset, customer 1 is going to both starbucks and coffeebean.

What I did was next was assign a store_id to each shop in case my loop function will rely on numeric values. Starbucks is 1, Peets is 2, Coffee Bean is 3, and DRNK is 4.

customers <- data.table(customer_id = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5), store = c("starbucks", "peets", "coffee bean", "drnk", "starbucks", "coffee bean", "peets", "coffee bean", "drnk", "starbucks"), store_id_value = c(1, 2, 3, 4, 1, 3, 2, 3, 4, 1))

In my loop function, I was hoping to do something like.. for each customer, if store_id_value of the first purchase is EQUAL to the store_id_value of their next purchase, then continue performing this function til the end. For customers who are purchasing coffee at different locations, this would return a false. Thus, I would like to create a column that shows these TRUEs and FALSEs instead of discontinuing the code.

Any suggestions on how to get this started? Any packages? Thanks for your help everyone!

Java - Can't get if statement with Scanner to work [duplicate]

This question already has an answer here:

Why does this output "Fail" and not "Success" when I type in "test"? It works when I just set the "word" variable in the code, but if I set it based on user input, it doesn't work.

import java.util.Scanner;

public class ifTest {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        String word = input.nextLine();

        if (word == "test") {
            System.out.println("Success");
        }
        else {
            System.out.println("Fail");
        }

    }

}

Basic python: having the user give a letter, and examin if the letter occures in the string

Create a script that examines a string “Geographic Information Science” for the occurrence of a particular letter.

Ask your user to give a letter and store it in a variable x. If the letter occurs in the text (for example, the letter S), the string “Yes” should be printed to the Interactive Window. If the letter does not occur in the text (for example, the letter Z), the string “No” should be printed.