Solved

storing a string

Posted on 2003-12-01
16
266 Views
Last Modified: 2011-09-20
Im reading a string from a server, how can I store it in a variable before displaying it?, the code below just displays the string.

while( ( buffer = input.readLine() ) != null )
          display.appendText( "\n" + buffer ) ;
          }
0
Comment
Question by:eire_ireland
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 3
  • +2
16 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9853797
Just save it to a variable. Of course it will be overwritten each time in the loop

String savedString = buffer;
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9853800
What is it you're trying to achieve?
0
 
LVL 16

Accepted Solution

by:
imladris earned 50 total points
ID: 9853806
How about:

String store="";
while( (buffer=input.readLine())!=null)
{   store+=buffer;
    display.appendText("\n"+buffer);
}

This will put the whole input into the String named stored.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 92

Expert Comment

by:objects
ID: 9853916
String buffer = null ;
   StringBuffer todisplay = new StringBuffer();
        while( ( buffer = input.readLine() ) != null )
          todisplay.append( "\n" + buffer ) ;
          }
    display.setText(todisplay.toString());
0
 
LVL 92

Expert Comment

by:objects
ID: 9853942
use following if you want to append the read text:

display.appendText(todisplay.toString());
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9854036
It's already stored in a variable in your question.  It's stored in "buffer".
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9854106
If 'display' is a text component like a TextArea then it's stored anyway and you can get it back as follows:

String original = display.getText();
0
 
LVL 92

Expert Comment

by:objects
ID: 9854123
> It's stored in "buffer".

Not the entire string read.

> String original = display.getText();

That returns the string displayed in the text field, and not the string read in.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9858314
>>That returns the string displayed in the text field, and not the string read in.

The value, if taken just after the text field is set, is exactly the same as the value contained in the StringBuffer 'todisplay' that you suggested, which is redundant if 'display' is a text component of some kind, as the String is already buffered.
0
 

Author Comment

by:eire_ireland
ID: 9860487
Cheers bucks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9860594
What does type does 'display' refer to eire_ireland?
0
 

Author Comment

by:eire_ireland
ID: 9860673
A string, I dont think i know what u mean
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9860768
>>display.appendText()

'display' can't be of type String, as it does not have an appendText method
0
 

Author Comment

by:eire_ireland
ID: 9860801
well im using display.appendText, whats has this to do with anything anyway
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9860879
Never mind - i don't want to labour the point!
0
 

Author Comment

by:eire_ireland
ID: 9860926
Fair enough, I was worried there for a second that you were lookin for advice off me!!
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question