Java Code Examples for java.util.Scanner.close()

The following are Jave code examples for showing how to use close() of the java.util.Scanner class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: ACE_HackerRank   File: Solution.java   View Source Code Vote up 7 votes
public static void main(String[] args) {
    /* Read and save input as LocalDates */
    Scanner scan = new Scanner(System.in);
    LocalDate returnDate = readDate(scan);
    LocalDate expectDate = readDate(scan);
    scan.close();
    
    /* Calculate fine */
    int fine;
    if (returnDate.isEqual(expectDate) || returnDate.isBefore(expectDate)) {
        fine = 0;
    } else if (returnDate.getMonth() == expectDate.getMonth() && returnDate.getYear() == expectDate.getYear()) {
        fine = 15 * (returnDate.getDayOfMonth() - expectDate.getDayOfMonth());
    } else if (returnDate.getYear() == expectDate.getYear()) {
        fine = 500 * (returnDate.getMonthValue() - expectDate.getMonthValue());
    } else {
        fine = 10000;
    }
    System.out.println(fine);
}
 
Example 2
Project: DreamBlocks   File: StockMethods.java   View Source Code Vote up 7 votes
public static String readFile(String pathname) throws IOException {
	// NOTE: drops newlines
	StringBuilder fileContents = new StringBuilder();
	InputStream in = Thread.currentThread().getContextClassLoader()
			.getResourceAsStream(pathname);
	if (in == null) {
		throw new IOException("no resource found at " + pathname);
	}
	Scanner scanner = new Scanner(in);
	try {
		while (scanner.hasNextLine()) {
			fileContents.append(scanner.nextLine());
		}
		return fileContents.toString();
	} finally {
		scanner.close();
	}
}
 
Example 3
Project: ACE_HackerRank   File: Solution.java   View Source Code Vote up 7 votes
public static void main(String[] args) {
    /* Save input */
    Scanner scan = new Scanner(System.in);
    int size     = scan.nextInt();
    int k        = scan.nextInt();
    int q        = scan.nextInt();
    int [] array = new int[size];
    for (int i = 0; i < size; i++) {
        array[i] = scan.nextInt();
    }
    
    /* Rotate array (in place) using 3 reverse operations */
    k %= size; // to account for k > size
    reverse(array, 0, array.length - 1);
    reverse(array, 0, k - 1);
    reverse(array, k, array.length - 1);
    
    /* Print output */
    while (q-- > 0) {
        int m = scan.nextInt();
        System.out.println(array[m]);
    }
    scan.close();
}
 
Example 4
Project: ACE_HackerRank   File: Solution.java   View Source Code Vote up 7 votes
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int N = scan.nextInt();
    
    /* Initialize BitSet */
    BitSet bitset = new BitSet(NUM_ELEMENTS);
    bitset.set(0, NUM_ELEMENTS);

    /* Create a BitSet for each rock. "AND" it with our original BitSet */
    for (int r = 0; r < N; r++) {
        String rock = scan.next();
        BitSet currBitSet = new BitSet(NUM_ELEMENTS);
        for (int i = 0; i < rock.length(); i++) {
            currBitSet.set(rock.charAt(i) - 'a');
        }
        bitset.and(currBitSet);
    }
    
    scan.close();
    System.out.println(bitset.cardinality());
}
 
Example 5
Project: HackerRank_solutions   File: Solution.java   View Source Code Vote up 7 votes
public static void main(String[] args) {
    /* Save input */
    Scanner scan = new Scanner(System.in);
    int s = scan.nextInt();
    int n = scan.nextInt();
    int m = scan.nextInt();
    int[] keyboards = new int[n];
    for (int i = 0; i < n; i++) {
        keyboards[i] = scan.nextInt();
    }
    int[] drives = new int[m];
    for (int i = 0; i < m; i++) {
        drives[i] = scan.nextInt();
    }
    scan.close();
    
    /* Calculate result */
    int moneySpent = getMoneySpent(keyboards, drives, s);
    System.out.println(moneySpent);
}
 
Example 6
Project: ACE_HackerRank   File: Solution.java   View Source Code Vote up 6 votes
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int s = scan.nextInt();
    int[] array = new int[s];
    for (int i = 0; i < s; i++) {
        array[i] = scan.nextInt(); 
    }
    scan.close();
    insertionSortShifts(array);
}
 
Example 7
Project: ACE_HackerRank   File: Solution.java   View Source Code Vote up 6 votes
public static void main(String[] args) {
    /* Save input grid */
    Scanner scan = new Scanner(System.in);
    rows = scan.nextInt();
    cols = scan.nextInt();
    int [][] grid = new int[rows][cols];
    for (int row = 0; row < rows; row++) {
        for (int col = 0; col < cols; col++) {
            grid[row][col] = scan.nextInt();
        }
    }
    scan.close();

    System.out.println(largestRegion(grid));
}
 
Example 8
Project: java-indian-bank-api-wrapper   File: BankDetails.java   View Source Code Vote up 6 votes
/**
 * @return Bank List JSON in String format
 * @throws IOException
 */
public String getBankList() throws IOException {
	String responseBody = null;
	URL url = new URL(BASE_URL + "/listbanks");
	connection = (HttpURLConnection) url.openConnection();
	connection.setRequestMethod("GET");
	/*
	 * connection.setUseCaches(false); connection.setDoOutput(true);
	 */
	InputStream response = connection.getInputStream();
	Scanner scanner = new Scanner(response);
	responseBody = scanner.useDelimiter("\\A").next();
	scanner.close();
	return responseBody;
}
 
Example 9
Project: MyCourses   File: DosyaOkumaOrnegiSade.java   View Source Code Vote up 6 votes
public static void main(String[] args) throws FileNotFoundException {
    File ogrencilerDosya = new File(".\\config\\Ogrenciler.txt"); 
    // Mac: "./config/Ogrenciler.txt"
    
    if(!ogrencilerDosya.exists() || !ogrencilerDosya.canRead()
            || !ogrencilerDosya.isFile() ) {
        System.out.println("Dosya yok ya da okuma hakkı yok." 
                + ogrencilerDosya.getAbsolutePath() );
        return;
    }
    
    Scanner dosyaOkuyucu = new Scanner(ogrencilerDosya);

    File ciktiDosyasi = new File(".\\config\\Ogrenciler_cikti.txt"); 
    // Mac: "./config/Ogrenciler_cikti.txt"
    
    if (!ciktiDosyasi.canWrite()) {
        System.out.println("Yazma hakki yok");
        return;
    }
    
    PrintWriter dosyaYazici = new PrintWriter(ciktiDosyasi);

    // Dosyaya ek yapmak icin:
    // FileOutputStream fos = new FileOutputStream(ciktiDosyasi);
    // PrintWriter dosyaYazici2 = new PrintWriter(fos, true);
    for (int i = 0; dosyaOkuyucu.hasNextLine(); i++) {
        String mevcutSatir = dosyaOkuyucu.nextLine();
        System.out.println(mevcutSatir);
        dosyaYazici.println("Satir " + (i + 1) + ": " + mevcutSatir);
    }

    dosyaYazici.close();
    dosyaOkuyucu.close();
}
 
Example 10
Project: HackerRank_solutions   File: Solution.java   View Source Code Vote up 6 votes
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int testCases = scan.nextInt();
    scan.nextLine(); // gets rid of the pesky newline.
    while (testCases-- > 0) {
       String pattern = scan.nextLine();
       try {
           Pattern.compile(pattern);
           System.out.println("Valid");
       } catch (PatternSyntaxException exception) {
           System.out.println("Invalid");
       }
    }
    scan.close();
}
 
Example 11
Project: ACE_HackerRank   File: Solution.java   View Source Code Vote up 6 votes
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int N = scan.nextInt();
    int K = scan.nextInt();
    ArrayList<Integer> contest = new ArrayList<>(N);
    int savedLuck = 0;
    for (int i = 0; i < N; i++) {
        int luck = scan.nextInt();
        int importance = scan.nextInt();
        if (importance == 0) {
            savedLuck += luck; // lose every non-important contest
        } else {
            contest.add(luck);
        }
    }
    scan.close();
    
    /* Compete in "important" contests */
    quickselect(contest, contest.size() - K);
    for (int i = 0; i < contest.size(); i++) {
        if (i < contest.size() - K) {
            savedLuck -= contest.get(i); // win contest
        } else {
            savedLuck += contest.get(i); // lose contest
        }
    }
    System.out.println(savedLuck);
}
 
Example 12
Project: HackerRank_solutions   File: Solution.java   View Source Code Vote up 6 votes
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int p = scan.nextInt();
    while (p-- > 0) {
        int n = scan.nextInt();
        System.out.println(isPrime(n) ? "Prime" : "Not prime");
    }
    scan.close();
}
 
Example 13
Project: HackerRank-in-Java   File: Solution.java   View Source Code Vote up 6 votes
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int N = in.nextInt();
    in.nextLine();

    for (int i = 0; i < N; i++) {
        String string = in.nextLine();
        char[] charArray = string.toCharArray();

        for (int j = 0; j < charArray.length; j++) {
            if (j % 2 == 0) {
                System.out.print(charArray[j]); //For even
            }
        }

        System.out.print(" ");

        for (int j = 0; j < charArray.length; j++) {
            if (j % 2 != 0) {
                System.out.print(charArray[j]); //For odd
            }
        }

        System.out.println();
    }

    in.close();
}
 
Example 14
Project: HackerRank_solutions   File: Solution.java   View Source Code Vote up 6 votes
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();
    int [] array = new int[n];
    for (int i = 0; i < n; i++) {
        array[i] = scan.nextInt();
    }
    scan.close();
    medianTracker(array);
}
 
Example 15
Project: Homework   File: Pay.java   View Source Code Vote up 6 votes
public static void main(String[] args) 
{
	Scanner sc = new Scanner(System.in);

	String isVip;
	double money;
	double discount;

	System.out.println("请输入是否是会员:是(y)/否(其他字符)");
	isVip = sc.next();
	System.out.println("请输入购物金额:");
	money = sc.nextDouble();
	if (isVip.equals("y")) 
	{
		if (money >= 200) 
		{
			discount = 0.75; 
		}
		else 
		{
			discount = 0.8;
		}
	}
	else if (money >= 100) 
	{
		discount = 0.9;
	}
	else 
	{
		discount = 1;
	}
	System.out.println("实际支付:" + money * discount);

	sc.close();


}
 
Example 16
Project: Homework   File: ShoppingList.java   View Source Code Vote up 6 votes
public static void main(String[] args) 
{
	Scanner sc=new Scanner(System.in);
	double price1 = 245;//T恤的单价
	double price2 = 570;//网球鞋的单价
	double price3 = 320;//网球拍的单价

	System.out.println("T恤的数量:");
	int quantity1 = sc.nextInt();
	double money1 = price1 * quantity1;

	System.out.println("网球鞋的数量:");
	int quantity2 = sc.nextInt();
	double money2 = price2 * quantity2;

	System.out.println("网球拍的数量:");
	int quantity3 = sc.nextInt();
	double money3 = price3 * quantity3;

	double total = money1 + money2 + money3;
	double disCount = (money1 + money2 + money3) * 0.8;
	double payment = 1500;
	int  integral = (int) disCount / 100 * 3;
	System.out.println("*************消费单*************");
	System.out.println("购买物品\t单价\t\t个数\t\t金额");
	System.out.println("T恤\t" + "\t¥" + price1 + "\t\t" + quantity1 + "\t\t" + "¥" + money1);
	System.out.println("网球鞋\t" + "\t¥" + price2 + "\t\t" + quantity2 + "\t\t" + "¥" + money2);
	System.out.println("网球拍\t" + "\t¥" + price3 + "\t\t" + quantity3 + "\t\t" + "¥" + money3);
	System.out.println();
	System.out.println("折扣:\t         8折");
	System.out.println("消费总金额    ¥"+ payment);
	System.out.println("实际交费      ¥"+ disCount);
	System.out.println("找钱          ¥"+ ( payment - disCount));
	System.out.println("本次购物所获得的积分是: "+ integral);   

	sc.close();
}
 
Example 17
Project: robird-reborn   File: TweetLongerUtils.java   View Source Code Vote up 6 votes
private static String readTweetLongedText(String id) {
    StringBuilder sb = new StringBuilder();
    try {
        Scanner scanner = new Scanner(new BufferedInputStream(
                new URL("http://www.twitlonger.com/api_read/" + id).openStream()));
        while (scanner.hasNext())
            sb.append(scanner.nextLine());
        scanner.close();
        return new TwitLongerResponse(sb.toString()).content;
    } catch (IOException e) {
        Timber.i("", e);
    }

    return sb.toString();
}
 
Example 18
Project: ACE_HackerRank   File: Solution.java   View Source Code Vote up 6 votes
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();
    Trie trie = new Trie();
    for (int i = 0; i < n; i++) {
        String operation = scan.next();
        String contact   = scan.next();
        if (operation.equals("add")) {
            trie.add(contact);
        } else if (operation.equals("find")) {
            System.out.println(trie.find(contact));
        }
    }
    scan.close();
}
 
Example 19
Project: Homework   File: Menu.java   View Source Code Vote up 5 votes
public static void main(String[] args) 
{
	Scanner sc = new Scanner(System.in);

	int choice;

	System.out.println("欢迎使用我行我素购物管理系统\n1.登陆系统\n2.退出");
	System.out.println("**********************************");
	System.out.print("请选择,输入数字:");
	choice = sc.nextInt();
	switch (choice)
	{
		case 1 :
		{
			System.out.println("主菜单(第二级):\n1.购物信息管理\n2.购物结算\n3.真情回馈\n4.注销");
			System.out.print("请选择,输入数字:");
			choice = sc.nextInt();
			switch (choice)
			{
				case 1 :
				
					System.out.println("购物管理系统>客户信息管理\n1.显示所有客户信息\n2.添加客户信息\n3.修改客户信息\n4.查询客户信息");
					break;
				
				case 2 :
					System.out.println("开始购物结算");
					break;
				case 3 :
					System.out.println("购物管理系统>真情回馈\n1.幸运大放送\n2.幸运抽奖\n3.生日问候");
					break;

				
				case 4:
					System.out.println("已注销,谢谢使用");
					break;
				default :
					System.out.println("选择错误");

			}
			break;
		}
		case 2 :
			System.out.println("谢谢您的使用,已退出");
			break;
		default :
			System.out.println("选择错误");



	}

	sc.close();	
}
 
Example 20
Project: HackerRank_solutions   File: Solution.java   View Source Code Vote up 3 votes
public static Tree solve() {
    Scanner scan = new Scanner(System.in);
    int numNodes = scan.nextInt();
    
    /* Save nodes and colors */
    values = new int[numNodes];
    colors = new Color[numNodes];
    map = new HashMap<>(numNodes);
    for (int i = 0; i < numNodes; i++) {
        values[i] = scan.nextInt();
    }
    for (int i = 0; i < numNodes; i++) {
        colors[i] = scan.nextInt() == 0 ? Color.RED : Color.GREEN;
    }
    
    /* Save edges */
    for (int i = 0; i < numNodes - 1; i++) {
        int u = scan.nextInt();
        int v = scan.nextInt();
        
        /* Edges are undirected: Add 1st direction */
        HashSet<Integer> uNeighbors = map.get(u);
        if (uNeighbors == null) {                
            uNeighbors = new HashSet<>();
            map.put(u, uNeighbors);
        }
        uNeighbors.add(v);
        
        /* Edges are undirected: Add 2nd direction */
        HashSet<Integer> vNeighbors = map.get(v);
        if (vNeighbors == null) {
            vNeighbors = new HashSet<>();
            map.put(v, vNeighbors);
        }
        vNeighbors.add(u);
    }
    scan.close();
    
    /* Handle 1-node tree */
    if (numNodes == 1) {
        return new TreeLeaf(values[0], colors[0], 0);
    }

    /* Create Tree */
    TreeNode root = new TreeNode(values[0], colors[0], 0);
    addChildren(root, 1);
    return root;
}