자바 배열을 이용한 아이디 저장 코드인데 출력이 잘 안됩니다.

ansdyd64의 이미지

밑에 코드는
자바 배열을 이용한 로그인 하고 그걸 보여주는 프로그램 인데요,
2째줄에 넣으면 첫째줄로 입력되고
어쩔때는 3번째로 넘어가네요.
마지막에 서치 항목은 아예 에러가 나네요,
코드에는 에러가 없다고 나오는데 뭐가 문제일까요?

public static void main(String[] args) { 
 
		//varaible declaration 
		int i,choice,lid,cid,f;
		String uid,chid,sid;  
		String l1[]=new String[5]; 
		String l2[]=new String[6]; 
		String l3[]=new String[5]; 
		String l4[]=new String[5]; 
 
 
	for(i=0;i<4;i++) { 
		l3[i]="empty"; 
			} 
	for(i=0;i<3;i++) { 
 
		l4[i]="empty"; 
		} 
		do 
		{ 
		f=0; //status of the machines 
			System.out.println("LAB STATUS"); 
			System.out.println("Lab # Computer Stations"); 
			System.out.print("1 "); 
 
			for(i=0;i<5;i++) { 
				System.out.print((i+1)+": "+l1[i]+" "); 
				} 
			System.out.println(); 
			System.out.print("2 "); 
			for(i=0;i<6;i++) 
			{ 
				System.out.print((i+1)+": "+l2[i]+" "); 
				} 
			System.out.println(); System.out.print("3 "); 
			for(i=0;i<4;i++) { 
				System.out.print((i+1)+": "+l3[i]+" "); 
				} 
			System.out.println(); System.out.print("4 "); 
			for(i=0;i<3;i++) {
				System.out.print((i+1)+": "+l4[i]+" "); 
				} 
			System.out.println(); 
			Scanner sc=new Scanner(System.in); 
			System.out.println("MAIN MENU"); 
			System.out.println("0) Quit"); 
			System.out.println("1) Simulate login"); 
			System.out.println("2) Simulate logoff"); 
			System.out.println("3) Search"); 
			choice = sc.nextInt();//asking for user choice 
			if(choice == 0) { 
				System.exit(0); 
			} 
			else if(choice == 1) { 
 
				//taking input from user 
				System.out.println("Enter the 5 digit ID number of the user logging in:"); 
				uid=sc.next(); System.out.println("Enter the lab number the user is logging in from (1-4):"); 
				cid=sc.nextInt(); System.out.println("Enter computer station number the user is logging in to (1-6):"); 
				cid=sc.nextInt(); 
				if(cid==1) { 
					l1[cid-1]=uid; 
					} 
 
				else 
					if(cid==2) { 
					l2[cid-1]=uid; 
					} 
				else 
					if(cid==3) { 
						l3[cid-1]=uid; 
						} 
					else 
						l4[cid-1]=uid; 
				} 
			else 
				if(choice == 2) { //taking input from user 
					System.out.println("Enter the 5 digit ID number of the user to find:"); 
					chid=sc.next(); 
					for(i=0;i<5;i++) { 
						if(l1[i].equalsIgnoreCase(chid)) l1[i]="empty"; 
						} 
					for(i=0;i<6;i++) { 
							if(l2[i].equalsIgnoreCase(chid)) l2[i]="empty"; 
							} 
						for(i=0;i<4;i++) { 
								if(l3[i].equalsIgnoreCase(chid)) l3[i]="empty"; 
								} 
						for(i=0;i<3;i++) { 
							if(l4[i].equalsIgnoreCase(chid)) l4[i]="empty"; 
							} 
						System.out.println("User "+chid+" is logged off."); 
						} else 
							if(choice == 3) { //taking input from user 
								System.out.println("Enter the 5 digit ID number of the user to find:"); 
								sid=sc.next(); 
								for(i=0;i<5;i++) { 
 
									if(l1[i].equalsIgnoreCase(sid)) { 
 
										System.out.println("User "+sid+" logged in lab 1 in system "+(i+1)); f=1; break; 
										}
									} 
								for(i=0;i<6;i++) { 
										if(l2[i].equalsIgnoreCase(sid)) { 
											System.out.println("User "+sid+" logged in lab 2 in system "+(i+1)); 
										f=1; break; 
										} 
									} 
									for(i=0;i<4;i++) { if(l3[i].equalsIgnoreCase(sid)) { 
										System.out.println("User "+sid+" logged in lab 3 in system "+(i+1)); 
										f=1; break; 
										} 
									} for(i=0;i<3;i++) { 
										if(l4[i].equalsIgnoreCase(sid)) { 
											System.out.println("User "+sid+" logged in lab 4 in system "+(i+1)); 
											f=1; break; 
											} 
										} 
									if(f==0) { 
										System.out.println("None of the user Id is not logged into any computer station"); 
										f=0; 
										} 
									} 
			}
		while(choice!=0); 
		} 
}

결과가 이렇게 나옵니다.

MAIN MENU
0) Quit
1) Simulate login
2) Simulate logoff
3) Search
1
Enter the 5 digit ID number of the user logging in:
ansdy
Enter the lab number the user is logging in from (1-4):
2
Enter computer station number the user is logging in to (1-6):
1
LAB STATUS
Lab # Computer Stations
1 1: ansdy 2: null 3: null 4: null 5: null
2 1: null 2: null 3: null 4: null 5: null 6: null
3 1: empty 2: empty 3: empty 4: empty
4 1: empty 2: empty 3: empty
MAIN MENU
0) Quit
1) Simulate login
2) Simulate logoff
3) Search
1
Enter the 5 digit ID number of the user logging in:
mfmfm
Enter the lab number the user is logging in from (1-4):
2
Enter computer station number the user is logging in to (1-6):
3
LAB STATUS
Lab # Computer Stations
1 1: ansdy 2: null 3: null 4: null 5: null
2 1: null 2: null 3: null 4: null 5: null 6: null
3 1: empty 2: empty 3: mfmfm 4: empty
4 1: empty 2: empty 3: empty

File attachments: 
첨부파일 크기
Image icon 스크린샷 2018-10-14 오후 8.05.00.png77.1 KB
세벌의 이미지

에러 메시지는 없는데, 원하는 대로 동작하지 않는다면 경고(Warning)메시지도 잘 읽어보세요.
도움 될 때가 많습니다.

jachin의 이미지

uid, cid, 그 다음에 chid 값을 입력받으셔야 하는거 아닐까요? ^^;

cid 값을 두 번 받으면서 본래 chid 값이 들어갈 것이 cid 로 들어가서 엉뚱한 곳에 입력된 것 같습니다.

검색을 하실 때 오류는 l1 배열과 l2 배열에 값이 입력되지 않았을 때 실행하셨을 것 같습니다. l1, l2에 대한 초기화도 추가하신다음에 실행해보시면 어떨까요?

배열값을 할당하실 때,
l1.fill("empty");
라고 쓰셔도 for 문 안 쓰시고 초기화 하실 수 있으실 겁니다.

ansdyd64의 이미지

else if(choice == 1) {

//taking input from user
System.out.println("Enter the 5 digit ID number of the user logging in:");
uid=sc.next(); System.out.println("Enter the lab number the user is logging in from (1-4):");
cid=sc.nextInt(); System.out.println("Enter computer station number the user is logging in to (1-6):");
chid=sc.nextLine();

이렇게 해봤는데 아예 랩실 번호 다음이 입력이 안되네요.
제가 잘 못 이해하고 있는 건가요?

chid=sc.next();
로 해도 원하는 랩 하고 열에 안들어가져서요...

jachin의 이미지

네. 제가 말씀드린대로 하셨는데, chid 가 String 인 것을 이제 봤네요.

변수 이름이 무엇을 의미하는지 잘 모르고 있었는데, lid 가 l1, l2, l3, l4를 선택하는 것이고, cid 가 l1[i], l2[i], l3[i], l4[i] 의 i 값으로 쓰시려고 선언하셨나보네요.

그럼 해당 소스코드를 이렇게 써야 하지 않을까요?

System.out.println("Enter the 5 digit ID number of the user logging in:");
uid=sc.next();
System.out.println("Enter the lab number the user is logging in from (1-4):");
lid=sc.nextInt();
System.out.println("Enter computer station number the user is logging in to (1-6):");
cid=sc.nextInt(); 

연습삼아 만드시는 것이겠지만, 코드를 잘 정리해서 써주시고, 어떤 이유로 썼는지 주석을 달아두시는 것이 좋습니다. :)

jachin의 이미지

				if(lid==1) { 
					l1[cid-1]=uid; 
					} 
 
				else 
					if(lid==2) { 
					l2[cid-1]=uid; 
					} 
				else 
					if(lid==3) { 
						l3[cid-1]=uid; 
						} 
				else 
					l4[cid-1]=uid; 
				} 

라고 쓰셔야겠네요...
ansdyd64의 이미지

다음에 올릴때는
주석으로 설명 달아서 올리겠습니다!!!

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.