I have been given a homework this is it:
Homework 7
Due in your lab in week 10
For this homework you are expected to write one program, which consists of a main method and an insertWord() method.
You must work on the program outside the formal laboratory sessions. The program must be ready to be executed at the start of the scheduled laboratory session.The problem
A sentence can be thought of as one or more words which are delimited by spaces.For example: “A sentence is constructed with a series of words”
The first word is ‘A’ The second word is ‘sentence’
There are nine words in totalJava has a standard to index elements starting with 0. So the first word would be at position 0, the second word would be at position 1, etc
“A sentence is constructed with a series of words”
Index 0 1 2 3 4 5 6 7 8
We need to provide a method which will insert a word into a string. The method will take the sentence to be altered, the word to be inserted and the word index location to place the word. The method will return the result as a String.
Method signature:
public static String insertWord(String sentence, String word, int position)
Example program output:
String word = “NEW”;
String sentence = “A sentence is constructed with a series of words”;String one = insertWord(sentence,word,0);
// one would contain: “NEW A sentence is constructed with a series of words”String two = insertWord(sentence,word,3);
// two would contain: “A sentence is NEW constructed with a series of words”String three = insertWord(sentence,word,9);
// two would contain: “A sentence is constructed with a series of words NEW”// in cases where the range of values is outside the possible index range,
// i.e. less than 0 or greater than the index needed for adding the word
// to the end the string. The methods should in return the string unaltered.String two = insertWord(sentence,word,-1);
// two would contain: “A sentence is constructed with a series of words”String three = insertWord(sentence,word,10);
// two would contain: “A sentence is constructed with a series of words”
Hint:
There are two possible solutions to the problem:Solution one involves looking at the methods of the string class, such as charAt and substring.
The other solution would be to use the scanner class, as discussed during the lectures.
MarkingMarking will test the behaviour of the method. If your method is not able to return the sting you will get a maximum of 2 marks. Your program does not have to deal with situations where the input string does not contain at least one word.
Mark Criteria
1 Inserting a word to index position 0
1 Inserting a word in the middle of the string
1 Inserting a word to the end of the string
1 String unaltered if negative index
1 String unaltered if index exceeds limit.
I have chosen to attack this problem using the scanner class. The problem I have is at runtime just the word "word" is printed loads and loads of times and then an error message which is very long and I dont understand prints out.
This is my code
import java.util.Scanner;
class HomeworkSeven
{
public static void main (String [] args)
{
String word = "new";
int position = 0;
String sentence = "A sentence is constructed with a series of words";
System.out.println(insertWord(sentence,word, position));
}
public static String insertWord(String sentence, String word, int position)
{
word = "NEW";
position = 0;
sentence = "A sentence is constructed with a series of words";
Scanner myScanner = new Scanner(sentence);
while (myScanner.hasNext())
{
word = myScanner.next();
position++;
}
for (int x = 0 ; x < position ; x++)
System.out.println(word);
return insertWord(sentence,word, position);
}
}
Any pointing in the right direction would be appreciated.