How do I detect a newline in a string? In my code I cannot find "\r\n" check out my snipitty snippit

edited November 2013 in How To...
 /*****************************************\
 >* viewport class that I can re-use  :D *<
 \*****************************************/

class outputbox{

  int x,y,w,h,text_height,linecount; 

  PImage img; 
  PFont font;
  String string;

  outputbox(int opbx,int opby,int opbw,int opbh){
    x = opbx;
    y = opby;
    w = opbw;
    h = opbh;

  }

  void blit(String in){


    if(in.charAt(in.length()-1)=='//'){
      if(in.charAt(in.length()-2)=='n'){
        linecount++;
      }
    }
    PGraphics buffer;
    buffer = createGraphics(w,h,JAVA2D);
    buffer.beginDraw();
    buffer.fill(0,255,0);
    buffer.text(in,10,10);
    buffer.text(linecount,40,200);
    buffer.endDraw();
    img = buffer.get(0,0,w,h);
  }

So the aim here is to make a re-usable viewport class, I was going to build this up so that I could keep getting input into a viewport object and there would be a little scrollbar to the side of the viewport and it would all by very dynamic and easily re-usable, but in order to get a rect to display the bottom few lines of the text on the buffer as the image I need to count the lines.

If I could count the lines then I would be able to calculate the texts total height in its box (also using text decent + ascent...and maybe font size all added together, havent got that far yet) and know what part to display, I could make up scrolling super smooth.

Also I would like to know If this is a bad way of doing it and that I am creating loads and loads of buffers rather than updating the same one...maybe I would get overlap if I used two viewports? never used buffer before...

Other than using a GUI how can I build this??

-Devonrevenge

Answers

  • edited November 2013
    if(in.charAt(in.length()-1)=='//'){  
      if(in.charAt(in.length()-2)=='n'){
    

    Ouch! First, you use a doubled slash instead of a doubled backslash. Since this won't compile, I suppose it is a typo in the message or something.

    Second, you don't seem to understand what the \n notation means... In Java (and lot of other programming languages), the \x notation is a shortcut to denote a control character, the backslash having a special meaning here (it is an escape sequence).

    The compiler just see these two characters and generate instead only one character, the newline character. So a correct check would be:

    if (in.charAt(in.length()-1) == '\n') {  
    

    Not sure if it solves your main problem, but I hope it helps.

  • Oh wow thats really really good, so thats how I would do it.

    onnoyingly now, when the data stops coming in the new lines just shoot up... oh wait, thats cos its stuck on the last line :)

    Thank you, thats one problem solved that is!

Sign In or Register to comment.