User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 455,982 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,776 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 798 | Replies: 9 | Solved
Reply
Join Date: Jul 2007
Posts: 53
Reputation: piers is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
piers piers is offline Offline
Junior Poster in Training

scannerClassAndSentenceStructure

  #1  
Nov 27th, 2007
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 total

Java 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.
Marking

Marking 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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 3,076
Reputation: Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold 
Rep Power: 15
Solved Threads: 306
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Sensei

Re: scannerClassAndSentenceStructure

  #2  
Nov 27th, 2007
You are merely scanning all the words and incrementing the position, but you don't do anything when you get to the position where the insertion is to take place. The for loop is just printing the last word found by the scanner "number of words" times.

You are also returning a recursive call back into the same function, which is definitely not what you want to do. You need to return the new sentence string (which you haven't created).
Last edited by Ezzaral : Nov 27th, 2007 at 3:10 pm.
Reply With Quote  
Join Date: Jul 2007
Posts: 53
Reputation: piers is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
piers piers is offline Offline
Junior Poster in Training

Re: scannerClassAndSentenceStructure

  #3  
Nov 27th, 2007
okay I have made a revision of my code. I know nothing is being outputted in my main class but have I created the new sentence? this one is a real head scratcher for me.

import java.util.Scanner;
class HomeworkSeven
{
public static void main (String [] args)
{
String word = "new";
String one = "A";
String two = "sentence";
String three = "is";
String four = "constructed";
String five = "with";
String six = "a";
String seven = "series";
String eight = "of";
String nine = "words";
int position = 0;
String sentence;
System.out.println();

}


public static String insertWord(String sentence, String word, int position)
{
String one = "A";
String two = "sentence";
String three = "is";
String four = "constructed";
String five = "with";
String six = "a";
String seven = "series";
String eight = "of";
String nine = "words";
position = 0;
sentence = one+two+three+four+five+six+seven+eight;
Scanner myScanner = new Scanner(sentence);
sentence = word + sentence;
while (myScanner.hasNext())
{
word = myScanner.next(word);
position++;
}
for (int x = 0 ; x < position ; x++)
System.out.println(sentence);


return sentence;
}


}

Reply With Quote  
Join Date: Jul 2007
Posts: 53
Reputation: piers is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
piers piers is offline Offline
Junior Poster in Training

Re: scannerClassAndSentenceStructure

  #4  
Nov 28th, 2007
I decided to take the other approach with this cus I cant use the scanner class to save my life. So instead I have decided to use substring. My thought it if I separate a string in two then I can separate it anywhere along the string and insert a word and put the different parts together. So I have got my substring command but when I tested it splits the string but prints out both parts of the split string. I only want to print out the substring part I have chosen IE the part which is between the numbers I have put next to the substring.

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".substring(0,2);
	    position = 0;
		sentence = "A sentence is constructed with a series of words".substring(2,48);
	    //System.out.println(sentence);
	  String  compound1 ="A sentence is constructed with a series of words".substring(0,14)+"New";
	  System.out.println(compound1); 
	  String  compound2 ="A sentence is constructed with a series of words".substring(13,47);
	

	
	return sentence;
	}


}
Reply With Quote  
Join Date: Nov 2007
Posts: 1
Reputation: 5671234 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
5671234 5671234 is offline Offline
Newbie Poster

Re: scannerClassAndSentenceStructure

  #5  
Nov 30th, 2007
Piers, i'd probably be abit more careful about where you post your homework. It's the first the link in google when you type java scanner class.
You've been warned.
Love northumbria university.
Last edited by 5671234 : Nov 30th, 2007 at 11:21 am.
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 7,009
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 25
Solved Threads: 368
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: scannerClassAndSentenceStructure

  #6  
Dec 1st, 2007
Someone's in deep deep trouble. ;-)
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: Jul 2007
Posts: 53
Reputation: piers is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
piers piers is offline Offline
Junior Poster in Training

Re: scannerClassAndSentenceStructure

  #7  
Dec 1st, 2007
so stupid. I know what help I can get for my homeworks. I dont know who that was who sent that. I always state if its a homework. There is a level of help you can get. So long as I am not plagerising then it is fine. I post homeworks on here for help like I get off my lecturers. I usually get a faster response on here.
Reply With Quote  
Join Date: Aug 2007
Location: Adelaide, South Australia
Posts: 428
Reputation: darkagn will become famous soon enough darkagn will become famous soon enough 
Rep Power: 3
Solved Threads: 53
darkagn's Avatar
darkagn darkagn is offline Offline
Posting Pro in Training

Re: scannerClassAndSentenceStructure

  #8  
Dec 1st, 2007
Hi piers,

The problem is that you posted a huge chunk of code that any of your fellow students could copy easily by searching for "scanner" in google. I think that is what the person is implying (possibly one of your professors?) Next time maybe just post the relevant code?
Reply With Quote  
Join Date: Jul 2007
Posts: 53
Reputation: piers is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
piers piers is offline Offline
Junior Poster in Training

Re: scannerClassAndSentenceStructure

  #9  
Dec 1st, 2007
okay I didnt think of that. thnx. I will bare that in mind.
Reply With Quote  
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: scannerClassAndSentenceStructure

  #10  
Dec 2nd, 2007
> I dont know who that was who sent that.

Maybe it was your boyfriend, after all why else would he sign it off with Love northumbria university.
Last edited by iamthwee : Dec 2nd, 2007 at 8:56 am.
... the hat of 'is this a cat in a hat?'
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Java Forum

All times are GMT -4. The time now is 9:21 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC