Results 1 to 8 of 8
- 01-01-2011, 05:36 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 18
- Rep Power
- 0
Keep on getting one error or the other
I've been either getting NoSuchElementException or NullPointerException errors when working with this code. Right now, I left it so that I have NoSuchElementException errors.
For this program, I am getting a sentence input from the user, and for every word that has "ui" in it, I want to replace it with a 'w'. (instead of "quick", I need to have "qwick" as a result.
Can anyone help me understand this?
These are the error messages I get:Java Code:import java.io.*; import java.util.*; public class LetterChange { public static void main (String [] args) throws IOException { BufferedReader objReader = new BufferedReader (new InputStreamReader (System.in)); System.out.println ("Enter a sentence to change, enter 'exit' to exit:"); String input = objReader.readLine(); String result = ""; String test= input; LetterSwitch changed = new LetterSwitch (test, result);// error here while (!input.equals("exit")) { System.out.println(changed.getResult()); System.out.println ("Enter a sentence to change, enter 'exit' to exit: "); input = objReader.readLine(); } if (input.equals("exit")) { System.out.println("End."); } } } class LetterSwitch { private String test; private String result= ""; LetterSwitch(String subject, String differ) { test = subject; result = differ; addLetter (test); // error here } public void addLetter (String test) { StringTokenizer line = new StringTokenizer (test); StringBuffer changes = new StringBuffer (); while (line.hasMoreTokens()) { String word= line.nextToken(); int x = word.indexOf('u'); if (word.charAt(x + 1) == 'i' ) { word = word.replaceFirst ("ui", "w"); } else { word = word; } } changes.append(line.nextToken()); //error on this line result = changes.toString(); } String getResult() { return result; } }
java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(Unknown Source)
at LetterSwitch.addLetter(LetterChange.java:59)
at LetterSwitch.<init>(LetterChange.java:38)
at LetterChange.main(LetterChange.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
at java.lang.reflect.Method.invoke(Unknown Source)
- 01-01-2011, 06:36 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
While there are more tokens, get next token. When there are no more tokens, get the next token. See what you did there?Java Code:while (line.hasMoreTokens()) { String word= line.nextToken(); int x = word.indexOf('u'); if (word.charAt(x + 1) == 'i' ) { word = word.replaceFirst ("ui", "w"); } else { word = word; } } changes.append(line.nextToken()); //error on this line result = changes.toString();
EDIT: Also, that else is totaly extraneous, what is word = word supposed to acomplish? What happens if you come across a text like this:
there is nothing for u
Take a look at your method and see what happens when you hit the single letter word "u".Last edited by m00nchile; 01-01-2011 at 06:39 PM.
Ever seen a dog chase its tail? Now that's an infinite loop.
- 01-01-2011, 08:44 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 18
- Rep Power
- 0
Ok, thanks for pointing that out.
Unless the character after the u is an i, the word should remain the same. I guess it's not necessary?
And that gets a StringIndexOutOfBounds error, should I change the else statement then?
- 01-01-2011, 09:10 PM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Yes, delete the else statement altogether. Also, read up on the indexOf() method, specificaly what it returns if the character was not found in the string. Also, a simple modification to your
if statement takes care of the string index out of bounds exception as well.Java Code:if ([B]x < word.length() &&[/B] word.charAt(x + 1) == 'i')
Ever seen a dog chase its tail? Now that's an infinite loop.
- 01-02-2011, 12:45 AM #5
Member
- Join Date
- Nov 2010
- Posts
- 18
- Rep Power
- 0
Thanks for your help, I appreciate it. :)
I tried changing the if statement, but I got the index out of bounds error again when I use 'u' as a single character in my input. So I'm a bit confused now.
Here are the error messages I got:
java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(Unknown Source)
at LetterSwitch.addLetter(LetterChange.java:50)
at LetterSwitch.<init>(LetterChange.java:38)
at LetterChange.main(LetterChange.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
at java.lang.reflect.Method.invoke(Unknown Source)
EDIT: I fixed my method so I'm not getting this error anymore. Can anyone help me understand why I'm getting the other errors?Last edited by wizar; 01-02-2011 at 02:01 AM.
- 01-02-2011, 06:01 AM #6
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
What errors are you still getting?
Ever seen a dog chase its tail? Now that's an infinite loop.
- 01-02-2011, 03:21 PM #7
Member
- Join Date
- Nov 2010
- Posts
- 18
- Rep Power
- 0
The errors I originally had in my first post.
My bad, I should have been more specific.
Thanks again for your help. :)
- 01-03-2011, 12:11 AM #8
Member
- Join Date
- Nov 2010
- Posts
- 18
- Rep Power
- 0
I am currently working with adding a set method, which seems to eliminate the errors. I'm just having problem with getting an output.
Is adding a set method wrong, or is something wrong with the code without the set method?
Here's the code I have now:
Java Code:import java.io.*; import java.util.*; public class LetterChange { public static void main (String [] args) throws IOException { BufferedReader objReader = new BufferedReader (new InputStreamReader (System.in)); System.out.println ("Enter a sentence to change, enter 'exit' to exit:"); String input = objReader.readLine(); String result = ""; String test= input; String word = ""; StringTokenizer line = new StringTokenizer(test); LetterSwitch changed = new LetterSwitch (test, result, word); changed.setWord(line.nextToken()); while (!input.equals("exit")) { System.out.println(changed.getResult()); System.out.println ("Enter a sentence to change, enter 'exit' to exit: "); input = objReader.readLine(); } if (input.equals("exit")) { System.out.println("End."); } } } class LetterSwitch { private String test; private String result= ""; private String word; LetterSwitch(String subject, String differ, String term) { test = subject; result = differ; word = term; addLetter (test, result, word); } String setWord (String term) { String word = term; return word; } public void addLetter (String test, String result, String word) { StringTokenizer line = new StringTokenizer (test); StringBuffer changes = new StringBuffer (); word = line.nextToken(); while (line.hasMoreTokens()) { int x = word.indexOf('u'); if ((x < word.length()) && (word.charAt(x + 1) == 'i' )) { word = word.replaceFirst ("ui", "w"); } changes.append(line.nextToken()); } result = changes.toString(); } String getResult() { return result; } }Last edited by wizar; 01-03-2011 at 12:39 AM.
Similar Threads
-
java out of memory error-heap space error
By elsanthosh in forum NetBeansReplies: 4Last Post: 06-15-2010, 09:31 AM -
> Operator cannot be applied error and return incompatible types error
By corney_16 in forum New To JavaReplies: 1Last Post: 03-10-2010, 01:53 PM -
Thread: Error 500--Internal Server Error java.lang.NullPointerException
By jackdear44 in forum New To JavaReplies: 1Last Post: 12-05-2009, 07:28 AM -
java.lang.Error: Error opening DSound for capture
By NARs in forum NetworkingReplies: 1Last Post: 10-26-2009, 04:38 PM -
Diference Between compiler error Garbage collection and Runtime Error?
By makpandian in forum New To JavaReplies: 3Last Post: 01-23-2009, 08:53 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks