473,388 Members | 1,256 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

java help very ergant

i have assigned several variables in one method and i want to use them in another method. that works ok until i call the second method. i get java.lang.string error

heres my work....

Expand|Select|Wrap|Line Numbers
  1.  /** 
  2. Add comment here.
  3. */
  4. public class WordChaining {
  5.  
  6. //--------------------------------------------------------------------------
  7. //------------------ ORGANISE GAME UNTIL PLAYER WISHES TO QUIT -------------
  8. //--------------------------------------------------------------------------
  9.     public void start() {
  10.  
  11.         boolean notFinished = true;
  12.  
  13.         while ( notFinished ) {
  14.             playOneRound();
  15.  
  16.             if ( userWantsToStop() ) {
  17.                 notFinished = false;
  18.             }
  19.         }
  20.  
  21.         System.out.println( "BYE" );
  22.     }
  23.  
  24. //--------------------------------------------------------------------------
  25. //------------------ PLAY ONE ROUND ----------------------------------------
  26. //--------------------------------------------------------------------------
  27.     private void playOneRound(){
  28.             String randomWord;
  29.             String input;
  30.             String chain;
  31.  
  32.             introductoryMessage();
  33.             randomWord = (displayRandomWord());
  34.             System.out.println(randomWord);
  35.             input = (promptUser());
  36.             input = input.toUpperCase();
  37.             testInput1();
  38.  
  39.  
  40.  
  41.         /**    if (randomWord.length() == input.length() && (!randomWord.equals(input))){
  42.                 System.out.println(true);
  43.             }else{
  44.                 System.out.println(false);
  45.             }
  46.  
  47.  
  48.             int index = 0;
  49.             int length = input.length();
  50.  
  51.  
  52.  
  53.             while (index < length){
  54.                 char letter = randomWord.charAt(index);
  55.                 char letter2 = input.charAt(index);
  56.                     if (letter == letter2){
  57.                         System.out.println("YES");
  58.                     }else{
  59.                         System.out.println("NO");
  60.                     }
  61.                 index++;
  62.             }
  63.  
  64. **/
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.             displayChain();
  75.             chain = (randomWord + " " + input);
  76.             chain = chain.toUpperCase();
  77.             System.out.println(chain);
  78.             randomWord = input;
  79.  
  80.  
  81.  
  82.  
  83.     }
  84.  
  85.  
  86.  
  87.  
  88. //--------------------------------------------------------------------------
  89. //------------------ OBTAIN A RANDOM WORD ----------------------------------
  90. //--------------------------------------------------------------------------
  91.     private String getRandomWord( ) {
  92.          String theWord = Words.getRandomWord();
  93.             return theWord.toUpperCase();
  94. }
  95.  
  96. //--------------------------------------------------------------------------
  97. //------------------ INTRODUCTORY MESSAGE ----------------------------------
  98. //--------------------------------------------------------------------------
  99. private void introductoryMessage() {
  100.             System.out.println("Each new word in the chain can have only one letter different from the previous word in the chain.");
  101.             System.out.println("");
  102.             System.out.println("");
  103.     }
  104.  
  105.  
  106.  
  107. //--------------------------------------------------------------------------
  108. //------------------ DISPLAY RANDOM WORD -----------------------------------
  109. //--------------------------------------------------------------------------
  110.     private String displayRandomWord() {
  111.             System.out.print("The current wordchain: ");
  112.             String randomWord = new String (getRandomWord());
  113.             return randomWord;
  114.     }
  115.  
  116.  
  117. //--------------------------------------------------------------------------
  118. //------------------ PROMPT USER -------------------------------------------
  119. //--------------------------------------------------------------------------
  120.     private String promptUser() {
  121.             System.out.println("Enter next word");
  122.             System.out.print("(press Enter to stop current chain): ");
  123.             String input = new String (Keyboard.readInput());
  124.             return input;
  125.     }
  126.  
  127.  
  128.  
  129.  
  130. //--------------------------------------------------------------------------
  131. //------------------ DISPLAY CHAIN -----------------------------------------
  132. //--------------------------------------------------------------------------
  133.     private void displayChain() {
  134.             System.out.println("");
  135.             System.out.print("The current word chain: ");
  136.     }
  137.  
  138. //--------------------------------------------------------------------------
  139. //------------------ TEST INPUT --------------------------------------------
  140. //--------------------------------------------------------------------------
  141.     private boolean testInput1(){
  142.             String randomWord;
  143.             String input;
  144.  
  145.             int position = 0;
  146.             int length = input.length();
  147.             int counter = 0;
  148.         /**    if (randomWord.length() == input.length() && (!randomWord.equals(input))){
  149.                 System.out.println("True");
  150.                 return true;
  151.             }else{
  152.                 System.out.println("False");
  153.                 return false;
  154. **/
  155.  
  156.  
  157.             while (position < length){
  158.                 char letter = randomWord.charAt(position);
  159.                 char letter2 = input.charAt(position);
  160.                     if (letter == letter2){
  161.                         System.out.println("True");
  162.                         counter++;
  163.                     }else{
  164.                         return false;
  165.                     }
  166.                 position++;
  167.             }
  168.  
  169.         System.out.println(counter);
  170.         return true;
  171.     }
  172.  
  173. //--------------------------------------------------------------------------
  174. //------------------ userWantsToStop ---------------------------------------
  175. //--------------------------------------------------------------------------
  176.  
  177.     private boolean userWantsToStop() {
  178.      System.out.print( "Press Enter to continue, any other key to finish: " );
  179.      String userString = Keyboard.readInput();
  180.      if ( userString.equals( "q" ) ) {
  181.              return false;
  182.      }
  183.      return true;
  184.     }
  185.  
  186.  
please help me if u can
Apr 2 '06 #1
2 2282
Banfa
9,065 Expert Mod 8TB
I can't actually see where the probelm lies. You have not given much detail in you problem description. Where are the variables assigned (did you really mean assigned or did you mean delared), which bit of the code is the bit that goes wrong?
Apr 3 '06 #2
hi there

if u have problem calling testInput1 method as it calls variables randomword and input strings.

Then u might delcare them just after u open ur class and outside any function and it becomes accessible to all the functions

Hope this solves ur problem
Jul 10 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

114
by: Maurice LING | last post by:
This may be a dumb thing to ask, but besides the penalty for dynamic typing, is there any other real reasons that Python is slower than Java? maurice
55
by: Elijah | last post by:
I have read many of the topics on learning C++ or Java first. It seems like everyone says something different. I would like to know if I should learn C++ or Java. First a little about myself. I...
30
by: Rhino | last post by:
I am giving some thought to applying for some jobs that want people with Java and C++ experience. I have been writing Java for several years and am fluent enough that I don't have to get help with...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
458
by: wellstone9912 | last post by:
Java programmers seem to always be whining about how confusing and overly complex C++ appears to them. I would like to introduce an explanation for this. Is it possible that Java programmers...
15
by: RAM | last post by:
Hello, I graduated computer science faculty and decided to became a programmer. Please help me to make a decision: Java or Microsoft .NET? What is the future of Java? Thanks! /RAM/
5
by: r035198x | last post by:
Setting up. Getting started To get started with java, one must download and install a version of Sun's JDK (Java Development Kit). The newest release at the time of writting this article is...
29
by: s0suk3 | last post by:
Hello, I was hoping to get some opinions on a subject. I've been programming Python for almost two years now. Recently I learned Perl, but frankly I'm not very comfortable with it. Now I want to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.