StringToken사용법좀 알려주세요
아빠몬
안녕하세요^^덕분에 많은것을 했어요...메세지 주고 받는데 까지 했어요^^서버랑!!ㅋㅋㅋ 감사드려요...
근데요 한가지 문제점이 있어요...서버에서 클라이언트 메세지를 짤라야 하거든여.......$chatting$[ADMIN]$고맙습니다$
이렇게 오면 고맙습니다만 출력되게 해야하는데요...
st = new StringTokenizer(received , $);
while (st.hasMoreTokens()){
s1 = st.nextToken();
s2 = st.nextToken();
s3 = st.nextToken();
s4 = st.nextToken();
이렇게해서 출력을 해보려하는데 에러가 나요..
Exception in thread Thread-3 java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:332)
at SignedAppletDemo.run(SignedAppletDemo.java:124)
at java.lang.Thread.run(Thread.java:595)
이런식으로요...
제소스입니다...조언 부탁드립니다...
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import javax.swing.table.*;
import javax.swing.border.*;
import java.applet.*;
import java.io.*;
import java.net.*;public class SignedAppletDemo extends Applet implements Runnable,ActionListener
{
JPanel jPanel1;
JPanel jPanel2;
JPanel jPanel3;
JTextArea jTextArea1;
JScrollPane jTextArea1_scroll;
JTextField jTextField1;
JButton jButton1;
JScrollBar jScrollBar1;
JButton jButton2;
JButton jButton3;
JButton jButton4;
JButton jButton5;
JButton jButton6;
JButton jButton7;
JButton jButton8;
JButton jButton9;
JButton jButton10;
JButton jButton11;
JButton jButton12;
JButton jButton13;
JButton jButton14;
JButton jButton15;
JButton jButton16;
JButton jButton17;
Socket socket;
InputStream is;
OutputStream os;
DataInputStream input;
BufferedReader in;
PrintWriter out;
String logid;
String logpw;
String ip;
String msg =;
StringTokenizer st;
String s1;
String s2;
String s3;
String s4;
private EnterKey enterKey = new EnterKey();public void init()
{
UIManager.put(swing.boldMetal, Boolean.FALSE);
setLayout(null);
//ip=JOptionPane.showInputDialog(this,아이피를 입력하세요);
logid=JOptionPane.showInputDialog(this,아이디 입력하세요);
logpw=JOptionPane.showInputDialog(this,비밀번호를 입력하세요);
makeComponent();
try{
socket = new Socket(127.0.0.1,8431);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
input = new DataInputStream(socket.getInputStream());
out = new PrintWriter(socket.getOutputStream());
out.println($login$ + logid + $ + logpw + $\0\n);
out.flush();
System.out.println($login$ + logid + $ + logpw + $);msg = in.readLine();
System.out.println(msg + \n);
if(msg.startsWith($login$id$)){
System.out.println(아이디가 틀렸데);
jTextArea1.append(야아이디 틀렸데);
socket.close();
}
else if(msg.startsWith($login$pw$)){
System.out.println(비밀번호가 틀렸다자나...);
jTextArea1.append(비밀번호 틀렸데);
socket.close();
}
else if(msg.startsWith($login$ok$)){
System.out.println(서버에 접속);
jTextArea1.append([ + logid + ] + 서버에 접속되었습니다\n);
}
new Thread(this).start();
}
catch(Exception j){
System.out.println(j.toString());
}
}public void run()
{
String received;
try{
while(true){
byte buffer[] = new byte[1024];
input.read(buffer);
received = new String(buffer);
received = received.trim();
st = new StringTokenizer(received , $);
while (st.hasMoreTokens()){
s1 = st.nextToken();
s2 = st.nextToken();
s3 = st.nextToken();
s4 = st.nextToken();
System.out.println([ADMIN] : + s1);
System.out.println([ADMIN] : + s2);
jTextArea1.append([ADMIN] : + received + \n);
}
}
}catch(IOException s){
System.out.println(s.toString());
}/*try{
msg = in.readLine();
while(in.readLine() != null) {
new Thread(this).start();
msg = in.readLine();
st = new StringTokenizer(msg , $);
s1 = st.nextToken();
p; s2 = st.nextToken();
s3 = st.nextToken();
s4 = st.nextToken();
jTextArea1.append(받은메세지 +s4 + \n);
System.out.print(s4 + \n);
}
}*/}
public void actionPerformed(ActionEvent e){
Object obj = e.getSource();
if(obj == jButton1){
String msg = jTextField1.getText();
jTextField1.setText();
out.println($chatting$[ADMIN]$ + msg + $\0\n);
out.flush();
}
else if (obj == jButton16){
out.println($login$ + logid + $ + logpw + $\0\n);
out.flush();
}
else if (obj == jButton17){
try{
System.out.println(접속해제되었습니다.소켓을 닫습니다.);
socket.close();
}catch(IOException s){}
}
else if (obj == jButton2){
out.println($robot$forword$\0);
out.flush();
}
else if (obj == jButton3){
out.println($robot$left$\0);
out.flush();
}
else if (obj == jButton4){
out.println($robot$right$\0);
out.flush();
}
else if (obj == jButton5){
out.println($robot$stop$\0);
out.flush();
}
else if (obj == jButton6){
out.println($robot$backword$\0);
out.flush();
}}
class EnterKey extends KeyAdapter{
public void keyPressed(KeyEvent e){
int code = e.getKeyCode();
if(code == KeyEvent.VK_ENTER){
String msg=jTextField1.getText();
out.println($chatting$[ADMIN]$ + msg + $\0\n);
out.flush();
jTextField1.setText();
}
}
}
public void destroy()
{
try{
out.close();
in.close();
is.close();
socket.close();
}
catch(IOException s){
}
}
public void makeComponent()
{
jPanel1 = new JPanel();
jPanel1.setLayout(null);
jPanel1.setFont(new Font(Dialog.plain, 0, 12));
jPanel1.setBackground(new Color(-5911066));
jPanel1.setBounds(4, 5, 926, 255);
add(jPanel1);
jPanel2 = new JPanel();
jPanel2.setLayout(null);
jPanel2.setFont(new Font(Dialog.bold, 1, 19));
jPanel2.setBackground(new Color(-1118482));
jPanel2.setBounds(23, 6, 235, 244);
jPanel2.setBorder(new TitledBorder(Robot Body));
jPanel1.add(jPanel2);
jPanel3 = new JPanel();
jPanel3.setLayout(null);
jPanel3.setFont(new Font(Dialog.bold, 1, 12));
jPanel3.setBackground(new Color(-1118482));
jPanel3.setBounds(265, 6, 342, 244);
jPanel3.setBorder(new TitledBorder(Web Camera));
jPanel1.add(jPanel3);
jTextArea1 = new JTextArea();
jTextArea1_scroll = new JScrollPane(jTextArea1);
jTextArea1.setText();
jTextArea1.setFont(new Font(Dialog.plain, 0, 12));
jTextArea1.setForeground(new Color(-13421773));
jTextArea1.setBackground(new Color(-1));
jTextArea1_scroll.setBounds(616, 5, 269, 169);
jPanel1.add(jTextArea1_scroll);
jTextField1 = new JTextField();
jTextField1.addKeyListener(enterKey);
jTextField1.setText();
jTextField1.setFont(new Font(Dialog.plain, 0, 12));
jTextField1.setForeground(new Color(-13421773));
jTextField1.setBackground(new Color(-1));
jTextField1.setBounds(615, 181, 222, 26);
jPanel1.add(jTextField1);
jButton1 = new JButton();
jButton1.addActionListener(this);
jButton1.setText(Send);
jButton1.setFont(new Font(Dialog.bold, 1, 12));
jButton1.setForeground(new Color(-13421773));
jButton1.setBackground(new Color(-9979928));
jButton1.setBounds(838, 181, 66, 27);
jPanel1.add(jButton1);
jScrollBar1 = new JScrollBar(1);
jScrollBar1.setMinimum(0);
jScrollBar1.setMaximum(100);
jScrollBar1.setBlockIncrement(10);
jScrollBar1.setUnitIncrement(1);
jScrollBar1.setBounds(883, 5, 17, 169);
jPanel1.add(jScrollBar1);
jButton2 = new JButton();
jButton2.addActionListener(this);
jButton2.setText(▲);
jButton2.setFont(new Font(Dialog.plain, 0, 18));
jButton2.setForeground(new Color(-10092544));
jButton2.setBackground(new Color(-1));
jButton2.setBounds(89, 28, 52, 52);
jPanel2.add(jButton2);
jButton3 = new JButton();
jButton3.addActionListener(this);
jButton3.setText(◀);
jButton3.setFont(new Font(Dialog.plain, 0, 18));
jButton3.setForeground(new Color(-10092544));
jButton3.setBackground(new Color(-1));
jButton3.setBounds(27, 92, 52, 52);
jPanel2.add(jButton3);
jButton4 = new JButton();
jButton4.addActionListener(this);
jButton4.setText(▶);
jButton4.setFont(new Font(Dialog.plain, 0, 18));
jButton4.setForeground(new Color(-10092544));
jButton4.setBackground(new Color(-1));
jButton4.setBounds(152, 92, 52, 52);
jPanel2.add(jButton4);
jButton5 = new JButton();
jButton5.addActionListener(this);
jButton5.setText(●);
jButton5.setFont(new Font(Dialog.plain, 0, 18));
jButton5.setForeground(new Color(-10092544));
jButton5.setBackground(new Color(-1));
jButton5.setBounds(89, 92, 52, 52);
jPanel2.add(jButton5);
jButton6 = new JButton();
jButton6.addActionListener(this);
jButton6.setText(▼);
jButton6.setFont(new Font(Dialog.plain, 0, 18));
jButton6.setForeground(new Color(-10092544));
jButton6.setBackground(new Color(-1));
jButton6.setBounds(89, 154, 52, 52);
jPanel2.add(jButton6);
jButton7 = new JButton();
jButton7.addActionListener(this);
jButton7.setText(▲);
jButton7.setFont(new Font(Dialog.plain, 0, 18));
jButton7.setForeground(new Color(-10092544));
jButton7.setBackground(new Color(-1));
jButton7.setBounds(95, 34, 52, 52);
jPanel3.add(jButton7);
jButton8 = new JButton();
jButton8.addActionListener(this);
jButton8.setText(◀);
jButton8.setFont(new Font(Dialog.plain, 0, 18));
jBunbsp;jButton8.setForeground(new Color(-10092544));
jButton8.setBackground(new Color(-1));
jButton8.setBounds(31, 95, 52, 52);
jPanel3.add(jButton8);
jButton9 = new JButton();
jButton9.addActionListener(this);
jButton9.setText(▶);
jButton9.setFont(new Font(Dialog.plain, 0, 18));
jButton9.setForeground(new Color(-10092544));
jButton9.setBackground(new Color(-1));
jButton9.setBounds(159, 95, 52, 52);
jPanel3.add(jButton9);
jButton10 = new JButton();
jButton10.addActionListener(this);
jButton10 .setText(●);
jButton10 .setFont(new Font(Dialog.plain, 0, 18));
jButton10 .setForeground(new Color(-10092544));
jButton10 .setBackground(new Color(-1));
jButton10 .setBounds(95, 95, 52, 52);
jPanel3.add(jButton10 );
jButton11 = new JButton();
jButton11.addActionListener(this);
jButton11.setText(▼);
jButton11.setFont(new Font(Dialog.plain, 0, 18));
jButton11.setForeground(new Color(-10092544));
jButton11.setBackground(new Color(-1));
jButton11.setBounds(95, 155, 52, 52);
jPanel3.add(jButton11);
jButton12 = new JButton();
jButton12.addActionListener(this);
jButton12.setText(ZOOM IN);
jButton12.setFont(new Font(Dialog.bold, 1, 12));
jButton12.setForeground(new Color(-16777216));
jButton12.setBackground(new Color(-9979928));
jButton12.setBounds(230, 58, 96, 31);
jPanel3.add(jButton12);
jButton13 = new JButton();
jButton13.addActionListener(this);
jButton13.setText(ZOOM OUT);
jButton13.setFont(new Font(Dialog.bold, 1, 12));
jButton13.setForeground(new Color(-16777216));
jButton13.setBackground(new Color(-9979928));
jButton13.setBounds(230, 95, 96, 31);
jPanel3.add(jButton13);
jButton14 = new JButton();
jButton14.addActionListener(this);
jButton14.setText(PLAY);
jButton14.setFont(new Font(Dialog.bold, 1, 12));
jButton14.setForeground(new Color(-16777216));
jButton14.setBackground(new Color(-9979928));
jButton14.setBounds(230, 132, 96, 31);
jPanel3.add(jButton14);
jButton15 = new JButton();
jButton15.addActionListener(this);
jButton15.setText(CAPTURE);
jButton15.setFont(new Font(Dialog.bold, 1, 12));
jButton15.setForeground(new Color(-16777216));
jButton15.setBackground(new Color(-9979928));
jButton15.setBounds(230, 169, 96, 31);
jPanel3.add(jButton15);
jButton16 = new JButton();
jButton16.addActionListener(this);
jButton16.setText(Connect);
jButton16.setFont(new Font(Dialog.bold, 1, 12));
jButton16.setForeground(new Color(-10092544));
jButton16.setBackground(new Color(-14185529));
jButton16.setBounds(658, 217, 108, 31);
jPanel1.add(jButton16);
jButton17 = new JButton();
jButton17.addActionListener(this);
jButton17.setText(Disconnect);
jButton17.setFont(new Font(Dialog.bold, 1, 12));
jButton17.setForeground(new Color(-10092544));
jButton17.setBackground(new Color(-14185529));
jButton17.setBounds(775, 216, 108, 31);
jPanel1.add(jButton17);
}
}
-
리나
StringTokenizer 객체에서 countTokens() 메소드를 사용해서. 몇개의 결과물을 반환할 수 있는지 확인해보는것도 좋을듯 합니다. ^_^
-
블1랙캣
이런 문제 같은 경우에는. StringTokenized 에서 getNext() 할때 얻어 오는 값을..
화면에 찍어서 확인해 보는 법이 가장 좋습니다.
그리고. 소스를 저렇게 길게 내 놓아도. 보는 사람은 거의 없답니다. 글의 상단에 주신 만큼이 좋을것 같네요.
그리고. 몇번째줄에 에러가 났다면.. 그 줄 역시 표시해 주는게. 답변을 주시는 분들한테는 편하겠지요.