Jump to content




Center Text Program - (Help!)


  • You cannot reply to this topic
8 replies to this topic

#1 ColbiesTheName

  • New Members
  • 1 posts

Posted 08 June 2016 - 09:42 PM

Quick info first - I quit playing minecraft for a while in favor of Garry's Mod. There I programmed with E2, which is sort of similar to LUA. Anyway, I came back to minecraft (specifically because a server i used to play on changed packs), and I've started doing CC again.

I can't figure out wtf is wrong with my code and I need help.
I'm probably just making a noob mistake, but could someone help me out?
while (side==nil) do
  term.clear()
  term.setCursorPos(1,1)
  term.write("Center Text Program, made by ColbiesTheName.")
  term.setCursorPos(1,3)
  term.write("Enter the side your monitor is touching this computer.")
  term.setCursorPos(1,4)
  term.write("(top, bottom, left, right, back, front)")
  side = read()
  os.sleep(0.5)
end

while (text==nil) do	--The error is happening on this line.
  term.clear()
  term.setCursorPos(1,1)
  term.write("Center Text Program made by ColbiesTheName.")
  term.setCursorPos(1,3)
  term.write("Please enter the text you would like to be written.")
  text = read()
  os.sleep(0.5)
end

local function centerText(text)
  mon = peripheral.wrap(side)

  if (mon == nil) do
	term.clear()
	term.setCursorPos(1,1)
	term.write("Incorrect side! Please restart this program.")
	os.sleep(10)
  else do

	sizex,sizey = mon.getScale()
	textlength = text.len()
	writex = math.floor(((sizex / 2) - (textlength / 2)))
	writey = math.floor(((sizey / 2) - 1))
	mon.setCursorPos(writex,writey)
	mon.write(text)
  end
end
It says "bios:14: [string "centerText"]:28: 'then' expected" and I have no idea why. There shouldn't be a need for a 'then' at line 14 (I marked where it is in the code.)

#2 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 09 June 2016 - 12:22 AM

Hey ColbiesTheName,

Welcome to the CC forums :)

The error statement you provided says the problem is on line 28, not where you think it's happening. However, in your case, the error is actually elsewhere...
Your if statements are malformed. They should read as if/then not if/do. Also your 'else do' should be an 'else'.

Edited by Dog, 09 June 2016 - 12:30 AM.


#3 ReBraLaCC

  • Members
  • 100 posts
  • LocationSublime Text 2

Posted 09 June 2016 - 11:45 AM

I don't get it why you are getting textlength of a nil value, i would say use "textlength = #text" # returns the length of the input..

Never used monitors that much to be honest, need to get into them :)

#4 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 09 June 2016 - 02:49 PM

ReBraLaCC brings up a good point. textlength = text.len() should be textlength = text:len() - note the colon instead of the dot. You could also use string.len(text) (note the dot in this case). Another way to do it would be, as ReBraLaCC mentioned - textlength = #text

Either way will get you the same result.

Edited by Dog, 09 June 2016 - 02:54 PM.


#5 TheRockettek

  • Members
  • 547 posts
  • LocationRem is best girl

Posted 09 June 2016 - 04:18 PM

XD alot of ways to just find the length of a string

#6 ReBraLaCC

  • Members
  • 100 posts
  • LocationSublime Text 2

Posted 11 June 2016 - 09:09 PM

And if you need a text centering function look at this http://www.computerc...intu-paint-api/ Its an API that could be very usefull!

#7 The_Cat

  • Members
  • 119 posts

Posted 12 June 2016 - 08:36 AM

View PostTheRockettek, on 09 June 2016 - 04:18 PM, said:

XD alot of ways to just find the length of a string

I mean there is another I could think off ;)
text = "Hello"
cursorX, __ = term.getCursorPos()
write(text)
cursorXAfter, _ = term.getCursorPos()
stringLength = cursorXAfter - cursorX
Would NOT recommend. :D

#8 ReBraLaCC

  • Members
  • 100 posts
  • LocationSublime Text 2

Posted 12 June 2016 - 08:57 AM

View PostThe_Cat, on 12 June 2016 - 08:36 AM, said:

text = "Hello"
cursorX, __ = term.getCursorPos()
write(text)
cursorXAfter, _ = term.getCursorPos()
stringLength = cursorXAfter - cursorX
Would NOT recommend. :D/>

Don't you need to do -1 also then? I can't test it so ;)

#9 The_Cat

  • Members
  • 119 posts

Posted 12 June 2016 - 09:20 AM

View PostReBraLaCC, on 12 June 2016 - 08:57 AM, said:



Nope, I tested it. :P





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users