samedi 1 septembre 2018

How can I use an if statement to while reading from a file? [duplicate]

This question already has an answer here:

I am almost brand new to programming. I am in my second college course. I have spent hours looking online and on here looking for help. I have to sort the salaries by year and add them together. I thought I could make an if statement to sort the salaries into different arrays, but it does not appear to be working. As you can see by the code output, it shows invalid year, but it also pops up that the year is 2014. The file that I am reading from follows this format: 2014 Employee Smith,John 2000 0 (this is one line and there are 9 more like it). Thanks for any help and advice.

public class JohnSummersProject1 {

public static void main(String[] args) {

    int count = 0;
    String[] year = new String[10];
    String[][] employeeType = new String[10][2];
    String[][] name = new String [10][2];
    String[][] monthlySalary = new String [10][2];
    String[][] micellanious = new String[10][2];
    String line = "";
    String splitBy = " ";
    BufferedReader inputStream = null;
    String[] input = new String[5];
    String year2014 = "2014";


    try {
        String x = (args[0]);
        inputStream = new BufferedReader(new FileReader(x));
        while ((line = inputStream.readLine()) != null && line.length()!=0) {
            input = line.split(splitBy);
            year[count] = input[0];

            employeeType[count][0] = input[1];
            if(year[count] == year2014){
            name[count][0] = input[2];
            monthlySalary[count][0] = input[3];
            micellanious[count][0] = input[4];
            System.out.print(year[count] + " ");
            System.out.print(employeeType[count][0] + " ");
            System.out.print(name[count][0] + " ");
            System.out.print(monthlySalary[count][0] + " ");
            System.out.println(micellanious[count][0]);
            }
            else if(year[count] == "2015"){
            employeeType[count][1] = input[1];
            name[count][1] = input[2];
            monthlySalary[count][1] = input[3];
            micellanious[count][1] = input[4];
            System.out.print(year[count] + " ");
            System.out.print(employeeType[count][1] + " ");
            System.out.print(name[count][1] + " ");
            System.out.print(monthlySalary[count][1] + " ");
            System.out.println(micellanious[count][1]);
        }
            else{
                System.out.println("Invalid year");
            }
            count++;

        }
    } 
    catch (IOException io) {
        System.out.println("File IO exception" + io.getMessage());
        System.exit(0);
    } finally {
        try {
            if (inputStream != null) {
                inputStream.close();
            }
        } catch (IOException io) {
            System.out.println("Issue closing the Files" + io.getMessage());
        }
    }
    for(int i = 0; i <year.length; i++){
     System.out.println("Year " + year[i] + " Employee type = " +
       employeeType[i][0] + " Name: " + name[i] + " Monthly count = " +
             monthlySalary[i][0] + "extras: " + micellanious[i][0]);
    }
}

} It shows invalid year, but below it shows that the year is in fact 2014

Hey everyone! I've been struggling to figure out this if-else statement

I am trying to have a modal pop up with my output in the body, and if the input is left blank then a sweet alert pops up. I don't currently have the if-else statement set up for that because I have tried everything and it won't work.

    $("#btnfb").click(function () {
        var numfiz = $("#fizz").val();
        var numbuz = $("#buzz").val();
        var outputarray = []; 
        for (var loop = 1; loop <= 100; loop++) {
            if (loop % numfiz === 0 & loop % numbuz === 0) {
                outputarray.push("<span 
                class='boldItalicPurple'>Fizzbuzz</span>");
            }
            else if (loop % numfiz === 0) {
                outputarray.push("<span 
                class='boldItalicGreen'>Fizz</span>");
            }
            else if (loop % numbuz === 0) {
                outputarray.push("<span 
                class='boldItalicOrange'>Buzz</span>");
            }
            else {
                outputarray.push(loop);
            }
        }
        $("fzbzout").html(outputarray.join(", "));
        $('#myModal5').modal('show');
    });

How to loop my question? Javascript Number Guessing Game

New programmer here - I am trying to create a number guessing game. If I guess too high, it tells me "Too high, guess again." If I guess too low, it tells me "Too low, guess again." If I guess correct it tells me correct. But I can only guess 1 wrong answer in each direction. I have searched and searched and tried almost everything (if/else/while)

I know this is probably a simple fix for experienced programmers. Please help. Thanks ---

var secretNumber = 53;

var guess = prompt('Guess a number');


if (guess == (53)) {
alert("Yes you got it! 53 is the right answer!")}

while (guess < (53)) {
var guess = prompt('Too Low, Guess again');
}

while (guess > (53)) {
var guess = prompt('Too High, Guess again');
}

How set a 'timer' before running body from if/else statement?

I want to set some kind of 'timer' before the if/else statement runs parts of it's body. In this case the edit1.setText("") and edit1.setBackgroundColor(Color.parseColor("#FFFFFFF")); Is there a way to do this.

Thanks in advance, have a nice day!

public void check1(View view) {
    uinput = edit1.getText().toString();
    if (uinput.equals(answerList[0])) {
        edit1.setBackgroundColor(Color.parseColor("#00FF00"));
        score++;
        score_view.setText(String.valueOf(score));

        // next question
        // TIMER HERE
        view1.setText(questionList[1]);
        edit1.setText("");
        edit1.setBackgroundColor(Color.parseColor("#FFFFFF"));
    } else {
        edit1.setBackgroundColor(Color.parseColor("#FF0000"));
        wrong++;
        wrong_view.setText(String.valueOf(wrong));

        // next question
        // TIMER HERE
        view1.setText(questionList[1]);
        edit1.setText("");
        edit1.setBackgroundColor(Color.parseColor("#FFFFFFF"));
    }
}

Simplify this into one sentence only

How can I simplify this into one sentence in ?

 If [BT12] = "a" Then
   Range("AB12").ClearContents
 End If

 If [BT13] = "a" Then
   Range("AB13").ClearContents
 End If

 If [BT14] = "a" Then
   Range("AB14").ClearContents
 End If

Fmod function clearly outputs an expected double, yet if(fmod == expected double) doesn't evaluate to true

When I call my function:

formatCurrency(7.5);

string formatCurrency(double cash) {
cout << "fmod(cash,.1) is equal to " << fmod(cash,.1) << endl;
if(fmod(cash,1) == 0) {
    cout << cash << ".00";
}
else if(fmod(cash,.1) == 0.1) {
    cout << cash << "0";
}
else if(fmod(cash,.01) == 0.01) {
    cout << cash;
}
else{
    cout << "Error: unable to display in currency format";
}
return "";
}

fmod(7.5,.1) is clearly equal to .1, and it even outputs as such when I run the program. But instead I get the following output:

fmod(cash,.1) is equal to 0.1
Error: unable to display in currency format

What gives? I see literally nothing wrong with my code. The code does work with whole numbers/the first if statement, but anything with a decimal makes things get dicey.

Python code to check if a input string can be written with a single row of keyboard(eg:potter,flash)

Write a Python code to check if the given word can be typed using just a single row of the keyboard. (e.g. POTTER, EQUITY). Print 'Yes' if the letters of the word are from a single row and print 'No' otherwise.

PLEASE DON'T USE CONCEPTS OTHER THAN STRINGS OR WHATEVER I HAVE USED

Here is my code (not getting desired output)

row1='qwertyuiop'
row2='asdfghjkl'
row3='zxcvbnm'
i=0
j=0
status='no'
st=input('enter a string : ')
if st[0] in row1:
    while(i<len(st)):
        while(j<len(row1)):
            if(st[i]==row1[j]):
                status='yes'
                i+=1
                j=0

            elif(j==(len(st)-1)):
                 status='no'
                 break
            else:
                j+=1
    j=0
    i=0

elif st[0] in row2:
     while(i<len(st)):
        while(j<len(row2)):
            if(st[i]==row2[j]):
                status='yes'
                i+=1
                j=0

            elif(j==(len(st)-1)):
                 status='no'
                 break
            else:
                j+=1
    j=0
    i=0
else: 
    while(i<len(st)):
        while(j<len(row3)):
            if(st[i]==row3[j]):
                status='yes'
                i+=1
                j=0

            elif(j==(len(st)-1)):
                 status='no'
                 break
            else:
                j+=1    ``
print(status)