string buffer won't provide the reversed answer

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 1470 Views - Last Post: 09 December 2012 - 08:12 PM Rate Topic: -----

#1 lostinjawa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-December 12

string buffer won't provide the reversed answer

Posted 09 December 2012 - 12:18 PM

hi all
am fixing the debugin ex. from school fixed everything except to show the reversed array to show in output message. i am totally lost as what to do now ..

entry = JOptionPane.showInputDialog(null,		
        "Enter any word\n" +
        "Enter " + STOP + " when you want to stop"); 
      while(!(entry.equals(STOP))) 	// xxx quits 
      {
         array[x] = entry;							
			entry = JOptionPane.showInputDialog(null,
            "Enter another word\n" +
            "Enter " + STOP + " when you want to stop"); 
      }
      for( y = x; y >= 0; y--)
      {
         message.append(array[y]);
         message.append("\n");
      }
      JOptionPane.showMessageDialog(null, message);





any suggestion / comment / help would greatly

Is This A Good Question/Topic? 0
  • +

Replies To: string buffer won't provide the reversed answer

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1398
  • View blog
  • Posts: 3,097
  • Joined: 05-April 11

Re: string buffer won't provide the reversed answer

Posted 09 December 2012 - 12:23 PM

You never increase the variable 'x' in your while loop, don't you want to be doing that :) ? Else you are just override the previous value in the array
Was This Post Helpful? 0
  • +
  • -

#3 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: string buffer won't provide the reversed answer

Posted 09 December 2012 - 12:25 PM

Your design is flawed because you keep overrideing array[x] with different values. You need to increment x. Also, another flaw is that you dont perform enough checks. What if you keep incrementing x - but the user never types in "xxx" to quit? - you will get ArrayIndexOutOfBounds exception.
Was This Post Helpful? 0
  • +
  • -

#4 lostinjawa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-December 12

Re: string buffer won't provide the reversed answer

Posted 09 December 2012 - 01:13 PM

thanks for quick comments, i tried adding
  while(!(entry.equals(STOP))) 	// xxx quits 
      {
         array[x] = entry;							
			entry = JOptionPane.showInputDialog(null,
            "Enter another word\n" +
            "Enter " + STOP + " when you want to stop"); 
				array[x]++;



give error sayin its not statement
Was This Post Helpful? 0
  • +
  • -

#5 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1398
  • View blog
  • Posts: 3,097
  • Joined: 05-April 11

Re: string buffer won't provide the reversed answer

Posted 09 December 2012 - 01:26 PM

Increment x not array[x]

array[x] = entry;
x++;
//...



The variable x is the one keeping track at the current/next index needed to be filled in the array, so that is the one you should be changing
Was This Post Helpful? 1
  • +
  • -

#6 lostinjawa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-December 12

Re: string buffer won't provide the reversed answer

Posted 09 December 2012 - 01:33 PM

when i input some string it provide the outputs null. is it because its not recognizing what the userinput is ?
Was This Post Helpful? 0
  • +
  • -

#7 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: string buffer won't provide the reversed answer

Posted 09 December 2012 - 04:32 PM

Could you clarify what it is that you are trying to accomplish? Write out the steps in English.
Was This Post Helpful? 0
  • +
  • -

#8 lostinjawa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-December 12

Re: string buffer won't provide the reversed answer

Posted 09 December 2012 - 04:39 PM

//user input series of words and it displays them in reverse order.
right now am clueless and little frustrated as to why it displays null ...it should be displaying nam ( if the input was man)..
thank you
Was This Post Helpful? 0
  • +
  • -

#9 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: string buffer won't provide the reversed answer

Posted 09 December 2012 - 04:46 PM

You know that a reverse order is ambigous, dont you?
Reverse order as in:

java python -----> nohtyp avaj

or

java python ----> avaj nohtyp

??
Was This Post Helpful? 2
  • +
  • -

#10 lostinjawa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-December 12

Re: string buffer won't provide the reversed answer

Posted 09 December 2012 - 04:53 PM

I am sorry i don't get what you saying. am doing basic java 101.its an debug question from chapter Arrays...we are not there yet at reverse().

// Allows user to enter a series of words
// and displays them in reverse order
import javax.swing.*;
public class FDebugEight4
{
   public static void main(String[] args)
   {
      int x = 0, y;
      String[] array = new String[100];
      String entry;
      final String STOP = "XXX";
      StringBuffer message = new
          StringBuffer("The words in reverse order are\n");
     
      entry = JOptionPane.showInputDialog(null,		
        "Enter any word\n" +
        "Enter " + STOP + " when you want to stop"); 
      while(!(entry.equals(STOP))) 	//keep asking for word till XXX is inputed
		      {
         array[x] = entry;	
			x++;						
			entry = JOptionPane.showInputDialog(null,
            "Enter another word\n" +
            "Enter " + STOP + " when you want to stop"); 
				
      }
      for( y = x; y > 0 ; --y)
      {
         message.append(array[y]);
         message.append("\n");
      }
      JOptionPane.showMessageDialog(null, message);
   }
}




Was This Post Helpful? 0
  • +
  • -

#11 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: string buffer won't provide the reversed answer

Posted 09 December 2012 - 05:00 PM

Is that gonna work?


import java.util.Scanner;

public class MainClass
{
    public static void main(String[] args){
        StringBuilder sb = new StringBuilder(20);
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Please enter some stuff in here: >>>  ");
        
        
        String input = scanner.nextLine();
        sb.append(input);
        while(!input.equals("end")){
            System.out.println("Please enter some stuff in here: >>>  ");
            input = scanner.nextLine();
            sb.append(input);
        }
        
        System.out.println("Printing the reverse: \n\n");
        
        System.out.println(sb.reverse().toString());
        
        
    }
}



Was This Post Helpful? 1
  • +
  • -

#12 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1398
  • View blog
  • Posts: 3,097
  • Joined: 05-April 11

Re: string buffer won't provide the reversed answer

Posted 09 December 2012 - 05:05 PM

Your loop is wrong
Since you do x++ before exiting, you will need to start at x-1 because that is where the last entry is placed
Arrays start at 0, so you should keep looping until y < 0
		for (y = x-1; y >= 0; --y) {


Was This Post Helpful? 0
  • +
  • -

#13 ccdan  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 39
  • Joined: 09-December 12

Re: string buffer won't provide the reversed answer

Posted 09 December 2012 - 05:08 PM

It almost does the trick, although there are some unnecessary things in there...

hint: you're counting the number of elements in x... so x is the total number of element... the index goes from 0 to the number of the last element - 1 :) Do you get it now? :)
Was This Post Helpful? 0
  • +
  • -

#14 lostinjawa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-December 12

Re: string buffer won't provide the reversed answer

Posted 09 December 2012 - 05:18 PM

hey darek, she wants us to do as it was and just fix what ever is missing...thanks for suggestion .. and it does what she wants though..

thanks CasiOo.. only if i can get it in reverse ....i tried it before but i guess i didn't do it in combination of >=

ccdan .. i tried with x.length() -1...but it gives me error at (.)

View Postlostinjawa, on 09 December 2012 - 05:16 PM, said:

hey darek, she wants us to do as it was and just fix what ever is missing...thanks for suggestion .. and it does what she wants though..

thanks CasiOo.. only if i can get it in reverse ....i tried it before but i guess i didn't do it in combination of >=

ccdan .. i tried with x.length() -1...but it gives me error at (.) saying int cannot be dereferenced

Was This Post Helpful? 0
  • +
  • -

#15 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1398
  • View blog
  • Posts: 3,097
  • Joined: 05-April 11

Re: string buffer won't provide the reversed answer

Posted 09 December 2012 - 05:21 PM

Nobody understands what you are having trouble with, and we don't know what your code is supposed to do

If the user inputs
"Hello"
"World!"
"XXX"

What would the expected output be?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2