[코딩 오류 모음]

Dawon Ruby Choi·2023년 8월 24일
0

Java

목록 보기
6/18
post-thumbnail

(1)
"M cannot be resolved to a variable"

23.08.24
상황 :

System.out.print("성별(M/F): ");
		char gender = sc.nextLine().charAt(0);
		
		String gender1 = gender == M? "남학생" : "여학생";

scanner를 통해 성별과 연산자를 이용하고 있었는데 해당 오류가 뜸
시도 : 괄호도 쳐보고 여러가지 다해봄
해결 방안 : m은 문자인데 ''를 깜빡함
개념 :

(2)
"i cannot be resolved to a variable"
23.08.25
상황 :

	public void method2_1() {
		//8부터 3까지 출력
		for (int i =8; i >= 3; i--); { 
		System.out.println(i);
		}

System.out.println(i);에서 i에 붉은색으로 자꾸 오류가남
시도 : 다른 메소드에서 i를 써서?그런가? 그럴리가 없는데...하면서 e로도 바꿔봄
해결 방안 : for (int i =8; i >= 3; i--); 마지막 세미콜론 때문에 오류난거였음
개념 :

(3)
"Type mismatch: cannot convert from char to boolean"
23.08.27
상황 :

public void practice8() {
		Scanner sc = new Scanner(System.in);
		System.out.print("피연산자1 입력 : ");
		int num1 = sc.nextInt();
		System.out.print("피연산자2 입력 : ");
		int num2 = sc.nextInt();
		System.out.print("연산자를 입력(+,-,*,/,% : ");
		String str = sc.nextLine();
		char ch = str.charAt(0);
		
		if (ch = '+') {  
			System.out.println(num1 + num2);

if (ch = '+') 에서 자꾸 오류가 남
시도 : 이번에는 문자열을 ''로 넣어줬다고 생각했는데 위에 string 클래스도 넣어서 ""로도 바꿔봄
해결 방안 : if (ch == '+') 비교 연산자가 아닌 대입연산자를 넣어버렸어서 =을 ==로 고쳐줌
개념 :

(4)
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:658)
at control.ControlPractice.practice8(ControlPractice.java:167)
at run.Run.main(Run.java:17)

23.08.27

상황 :

	public void practice8() {
		Scanner sc = new Scanner(System.in);
		System.out.print("피연산자1 입력 : ");
		int num1 = sc.nextInt();
		System.out.print("피연산자2 입력 : ");
		int num2 = sc.nextInt();
		sc.nextLine();
		System.out.print("연산자를 입력(+,-,*,/,% : ");
		String str = sc.nextLine();
		char ch = str.charAt(0);
		
		if (ch == '+') {  
			System.out.println(num1 + num2);
		}else if (ch == '-') {
			System.out.println(num1 - num2);
		}else if (ch == '*') {
			System.out.println(num1 * num2);
		}else if (ch == '/') {
			System.out.println(num1 / num2);
		}else if (ch=='%') {
			System.out.println(num1 % num2);
		}else {
				System.out.println("잘못 입력하셨습니다. 프로그램을 종료합니다.");
			}

출력이 안됨
시도 : 콘솔에서만 문제가되고 클래스에는 이상이 없음
해결 방안 : 스캐너에서 인수 정수를 사용하고 나서 버퍼가 남는것을 깜빡했음 그래서 sc.nextLine();로 버퍼를 다시 비우는 작업 진행 후 string 사용
개념 :

(5)
Syntax error on token "else", delete this token

23.08.28
상황 : else에 자꾸 빨간 줄이 그어짐

package com.kh.general.chap02.loop;

import java.util.Scanner;

public class LoopPractice2 {
	public void practice1() {
	
	Scanner sc = new Scanner (System.in);
	System.out.print("1 이상의 숫자를 입력하세요 : ");
	int num = sc.nextInt();
	
	if(num > 0) {
	for (int i= 1; i <= num; i++) {
		System.out.print(i+" ");
	} else {
		System.out.println("1 이상의 숫자를 입력해주세요.");
			}
		}
	}
}

시도 :
해결 방안 : 중괄호를 잘못 사용함, if 문 블록 안에 for 문이 들어가야 함

수정 후 :

package com.kh.general.chap02.loop;

import java.util.Scanner;

public class LoopPractice2 {
	public void practice1() {
	
	Scanner sc = new Scanner (System.in);
	System.out.print("1 이상의 숫자를 입력하세요 : ");
	int num = sc.nextInt();
	
	if(num > 0) {
	for (int i= 1; i <= num; i++) {
		System.out.print(i+" ");
	} 
	} else {
		System.out.println("1 이상의 숫자를 입력해주세요.");
			}
		}
	}

(6)
length cannot be resolved or is not a field

23.08.29

상황

public void practice5() {
		Scanner sc = new Scanner(System.in);
	    System.out.print("문자열 : ");
	    String str = sc.next();
	    System.out.print("문자 : ");
	    char ch = sc.next().charAt(0);
	    
	    String[] arr = new String [str.length];

배열의 길이를 사용자가 "문자열"에 입력한 개수로 지정하고 싶은데 자꾸 빨간색으로 오류가 남
시도 : next가 문제인가 해서 nextLine으로도 바꿔 작성해 봄
해결방안 : 배열에서 배열의 크기는 배열 안의 변수를 통해 얻을 수 있으므로 length하고 아무것도 안 붙이는게 맞지만, String클래스에서의 문자열 길이는 메소드를 통해 얻을 수 있으므로 length()를 쓰는게 맞다.

(7)
null 0 null

23.08.31

상황

package mine.practice2.mode.vo;

public class Product {
	
	private String pName;
	private int price;
	private String brand;

	
public void information () {
	System.out.print(pName + " " + price +  " " + brand);
	
	pName = "지갑";
	price = 100000;
	brand = "비비안 웨스트우드"; 
	}
}

"지갑 10000 비비안 웨스트우드"가 뜨는게 아니라 자꾸 "null 0 null"이 뜸

시도 :
뒤에 실행 클래스에서 p.information(pName + price + brand); 매개변수도 넣어보고 별 짓을 다해봄

해결방안 : 변수 초기화를 먼저해주고 프린트했었으면 됐었음

pName = "지갑";
	price = 100000;
	brand = "비비안 웨스트우드"; //변수 초기화
 
 System.out.print(pName + " " + price +  " " + brand);

(7)
발생 일자 : 23.09.13

오류문구 :
Default constructor cannot handle exception type IOException thrown by implicit super constructor. Must define an explicit constructor

package memo;

import java.net.ServerSocket;

public class NetworkPractice {
	int port = 10000;
	
	ServerSocket socket = new ServerSocket();
}

new ServerSocket(); 부분 자꾸 빨간줄로 오류가 뜸

시도 : 매개변수 값 안넣어서 그런가 해서 new ServerSocket(prot); 넣어봄

해결방안 : public void startServer() {
메소드를 정의하지 않았음

profile
나의 코딩 다이어리🖥️👾✨

0개의 댓글