[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 247
  • Last Modified:

Best way to get text from a Jtext area and send it in a DataGram packet

I can't seem to get past a compile error of
 incompatible types
 found byte[]
required java.lang.Byte[]
Maybe I need to rework everything, I feel like I'm doing too many conversions of the text
from a string to bytes to char, there must be a better way, although I guess DatagramPacket() is defined as
DatagramPacket(byte[] buf, int length, InetAddress address, int port)  so I do need the text converted to a byte[]
Here's my code:
I have a Jtext area and made a dataStructure to hold the data from there and also a few integers, which
the values will be generated depending on what radio buttons are selected on another part of the GUI.

My data structure to hold the jText text and radio button selections
public class guiPacket
{
  public char [] text = new char[256];
  RadioData [] radioData = new RadioData[25];
  public class guiPacket(){}
   public class RadioData
   {
    int radioSelected;
    int radioLevel;
    public RadioData(){}
   }
  public byte [] packageText()
  {
    try
    {
      ByteArrayOutputStream textArray = new ByteArrayOutputStream();
      DataOutputStream out = new DataOutputStream(textArray);
      out.write(new String(text).getBytes("UTF-8"));
      return textArray.toByteArray();
    }
   catch(IOException e)
    {
      return null;
    }
}

I have a send Jbutton below my text area, which is supposed to trigger the UDP packet to be
filled with textArea data and sent to another program, my callback function for the JButton

public void sendTextAreaDataActionPerformed(java.awt.event.ActionEvent evt)
{
  String textAreaString = textArea.getText();
  guiPacket.text = textAreaString.toCharArray();
  newTextPacket = true;
//newTextPacket is a boolean declared in my top level app class and is used in a server thread to                           //package and send the UDP packet when when updated to true
}

my run function for my server thread

public void run( )
{
  while(true)
   {
       if(newTextPacket)
       {
         Byte [] byteArray = guiPacket.packageText();
//THIS IS WHERE MY ERROR HAPPENS!!! on the call to guiPacket.packageText();
//the function is defined to return byte [] via    return textArray.toByteArray();
// incompatible types
// found byte[]
//required java.lang.Byte[]


         dataPacket = new DatagramPacket(byteArray,byteArray.length,address,serverPort);
         socket.send(dataPacket);
       }
   }

}










0
mitchguy
Asked:
mitchguy
  • 4
  • 3
  • 2
2 Solutions
 
TimYatesCommented:
change

         Byte [] byteArray = guiPacket.packageText();

to

         byte [] byteArray = guiPacket.packageText();
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Byte is a wrapper class for byte - you cannot directly type-cast a byte[] to a Byte[]
0
 
mitchguyAuthor Commented:
I was just about to update that I changed Byte to byte and got it to compile
writing out everything pointed out the obvious to me, But I still feel like I'm doing things the hard way,
maybe doing too many steps, is there a better way to do what I'm doing?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
TimYatesCommented:
Instead of having a global (static?) boolean to flag when there's some text to be sent, why not just add the bytearray to a Queue, and have the Datagram sending code sending data when there's something in the queue?

It really depends on wht you are doing and how you are doing it...

You need a byte array, and you are converting the string into one in the best way I can think of...

Tim
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> public char [] text = new char[256];

- is public. It should be private and you should expose it through an accessor (getter) method.

>> RadioData [] radioData = new RadioData[25];

can also be made private similarly.
0
 
mitchguyAuthor Commented:
>>Tim
I'm always interested in writing better code, I definitely would like to learn
what your method is for replacing the global boolean flag, can you give me
a code example of what you mean and an explanation of why it's a better way to do it?
0
 
TimYatesCommented:
basically, you'd be looking at implementing a Queue (or a java.util.List if you are not on Java 5)

And then you'd be looking at the producer/consumer model

http://java.sun.com/docs/books/tutorial/essential/threads/synchronization.html

So your Swing app would be the producer and it would add stuff to the list, and your Datagram thread would be the consumer, which would send these out as they arrived in the queue

Tim
0
 
TimYatesCommented:
As I said though, this might be totally unneccessary complication of your project though...

The only concern I have with your code currently is what happens if the user enters two packets of data in quick succession before the Datagram run() method gets a chance to fire?

I guess one of the packets will be lost?

Tim
0
 
mitchguyAuthor Commented:
In this particular instance, I think I'm ok, because the user has to type in the text and then
hit send so i don't think there's much of a chance of them typing fast enough to send another
one before the first one is processed, but I do see how easily this could be a problem, so I think I'll implement it for the sake of learning it, besides better safe than sorry, maybe it is applicable to this
program.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now