import java.util.*;
public class PairFinder {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Read in the value of k
int k = Integer.parseInt(sc.nextLine());
// Read in the list of numbers
int[] numbers;
String input = sc.nextLine();
if (input.equals("")) {
numbers = new int[0];
} else {
String[] numberStrings = input.split(" ");
numbers = new int[numberStrings.length];
for (int i = 0; i < numberStrings.length; i++) {
numbers[i] = Integer.parseInt(numberStrings[i]);
}
}
System.out.println(findPairs(numbers, k));
}
//method that tells you number of pairs that add to int k
private static int findPairs(int[] numbers, int k) {
// Tracks the number of pairs found
int pairsFound = 0;
int dulipcates = 0;
//initializing the HashMaps
Map<Integer, Integer> pairs = new HashMap<Integer,
Integer>();
//initializing the ArrayList
List<Integer> numbersCopy = new ArrayList<>();
//copying the numbers into an ArrayList
for(int i=0; i<numbers.length; i++){
numbersCopy.add(numbers[i]);
}
//adding all the numbers from the array into a HashMap object
for(int i = 0; i<numbers.length; i++){
// making the otherPair into the key for HashMap
int otherPair = k - (numbers[i]);
if(numbersCopy.contains(otherPair)){
if(Collections.frequency(numbersCopy, otherPair) > 0){
//pairs.remove(numbersCopy.get(otherPair));
numbersCopy.remove(otherPair);
dulipcates = Collections.frequency(numbersCopy, otherPair);
}else{
pairs.put(otherPair, numbersCopy.get(i));
pairsFound++;
}
}
}
return (pairsFound+dulipcates);
}
}
This is my code and I'm not sure why its not returning correcting. I must also count the duplicates in the array.
import java.util.*;
public class PairFinder {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Read in the value of k
int k = Integer.parseInt(sc.nextLine());
// Read in the list of numbers
int[] numbers;
String input = sc.nextLine();
if (input.equals("")) {
numbers = new int[0];
} else {
String[] numberStrings = input.split(" ");
numbers = new int[numberStrings.length];
for (int i = 0; i < numberStrings.length; i++) {
numbers[i] = Integer.parseInt(numberStrings[i]);
}
}
System.out.println(findPairs(numbers, k));
}
// method that tells you number of pairs that add to int k
private static int findPairs(int[] numbers, int k) {
// Tracks the number of pairs found
int pairsFound = 0;
int dulipcates = 0;
// initializing the HashMaps
Map<Integer, Integer> pairs = new HashMap<Integer, Integer>();
for (int i=0; i<numbers.length; i++){
if(!pairs.containsKey(numbers[i]))
pairs.put(numbers[i],0);
pairs.put(numbers[i], pairs.get(numbers[i])+1);
}
for (int i=0; i<numbers.length; i++)
{
if(pairs.get(k-numbers[i]) != null)
pairsFound += pairs.get(k-numbers[i]);
if (k-numbers[i] == numbers[i])
pairsFound--;
}
return pairsFound/2;
}
}
==============================================================
See OUTPUT
Thanks, PLEASE COMMENT if there is any concern.
Import java.util.*; public class PairFinder { public static void main(String[] args) { Scanner sc...
import java.util.Random; import java.util.ArrayList; /** * */ public class hw5_task8 { public static void main(String[] args) { int[] grades = randomIntArr(10); printIntArray("grades", grades); ArrayList<Integer> indexesF_AL = selectIndexes_1(grades); System.out.println(" indexesF_AL: " + indexesF_AL); int[] indexesF_Arr = selectIndexes_2(grades); printIntArray("indexesF_Arr",indexesF_Arr); } public static int[] randomIntArr(int N){ int[] res = new int[N]; Random r = new Random(0); for(int i = 0; i < res.length; i++){ res[i] = r.nextInt(101); // r.nextInt(101) returns an in in range [0, 100] } return res; } public static void...
public static void main(String[] args) { System.out.println("Welcome to the Future Value Calculator\n"); Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // get the input from the user System.out.println("DATA ENTRY"); double monthlyInvestment = getDoubleWithinRange(sc, "Enter monthly investment: ", 0, 1000); double interestRate = getDoubleWithinRange(sc, "Enter yearly interest rate: ", 0, 30); int years = getIntWithinRange(sc, "Enter number of years: ", 0, 100); System.out.println(); ...
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner inp = new Scanner(System.in); System.out.print("In:"); int nn = inp.nextInt(); int n0, n1, n2, n3, n4; //Change only the code below //Set n0, n1, . . . n4 to each digits in nn by replacing //the blanks _A_, _B_, ect //Hint: the ones digit of 1234 is 1234%10 // the hundredth digit of 31415 is (31415/100)%10 n0 = _A_; n1 = _B_; n2 = _C_; n3 = _D_;...
1: import java.util.*; 2: class Test 3: { 4: public static void main(String[] args) 5: { 6: ArrayList<Object> list = new ArrayList<Object>(); 7: list.add("First Element"); 8: list.add(new Integer(12)); 9: String str = (String) list.get(0); 10: String str2 = (String)list.get(1); 11: } 12: } There is a run time error on which line number? A) Line 9 B) Line 7 C) No errors D) Line 8 E) Line 10 in Java
import java.util.Scanner; import java.util.ArrayList; public class P3A2_BRANDT_4005916 { public static void main(String[] args) { String name; String answer; int correct = 0; int incorrect = 0; Scanner phantom = new Scanner(System.in); System.out.println("Hello, What is your name?"); name = phantom.nextLine(); System.out.println("Welcome " + name + "!\n"); System.out.println("My name is Danielle Brandt. " +"This is a quiz program that...
import java.util.ArrayList; import java.util.List; import java.util.Stack; public class Main { public static void main(String[] args) { int programCounter = 0; List<String> program = new ArrayList<String>(); Stack<String> stack = new Stack<String>(); // TODO string probably not best program.add("GOTO start<<1>>"); program.add("LABEL Read"); program.add("LINE -1"); program.add("FUNCTION Read -1 -1"); program.add("READ"); program.add("RETURN "); program.add("LABEL Write"); program.add("LINE -1"); program.add("FUNCTION Write -1 -1"); program.add("FORMAL dummyFormal 0"); program.add("LOAD 0 dummyFormal"); program.add("WRITE"); program.add("RETURN "); program.add("LABEL start<<1>>"); program.add("LINE 1"); program.add("FUNCTION main 1 4"); program.add("LIT 0 i"); program.add("LIT 0 j");...
import java.util.Scanner; import java.io.File; public class Exception2 { public static void main(String[] args) { int total = 0; int num = 0; File myFile = null; Scanner inputFile = null; myFile = new File("inFile.txt"); inputFile = new Scanner(myFile); while (inputFile.hasNext()) { num = inputFile.nextInt(); total += num; } System.out.println("The total value is " + total); } } /* In the first program, the Scanner may throw an...
import java.util.Scanner; public class Lab6d { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); // TODO: get user choice // TODO: call printTable method passing choice as the parameter } public static void printTable(int stop) { // TODO: print header // TODO: loop to print table rows up to stop value } Write a Java program where the main () method prompts the user to select an integer value between 1 and...
Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp { public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...
import java.util.List; import java.util.ArrayList; import java.util.LinkedList; public class ListPractice { private static final int[] arr = new int[100000]; public static void main(String[] args) { for(int i=0; i<100000; i++) arr[i] = i; //TODO comment out this line LinkedList<Integer> list = new LinkedList<Integer>(); //TODO uncomment this line //List<Integer> list = new ArrayList<Integer>(); //TODO change the rest of the...