Question

Problem 1: Dynamic Grocery Array Using Java to write a program that prompts the user to...

Problem 1: Dynamic Grocery Array

Using Java to write a program that prompts the user to enter an item’s name for each position in the array. The program then prints uses a loop to output the array.

Example

1: Banana
2: Orange
3: Milk
4: Carrot
5: Chips
Banana, Orange, Milk, Carrot, Chips

Problem 2: Print Characters in String Array   

Declare a string array of size 5. Prompt the user enters five strings that are stored in the array. Using Java to write a function that prints a string in a reverse order. The function must use loop to reverse a string. Call that function from the main and output the value.

Hint: An array of string is like working with multi-dimensional array.

Example

Enter five items.
Item 1: hello
Item 2: C++
Item 3: Wednesday
Item 4: It’s a great day
Item 5: goodbyeReverse strings:
Item 1: olleh
Item 2: ++C
Item 3: yadsendeW
Item 4: yad taerg a s’tI
Item 5: eybdoog

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.

import java.util.Scanner;
public class Main{

public static void main(String []args){
int n=5;
String[] strArray = new String[5];
System.out.println("Enter five items: ");
for(int i=0;i<n;i++)
{
Scanner in = new Scanner(System.in);
strArray[i] = in.nextLine();
}
for(int i=0;i<n-1;i++)
{
System.out.print( strArray[i] + ", ");
}
System.out.println( strArray[n-1]);
  
}
}

2.

import java.util.Scanner;
public class Main {
   public static void reverse (String input)
{   
char[] cha = input.toCharArray();
for (int i = cha.length-1; i>=0; i--)
System.out.print(cha[i]);
}
   public static void main(String []args){
   int n=5;
   String[] strArray = new String[5];
   System.out.println("Enter five items: ");
   for(int i=0;i<n;i++)
   {
   Scanner in = new Scanner(System.in);
   strArray[i] = in.nextLine();
   }
   for(int i=1;i<n;i++)
   {     
   System.out.println( "Item "+ i + ": " +strArray[i-1] );
   }
   System.out.print( "Item "+ 5 + ": " +strArray[4] );
   System.out.println("Reverse strings:");
   for(int i=1;i<=n;i++)
   {  
       System.out.print( "Item "+ i + ": ");
       reverse(strArray[i-1]);
       System.out.println("");
   }
   }
}
If you have any doubt in code feel free to ask in comments.

Add a comment
Know the answer?
Add Answer to:
Problem 1: Dynamic Grocery Array Using Java to write a program that prompts the user to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java program that reads in 5 words from the user and stores these words...

    Write a Java program that reads in 5 words from the user and stores these words in a String array. Then your program needs to print 2 lines of output with the following information: All words are in reverse order (based both on content and order). Every word at an even index of the array (i.e., indices 0, 2, and 4). For example, if user inputs "ABC", "DEF", "TY", "JK", and "WE". Then your program should output the following two...

  • Write a Java program that will create an array of Strings with 25 elements. Input Strings...

    Write a Java program that will create an array of Strings with 25 elements. Input Strings from the user and store them in the array until either the user enters “quit” or the array is full. HINT: the sentinel value is “quit” all lowercase. “Quit” or “QUIT” should not stop the loop. HINT: you have to use the equals method (not ==) when you are comparing two Strings. After the input is complete, print the Strings that the user entered...

  • Write a C++ program for the instructions below. Please read the instructions carefully and make sure...

    Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   please put comment with code! and please do not just copy other solutions. 1. write the code using function 2. Please try to implement a function after the main function and provide prototype before main function. Total Characters in String Array 10 points Problem 2 Declare a string array of size 5. Prompt the user enter five strings that are...

  • Write a Java class named StringProcess that prompts the user to enter two non-empty strings and...

    Write a Java class named StringProcess that prompts the user to enter two non-empty strings and report the followings: 1) The length of the two strings 2) The ASCII value of the first letter in the first string 3) The ASCII value of the last letter in the second string 4) Whether the second string is a substring of the first string. Outputs: Your output should look similar as follows. Please enter the first string: Carrot Please enter the second...

  • Q1 (2 pts) Shopping list Write a program that prompts a user for items on their...

    Q1 (2 pts) Shopping list Write a program that prompts a user for items on their shopping list and keeps prompting the user until they enter "Done" (make sure your program can stop even if the user enters "Done" with different cases, like "done"), then prints out the items in the list, line by line, and prints the total number of items on the shopping list. Output (after collecting items in the while loop) should look something like this: Apple...

  • Write a Java program which takes a string as a user input. Create 2 functions. The...

    Write a Java program which takes a string as a user input. Create 2 functions. The first function expects a string as argument and returns void. It converts all upper case characters to lower case and converts all lower case characters to upper case. It then prints out the converted string to a console. The second function also expects a string as argument and returns void. It will find first charactor that is repeated exactly 2 times in the string....

  •   Write codes that will produce the screen as shown Firstly, the program prompts user to...

      Write codes that will produce the screen as shown Firstly, the program prompts user to enter his name. Then it will display Hello <user name>. Next it prints out a menu which contains 5 options (1- add two integers, 2- add two strings, 3- compute factorial, 4- reverse a string, 5- quit program). Code all 5 tasks appropriately. When the task is completed (not including quitting program of course), the menu pops up again and asks user to try...

  • 11) Write a method (including header and body) that takes as an input an array of...

    11) Write a method (including header and body) that takes as an input an array of doubles and f three doubles containing the average, the maximum and the minimunm values of the input array. Test the program by calling the method from the main method. For example Input: 1 2 3 45 Output: 3 5 1 12) Write a Java method that takes a string as an argument and returns the reverse the string. Test the program by calling the...

  • java programe Write a program that prompts the user to enter in an integer number representing...

    java programe Write a program that prompts the user to enter in an integer number representing the number of elements in an integer array. Create the array and prompt the user to enter in values for each element using a for loop. When the array is full, display the following: The values in the array on a single line. The array with all of the elements reversed. The values from the array that have even numbered values. The values from...

  • Write a Java program to meet the following requirements: 1. Prompt the user to enter three...

    Write a Java program to meet the following requirements: 1. Prompt the user to enter three strings by using nextLine(). Space can be part of the string). ( Note: your program requires using loop: for-loop, while-loop or do-while-loop to get the input String ) 2. Write a method with an input variable (string type).The method should return the number of lowercase letters of the input variable. 3. Get the number of the lowercase letters for each user input string by...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT