Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

To be or not to be, that is the question. Even though it does not make much sens

ID: 3694798 • Letter: T

Question

To be or not to be, that is the question.

Even though it does not make much sense to do this as the phrase is short. We are going to create bins for each word, and record the position of the instances of the words in the proper bins. So write a program that first has a series of bins

to

be

or

not

etc....

Because the word to occurs in positions 1 and 5, the bin for the word to will contain 1 and 5

to: 1 5

be: 2 6

or: 3

Write an program that will take the phrase, create bins for each word, and then record the instance position of each word in the bin. It will then output each bin (word) and the locations of the word in the phrase. So if the input was;

I want what I want and I know what I want.

The output would be;

I: 1 4 7 10

want: 2 5 11

what: 3 9

and: 6

know: 8

Explanation / Answer

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package cheggprograms;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
*
* @author Laasya
*/
public class Bins {
public static void main(String args[])
{
String str="";
int count=1;
String c[]=new String[30];
Scanner s =new Scanner(System.in);
String s1=s.nextLine();
String words[]=s1.split(" ");
Map repeat=new HashMap();
Map rep=new HashMap();
repeat.put(words[0], words[0]);
String abc=words[0]+" "+Integer.toString(1);
rep.put(words[0], abc);
c[0]=Integer.toString(1);
int check=0;
for(int i=1;i<words.length;i++)
{
for(int j=0;j<repeat.size();j++)
{
if(words[i].equals(repeat.get(words[j])))
{
check++;
}

}
if(check>0)
{
String s11=rep.get(words[i])+" "+Integer.toString(++count);
rep.put(words[i],s11);
//System.out.println(rep.get(words[i]));
}
else
{
repeat.put(words[i], words[i]);
String s11=words[i]+" "+Integer.toString(++count);
rep.put(words[i], s11);
// System.out.println(rep.get(words[i]));
}
  

}

for(String ss:words)
{
System.out.println(rep.get(ss));
}

  
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote