hasNext() + next()

MJ·2022년 11월 12일
0

IT 개념

목록 보기
1/3

hasNext()

반복할 요소들이 남아 있다면 true를 그 반대라면 false를 반환함(즉, next()가 예외를 던지는 대신 요소를 반환하는 경우 true를 반환)
ex)

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = 0, b = 0;
        while(sc.hasNext()) { 
            a = sc.nextInt();
            b = sc.nextInt();
            System.out.println(a + b);
        }
    }
}

Next()

반복할 다음 요소를 그대로 반환함. 반복할 요소가 없는 경우 예외 발생
ex)

public List<OrderDTO> selectOrderByUserId(String userId) throws SQLException {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<OrderDTO> list = new ArrayList<OrderDTO>();
		try {
			con = DbUtil.getConnection();
			ps = con.prepareStatement("select * from orders where user_id=? order by order_code desc");
			ps.setString(1, userId);
			rs = ps.executeQuery();
			while(rs.next()) { 	
				OrderDTO order = new OrderDTO(rs.getInt(1), rs.getString(2), rs.getDate(3), rs.getString(4), rs.getInt(5), rs.getInt(6), rs.getInt(7), rs.getInt(8), rs.getInt(9));
				List<OrderLineDTO> orderLines = selectOrderLineByOrderCode(order.getOrderCode());
				order.setOrderLineList(orderLines);
				list.add(order);
			}
		} finally {
			DbUtil.dbClose(con, ps, rs);
		}
		return list;
	}
profile
enthusiasm and positivity every single day!

0개의 댓글