수다닷컴

  • 해외여행
    • 괌
    • 태국
    • 유럽
    • 일본
    • 필리핀
    • 미국
    • 중국
    • 기타여행
    • 싱가폴
  • 건강
    • 다이어트
    • 당뇨
    • 헬스
    • 건강음식
    • 건강기타
  • 컴퓨터
    • 프로그램 개발일반
    • C언어
    • 비주얼베이직
  • 결혼생활
    • 출산/육아
    • 결혼준비
    • 엄마이야기방
  • 일상생활
    • 면접
    • 취업
    • 진로선택
  • 교육
    • 교육일반
    • 아이교육
    • 토익
    • 해외연수
    • 영어
  • 취미생활
    • 음악
    • 자전거
    • 수영
    • 바이크
    • 축구
  • 기타
    • 강아지
    • 제주도여행
    • 국내여행
    • 기타일상
    • 애플
    • 휴대폰관련
  • 프로그램 개발일반
  • C언어
  • 비주얼베이직

StringToken사용법좀 알려주세요

아빠몬

2023.04.01

안녕하세요^^덕분에 많은것을 했어요...메세지 주고 받는데 까지 했어요^^서버랑!!ㅋㅋㅋ 감사드려요...
근데요 한가지 문제점이 있어요...서버에서 클라이언트 메세지를 짤라야 하거든여.......$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);
}
}

신청하기





COMMENT

댓글을 입력해주세요. 비속어와 욕설은 삼가해주세요.

  • 리나

    StringTokenizer 객체에서 countTokens() 메소드를 사용해서. 몇개의 결과물을 반환할 수 있는지 확인해보는것도 좋을듯 합니다. ^_^

  • 블1랙캣

    이런 문제 같은 경우에는. StringTokenized 에서 getNext() 할때 얻어 오는 값을..
    화면에 찍어서 확인해 보는 법이 가장 좋습니다.

    그리고. 소스를 저렇게 길게 내 놓아도. 보는 사람은 거의 없답니다. 글의 상단에 주신 만큼이 좋을것 같네요.
    그리고. 몇번째줄에 에러가 났다면.. 그 줄 역시 표시해 주는게. 답변을 주시는 분들한테는 편하겠지요.

번호 제 목 글쓴이 날짜
2694795 이것 어떻게 좀 해결좀;; (3) 개럭시 2025-05-14
2694724 코딩시 폰트 문제; ㅠ 후력 2025-05-13
2694696 텍스트박스 입력에 관한 문제입니다. 딥공감 2025-05-13
2694668 [질문] 페이퍼비전 PointLight 관련 질문 드려요.. 두바다찬솔 2025-05-13
2694611 Flash Lite 2.1에서 BitmapData와 Matrix 지원안하나요? (3) 이플 2025-05-12
2694582 IE & 파이어폭스 (2) 흙이랑 2025-05-12
2694553 무비클립안의 duplicate 발동이 안돼네요; 딥보라 2025-05-12
2694523 자바 애플릿 질문좀 ^^ (6) 동이 2025-05-12
2694494 [질문] JAVA 또는 C++ 로 프로그램 개발시.. 레지스터리 등록 관련 의문점? (3) 우람늘 2025-05-11
2694469 익스6에서 css버그 나오는것 해결방법좀요 !!!! (6) 원술 2025-05-11
2694442 로컬에선 잘 나오는데 운영에 반영하면 이상하게 나와요. (8) 목화 2025-05-11
2694412 [질문] 이미지 로딩후 사이즈 조절할때 (1) 아담 2025-05-11
2694391 설치형 블로그 쓰시는 분들 어떤거 쓰세요?? (7) AngelsTears 2025-05-10
2694362 Microsoft SQL Server에서 서버만드는법 어둠 2025-05-10
2694333 for문으로 돌린 이름의 제어 (4) 레이 2025-05-10
2694308 이미지 css 도와주세요 ㅠㅠ (2) 애기 2025-05-10
2694223 [급질문]스크롤스파이의 offset값 진나 2025-05-09
2694195 li에 이미지 넣고 세로로 메뉴 구성하는 경우 (1) 예님 2025-05-09
2694167 canvas 질문요. (4) 찬늘봄 2025-05-08
2694136 왜 이렇게 나오는지 이해가 잘 가지 않네요. 부탁드리겠습니다... (2) 세련 2025-05-08
<<  이전  1 2 3 4 5 6 7 8 9 10  다음  >>

수다닷컴 | 여러분과 함께하는 수다토크 커뮤니티 수다닷컴에 오신것을 환영합니다.
사업자등록번호 : 117-07-92748 상호 : 진달래여행사 대표자 : 명현재 서울시 강서구 방화동 890번지 푸르지오 107동 306호
copyright 2011 게시글 삭제 및 기타 문의 : clairacademy@naver.com