I'm stuck on this Java exercise problem and don't know how to organize my code with proper formatting and it's not helping that I'm pretty terrible at logic. I can't figure out how to return the number of spaces to be displayed in the main method.
banhbaozi avatar

Answers

Answer 1
Answer:

In your "count spaces method" before the for loop, you want to declare an int variable (let's call it spc)

then, each time you find that charat, spc++.


also, the string must be declared in the main method, not the spaces count method.


The actual code:


public class CountSpaces{

public static void main(String[] args){

String instring = "(your quote here)";

int spaces = calculateSpaces(instring);

System.out.println("The number of spaces is " + spaces);

}

public static int calculateSpaces(String in){

int spc = 0;

for(int i= 0; i<in.length; i++){

if(in.charAt(i)== ' '){

spc++;

}

}

return spc;

}

}


that should be it.  If you have any questions, feel free to reach out.


Related Questions

Jimmy Fallon's persona when he is outside the White House can best be characterized as:A. humble and excited.B. outrageous and crazy.C. stern and authoritative.D. dark and serious.
Hardware Name:Description:Picture:1. Motherboard2. Power Supply3. CPU (Central Processing Unit)4. Random Access Memory (RAM)5. Hard Disk Drive/Solid State Drive6. Video Card7. Optical Drives8. Input and Output Devices
Write a program that asks the user to enter the size of a triangle (an integer from 1 to 50). Display the triangle by writing lines of asterisks. The first line will have one asterisk, the next two, and so on, with each line having one more asterisk than the previous line, up to the number entered by the user. On the next line write one fewer asterisk and continue by decreasing the number of asterisks by 1 for each successive line until only one asterisk is displayed. (Hint: Use nested for loops; the outside loop controls the number of lines to write, and the inside loop controls the number of asterisks to display on a line.) For example, if the user enters 3, the output would be:_______.a. *b. **c. ***d. **e. *
What are the 21St century competencies or skills required in the information society?​
(5 pt.) The name of a variable in the C programming language is a string that can contain uppercase letters, lowercase letters, digits, or underscores. Furthermore, the first character in the string must be a letter, either uppercase or lowercase, or an underscore. If the name of a variable is determined by its first 8 characters, how many different variables can be named in C

Seasons Write a program that takes a date as input and outputs the date's season. The input is a string to represent the month and an int to represent the day. Ex: If the input is April 11, the output is: spring In addition, check if the string and int are valid (an actual month and day). Ex: If the input is invalid, the output is: invalid The dates for each season are: spring: March 20 - June 20 summer: June 21 - September 21 autumn: September 22 - December 20 winter: December 21 - March 19

Answers

Answer:

#include <iostream>

#include <string>

using namespace std;

int main() {

   string inputMonth;

   int inputDay;

   cin >> inputMonth >> inputDay;

   if (inputMonth == "January" && inputDay >= 1 && inputDay <= 31)

       cout << "Winter" << endl;

   else if (inputMonth == "February" && inputDay >= 1 && inputDay <= 29)

       cout << "Winter" << endl;

   else if (inputMonth == "April" && inputDay >= 1 && inputDay <= 30)

       cout << "Spring" << endl;

   else if (inputMonth == "May" && inputDay >= 1 && inputDay <= 30)

       cout << "Spring" << endl;

   else if (inputMonth == "July" && inputDay >= 1 && inputDay <= 31)

       cout << "Summer" << endl;

   else if (inputMonth == "August" && inputDay >= 1 && inputDay <= 31)

       cout << "Summer" << endl;

   else if (inputMonth == "October" && inputDay >= 1 && inputDay <= 31)

       cout << "Autumn" << endl;

   else if (inputMonth == "November" && inputDay >= 1 && inputDay <= 30)

       cout << "Autumn" << endl;

   else if (inputMonth == "March" && inputDay >= 20 && inputDay <= 31)

       cout << "Spring" << endl;

   else if (inputMonth == "June" && inputDay >= 1 && inputDay <= 20)

       cout << "Spring" << endl;

   else if (inputMonth == "June" && inputDay >= 21 && inputDay <= 30)

       cout << "Summer" << endl;

   else if (inputMonth == "September" && inputDay >= 1 && inputDay <= 21)

       cout << "Summer" << endl;

   else if (inputMonth == "September" && inputDay >= 22 && inputDay <= 30)

       cout << "Autumn" << endl;

   else if (inputMonth == "December" && inputDay >= 0 && inputDay <= 20)

       cout << "Autumn" << endl;

   else if (inputMonth == "December" && inputDay >= 21 && inputDay <= 30)

       cout << "Winter" << endl;

   else if (inputMonth == "March" && inputDay >= 1 && inputDay <= 19)

       cout << "Winter" << endl;

   else

       cout << "Invalid" << endl;

   return 0;

}

Answer:

FOR PYTHON!!

input_month = input()

input_day = int(input())

months= ('January', 'February','March', 'April' , 'May' , 'June' , 'July' , 'August' , 'September' , "October" , "November" , "December")

if not(input_month in months):

   print("Invalid")

elif input_month == 'March':

   if not(1<=input_day<=31):

       print ("Invalid")

   elif input_day<=19:

       print("Winter")

   else:

       print ("Spring")

elif input_month == 'April' :

   if not(1<=input_day<=30):

       print("Invalid")

   else:

       print("Spring")

elif input_month == 'May':

   if not(1<=input_day<=31):

       print("Invalid")

   else:

       print("Spring")

elif input_month == 'June':

   if not(1<=input_day<=30):

       print("Invalid")

   elif input_day<=20:

       print ("Spring")

   else:

       print("Summer")

elif input_month == 'July':

   if not(1<=input_day<=31):

       print("Invalid")

   else:  

       print("Summer")

       

elif input_month == 'August':

   if not(1<=input_day<=31):

       print("Invalid")

   else:  

       print("Summer")

elif input_month == 'September':

   if not(1<=input_day<31):

       print("Invalid")

   elif input_day<=21:

       print ("Summer")

   else:

       print ("Autumn")

elif input_month == "October":

   if not(1<=input_day<=31):

       print("Invalid")

   else:

       print("Autumn")

elif input_month == "November":

   if not(1<=input_day<=30):

       print("Invalid")

   else:

       print ("Autumn")

elif input_month == "December":

   if not(1<=input_day<=31):

       print("Invalid")

   elif input_day <=20:

       print ("Autumn")

   else:

       print ("Winter")

elif input_month == 'January':

   if not(1<=input_day<=31):

       print("Invalid")

   else:

       print("Winter")

elif input_month == "February":

   if not(1<=input_day<=29):

       print("Invalid")

   else:

       print ("Winter")

Explanation:

Good luck in the rest of your class!

What is computer?iwhat is computer ​

Answers

Answer:

an electronic device for storing and processing data, typically in binary form, according to instructions given to it in a variable program

Explanation:

Answer:

a divice to play games on, learn on, and other helpful things

Explanation:

Subtract (100000)2 from (111)2 using 1s and 2s complement method of subtraction​

Answers

Answer:

-11001

Explanation:

The following steps are performed in order to perform subtraction of the given binary numbers:

Step 1:

Find 2’s complement of the subtrahend. The subtrahend here is 100000

100000

First take 1's complement of 100000

1's complement is taken by inverting 100000

1's complement of 100000 = 011111

Now takes 2's complement by adding 1 to the result of 1's complement:

011111 + 1 = 100000

2's complement of 100000 = 100000

Step 2:

Add the 2's complement of the subtrahend to the minuend.

The number of bits in the minuend is less than that of subtrahend. Make the number of bits in the minuend equal to that of subtrahend by placing 0s in before minuend. So the minuend 111 becomes:

000111

Now add the 2's complement of 100000 to 000111

   0 0 0 1  1  1

+   1 0 0 0 0 0

    1 0 0 1  1  1

The result of the addition is :

 1 0 0 1  1  1

Step 3:

Since there is no carry over the next step is to take 2's complement of the sum and place negative sign with the result as the result is negative.

sum =   1 0 0 1  1  1

2's complement of sum:

First take 1's complement of 1 0 0 1  1  1

1's complement is taken by inverting 1 0 0 1  1  1

1's complement of 1 0 0 1  1  1 = 0 1 1 0 0 0

Now takes 2's complement by adding 1 to the result of 1's complement:

0 1 1 0 0 0 + 1 = 0 1 1 0 0 1

Now place the minus sign with the result of 2's complement:

- 0 1 1 0 0 1

Hence the subtraction of two binary numbers (100000)₂ and (111)₂  is

(-011001)₂

This can also be written as:

(-11001)₂

What information is required for a complete citation of a website source?A: the title, volume number, and page numbers
B: the responsible person or organization and the website URL
C: the responsible person or organization, date accessed, and URL
D: the responsible person or organization and the date accessed

Answers

Answer:

C: the responsible person or organization, date accessed, and URL

Explanation:

You wrote a list of steps the user will take to perform a task. Which statement is true about this step?You have drawn pictures of what your screens will look like and identified the input-process-output that occurs on each
screen.
O Your app is functioning and ready to test.
O You have defined a use case.
O In this step, you defined your target audience and main goal.

Answers

The statements that are true about this step are:

  • In this step, you defined your target audience and main goal.
  • You have drawn pictures of what your screens will look like and identified the input-process-output that occurs on each screen.
  • Your app is functioning and ready to test.

What is a an app?

A mobile application, sometimes known as an app, is a computer program or software application that is meant to operate on a mobile device, such as a phone, tablet, or watch.

An app is a software program that allows users to do certain functions on their mobile or desktop device. Apps are either pre-installed on your device or downloaded through a specialized app store, such as the Apple App Store. Apps are usually created in a variety of programming languages.

Learn more about Apps:
brainly.com/question/11070666
#SPJ1

In confirmatory visualization Group of answer choices Users expect to see a certain pattern in the data Users confirm the quality of data visualization Users don't know what they are looking for Users typically look for anomaly in the data

Answers

Answer: Users expect to see a certain pattern in the data

Explanation:

Confirmatory Data Analysis occurs when the evidence is being evaluated through the use of traditional statistical tools like confidence, significance, and inference.

In this case, there's an hypothesis and the aim is to determine if the hypothesis is true. In this case, we want to know if a particular pattern on the data visualization conforms to what we have.

Other Questions