I've read some topics on the project Euler problem 8. However I don't know why my code is giving me the wrong answer.
package main;
import java.util.ArrayList;
public class Euler8 {
public static void main(String[] args) {
String bigNumber = "73167176531330624919225119674426574742355349194934"
+ "96983520312774506326239578318016984801869478851843"
+ "85861560789112949495459501737958331952853208805511"
+ "12540698747158523863050715693290963295227443043557"
+ "66896648950445244523161731856403098711121722383113"
+ "62229893423380308135336276614282806444486645238749"
+ "30358907296290491560440772390713810515859307960866"
+ "70172427121883998797908792274921901699720888093776"
+ "65727333001053367881220235421809751254540594752243"
+ "52584907711670556013604839586446706324415722155397"
+ "53697817977846174064955149290862569321978468622482"
+ "83972241375657056057490261407972968652414535100474"
+ "82166370484403199890008895243450658541227588666881"
+ "16427171479924442928230863465674813919123162824586"
+ "17866458359124566529476545682848912883142607690042"
+ "24219022671055626321111109370544217506941658960408"
+ "07198403850962455444362981230987879927244284909188"
+ "84580156166097919133875499200524063689912560717606"
+ "05886116467109405077541002256983155200055935729725"
+ "71636269561882670428252483600823257530420752963450"
;
ArrayList<Integer> myArray = new ArrayList<Integer>();
int counter = 1;
int current = 0;
int product = 1;
int maximumProduct = 0;
for(int i = 0; i < bigNumber.length(); i++) {
String b = "" + bigNumber.charAt(i);
current = Integer.parseInt(b);
myArray.add(current);
if(counter % 5 == 0) {
for(int x : myArray) {
product = x * product;
}
if(product > maximumProduct) {
maximumProduct = product;
}
myArray.clear();
product = 1;
}
counter++;
}
System.out.println(maximumProduct);
}
}
I'm getting the answer 31752 and the right one is 40824. Since this is for a homework I can't copy the solution to the problem so I would like any explanation on why my code is not working so I can fix it.
Thanks.
You were not considering the right elements to multiply. Each product is obtained by multiplying 5 consecutive digits, for each digit the code below saves these digits in myArray
public static void main(String[] args) {
String bigNumber = "73167176531330624919225119674426574742355349194934"
+ "96983520312774506326239578318016984801869478851843"
+ "85861560789112949495459501737958331952853208805511"
+ "12540698747158523863050715693290963295227443043557"
+ "66896648950445244523161731856403098711121722383113"
+ "62229893423380308135336276614282806444486645238749"
+ "30358907296290491560440772390713810515859307960866"
+ "70172427121883998797908792274921901699720888093776"
+ "65727333001053367881220235421809751254540594752243"
+ "52584907711670556013604839586446706324415722155397"
+ "53697817977846174064955149290862569321978468622482"
+ "83972241375657056057490261407972968652414535100474"
+ "82166370484403199890008895243450658541227588666881"
+ "16427171479924442928230863465674813919123162824586"
+ "17866458359124566529476545682848912883142607690042"
+ "24219022671055626321111109370544217506941658960408"
+ "07198403850962455444362981230987879927244284909188"
+ "84580156166097919133875499200524063689912560717606"
+ "05886116467109405077541002256983155200055935729725"
+ "71636269561882670428252483600823257530420752963450"
;
;
ArrayList<Integer> myArray = new ArrayList<Integer>();
int counter = 1;
int current = 0;
int product = 1;
int maximumProduct = 0;
for(int i = 0; i < bigNumber.length(); i++) {
String b = "" + bigNumber.charAt(i);
current = Integer.parseInt(b);
myArray.add(current);
counter++;
if(counter == 5) {
for(int x : myArray) {
product = x * product;
}
if(product > maximumProduct) {
maximumProduct = product;
}
myArray.remove(0);
product = 1;
}
counter=myArray.size();
}
System.out.println(maximumProduct);
}
}
See also Greatest product of five consecutive digits in a 1000-digit number
Related
The task is to:
Part one:
Write a program which prints the integers from 1 to a number given by the user.
Sample output part 1
Part two:
Ask the user for the starting point as well.
Sample output part 2
My code:
import java.util.Scanner;
import javax.swing.plaf.synth.SynthOptionPaneUI;
public class FromWhereToWhere {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// part one
System.out.println("Where to?");
int end = Integer.valueOf(scanner.nextLine());
for (int i = 1; i < end + 1; i++) {
System.out.println(i);
}
// part two
System.out.println("Where to?");
end = Integer.valueOf(scanner.nextLine());
System.out.println("Where from?");
int start = Integer.valueOf(scanner.nextLine());
for (int i = start; i < end + 1; i++) {
System.out.println(i);
}
}
}
It works as intended my terminal
But I get an error in TMC saying
FAIL:
WhereFromTest test
NoSuchElementException: No line found
The code for testing:
import fi.helsinki.cs.tmc.edutestutils.MockStdio;
import fi.helsinki.cs.tmc.edutestutils.Points;
import fi.helsinki.cs.tmc.edutestutils.ReflectionUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.*;
import static org.junit.Assert.*;
#Points("02-16.2")
public class WhereFromTest {
#Rule
public MockStdio io = new MockStdio();
#Test
public void test() {
int[][] pairs = {{1, 1}, {12, 8}, {50, 100}, {-2,2}};
for (int[] pair : pairs) {
test(pair);
}
}
private void test(int[] pair) {
io.setSysIn(pair[0] + "\n" + pair[1] + "\n");
int len = io.getSysOut().length();
ReflectionUtils.newInstanceOfClass(FromWhereToWhere.class);
FromWhereToWhere.main(new String[0]);
String output = io.getSysOut().substring(len);
output = output.replaceAll("[^-\\d]+", " ").trim();
String[] lines = output.split("\\s+");
int linesInOutput = (lines.length == 1 && lines[0].isEmpty()) ? 0 : lines.length;
int linesCount;
if(pair[0] < pair[1]) {
linesCount = 0;
} else {
linesCount = pair[0] - pair[1] + 1;
}
if (linesCount != linesInOutput) {
String numbersCount = (linesCount == 1) ? "number": "numbers";
fail("With the input " + pair[0] + ", " + pair[1] + " output should contain " + linesCount + " " + numbersCount + ", now it contained " + linesInOutput);
}
if(linesCount == 0) {
return;
}
int firstNumber = Integer.valueOf(lines[0]);
if(firstNumber != pair[1]) {
fail("With the input " + pair[0] + ", " + pair[1] + " the first printed number should be " + pair[1] + ", now it was " + firstNumber);
}
int lastNumber = getLastNumber(output);
if(lastNumber != pair[0]) {
fail("With the input " + pair[0] + ", " + pair[1] + " the last printed number should be " + pair[0] + ", now it was " + lastNumber);
}
}
private static int getLastNumber(String inputStr) {
String patternStr = "(?s).*?(-?\\d+)\\s*$";
Matcher matcher = Pattern.compile(patternStr).matcher(inputStr);
assertTrue("The output should be a number.", matcher.find());
int number = Integer.valueOf(matcher.group(1));
return number;
}
}
I'm making a simple multiplication game, but when the user enters a correct answer, it still runs the else statement. I know it's a simple solution but I just can't figure it out. Can someone give me a hand?
public static void partB() {
System.out.println("Exercise 1B");
int count = 0;
String active = "true";
int correct = 0;
while (active == "true") {
Random r = new Random();
int Low = 10; //inclusive
int High = 21; //not inclusive
int Result = r.nextInt(High - Low) + Low;
Random r2 = new Random();
int Result2 = r2.nextInt(High - Low) + Low;
int Total = Result * Result2;
Scanner input = new Scanner(System.in);
System.out.println(Result + "*" + Result2 + "=?");
String guess = new String(input.nextLine());
String tostrng = String.valueOf(Total);
if (guess.equals (tostrng)) {
correct += 1;
count+=1;
System.out.println("Correct answer. Score: " + correct + "(" + count + ")");
}
if (guess.equals("q")) {
System.out.println("Good Bye!");
active = "false";
//return;
}
else {
count +=1;
System.out.println("Incorrect answer. Score:" + correct + "(" + count + ")");
}
}
}
Your else with wrong if
public static void partB() {
System.out.println("Exercise 1B");
int count = 0;
String active = "true";
int correct = 0;
while (active == "true") {
Random r = new Random();
int Low = 10; //inclusive
int High = 21; //not inclusive
int Result = r.nextInt(High - Low) + Low;
Random r2 = new Random();
int Result2 = r2.nextInt(High - Low) + Low;
int Total = Result * Result2;
Scanner input = new Scanner(System.in);
System.out.println(Result + "*" + Result2 + "=?");
String guess = new String(input.nextLine());
String tostrng = String.valueOf(Total);
if (guess.equals (tostrng)) {
correct += 1;
count+=1;
System.out.println("Correct answer. Score: " + correct + "(" + count + ")");
}
else {
count +=1;
System.out.println("Incorrect answer. Score:" + correct + "(" + count + ")");
}
if (guess.equals("q")) {
System.out.println("Good Bye!");
active = "false";
//return;
}
}
}
Make type of active variable as boolean i.e. boolean active=true;
and for comparing string values always use .equals() method.
Fixed it like this!
thanks for the help though guys.
if (guess.equals("q")) {
System.out.println("Good Bye!");
return;
}
if (guess.equals(tostrng)) {
correct += 1;
count += 1;
System.out.println("Correct answer. Score: " + correct + "(" + count + ")");
} else {
count += 1;
System.out.println("Incorrect answer. Score:" + correct + "(" + count + ")");
}
On the for loop I have the Java applet is showing me that I have an error. I am trying to use the for loop to count the repetition of letter.
String countString = "";
for (int i = 0; i < 26; i++){
// at the line below, my java applet says I have an error, and that the
//"letterCounts" should be a int and not a string, but I need it to be a string
String n = letterCounts[i];
if (n.equals("0")) {
countString = countString + " ";
} else if (n.length() == 1) {
countString = countString + " " + n + " ";
} else {
countString = countString + n + " ";
}
}
this.countLabel.setText(countString);
You donot show the definition of letterCounts, but I bet it is int[] letterCounts.
So since letterCounts is an array of int, you cannot just assign it to a String.
Just change String n to int n and your comparison to n == 0 and it should work. See below:
String countString = "";
for (int i = 0; i < 26; i++)
{
int n = letterCounts[i];
if (n == 0) {
countString = countString + " ";
} else if (n < 10) {
countString = countString + " " + n + " ";
} else {
countString = countString + n + " ";
}
}
this.countLabel.setText(countString);
I am trying to create a basic bubble sort program but at some point the array tries to reference the 11th position despite the array being 10 long and i am not sure when it occurs
int Last, i = 0, Temp;
int[] Numbers = new int[10];
String[] NumbersString = new String[10];
String initialString = TextBox.getText();
NumbersString = initialString.split(" ");
while(i<10){
Numbers[i] = Integer.parseInt(NumbersString[i]);
i = i + 1;
}
Last = 9;
Boolean Swapped = true;
while(Swapped = true) {
Swapped = false;
i = 0;
while(i < Last) {
if(Numbers[i] > Numbers[i+1]){
Temp = Numbers[i];
Numbers[i] = Numbers[i+1];
Numbers[i+1] = Temp;
Swapped = true;
}
i = i + 1;
}
Last = Last-1;
}
String Result = Numbers[0] + " " + Numbers[1] + " " + Numbers[2] + " " + Numbers[3] + " " + Numbers[4] + " " + Numbers[5] + " " + Numbers[6] + " " + Numbers[7] + " " + Numbers[8] + " " + Numbers[9];
ResultText.setText(Result);
change
while(Swapped = true) {
to
while(Swapped == true) {
What is happening is that Last is firstly wrapping to Negative and then when it gets to the minimum negative number it wraps to Integer.MAX_VALUE and then i will exceed 9
just try to put the length of the Array in here.
int Last, i = 0, Temp;
int[] Numbers = new int[10];
String[] NumbersString = new String[10];
String initialString = "1";
NumbersString = initialString.split(" ");
while (i < NumbersString.length) { // <-- I change it to NumbersString.length
Numbers[i] = Integer.parseInt(NumbersString[i]);
i = i + 1;
}
Last = Numbers.length-1; // <-- I change it to Numbers.length-1
Boolean Swapped = true;
while (Swapped) { // <-- I change it to Swapped only because your Swapped is a Boolean type, no need to equals it into true.
Swapped = false;
i = 0;
while (i < Last) {
if (Numbers[i] > Numbers[i + 1]) {
Temp = Numbers[i];
Numbers[i] = Numbers[i + 1];
Numbers[i + 1] = Temp;
Swapped = true;
}
i = i + 1;
}
Last = Last - 1;
}
String Result = Numbers[0] + " " + Numbers[1] + " " + Numbers[2] + " " + Numbers[3] + " " + Numbers[4] + " "
+ Numbers[5] + " " + Numbers[6] + " " + Numbers[7] + " " + Numbers[8] + " " + Numbers[9];
System.out.println(Result);
}
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
This is my code for a program that should count the number of each letter in an inputted string. When I run the program, it says that there is 0 of each letter, no matter what I input. Thanks for the help in advance!
import java.util.Scanner;
public class stringprogram {
public static void stringinputmethod()
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter a String");
String strs = scan.nextLine();
int strslength = strs.length();
int numa = 0;
int numb = 0;
int numc = 0;
int numd = 0;
int nume = 0;
int numf = 0;
int numg = 0;
int numh = 0;
int numi = 0;
int numj = 0;
int numk = 0;
int numl = 0;
int numm = 0;
int numn = 0;
int numo = 0;
int nump = 0;
int numq = 0;
int numr = 0;
int nums = 0;
int numt = 0;
int numu = 0;
int numv = 0;
int numw = 0;
int numx = 0;
int numy = 0;
int numz = 0;
for(int i = 0; i <= strslength; i++)
{
if (strs.substring(i, i) == "a")
{
numa = numa + 1;
}
if (strs.substring(i, i) == "b")
{
numb = numb + 1;
}
if (strs.substring(i, i) == "c")
{
numc = numc + 1;
}
if (strs.substring(i, i) == "d")
{
numd = numd + 1;
}
if (strs.substring(i, i) == "e")
{
nume = nume + 1;
}
if (strs.substring(i, i) == "f")
{
numf = numf + 1;
}
if (strs.substring(i, i) == "g")
{
numg = numg + 1;
}
if (strs.substring(i, i) == "h")
{
numh = numh + 1;
}
if (strs.substring(i, i) == "i")
{
numi = numi + 1;
}
if (strs.substring(i, i) == "j")
{
numj = numj + 1;
}
if (strs.substring(i, i) == "k")
{
numk = numk + 1;
}
if (strs.substring(i, i) == "l")
{
numl = numl + 1;
}
if (strs.substring(i, i) == "m")
{
numm = numm + 1;
}
if (strs.substring(i, i) == "n")
{
numn = numn + 1;
}
if (strs.substring(i, i) == "o")
{
numo = numo + 1;
}
if (strs.substring(i, i) == "p")
{
nump = nump + 1;
}
if (strs.substring(i, i) == "q")
{
numq = numq + 1;
}
if (strs.substring(i, i) == "r")
{
numr = numr + 1;
}
if (strs.substring(i, i) == "s")
{
nums = nums + 1;
}
if (strs.substring(i, i) == "t")
{
numt = numt + 1;
}
if (strs.substring(i, i) == "u")
{
numu = numu + 1;
}
if (strs.substring(i, i) == "v")
{
numv = numv + 1;
}
if (strs.substring(i, i) == "w")
{
numw = numw + 1;
}
if (strs.substring(i, i) == "x")
{
numx = numx + 1;
}
if (strs.substring(i, i) == "y")
{
numy = numy + 1;
}
if (strs.substring(i, i) == "z")
{
numz = numz + 1;
}
}
System.out.println("Number of a's: " + numa + "\n" + "Number of b's: " + numb + "\n" + "Number of c's: " + numc + "\n" + "Number of d's: " + numd + "\n" + "Number of e's: " + nume + "\n" + "Number of f's: " + numf + "\n" + "Number of g's: " + numg + "\n" + "Number of h's: " + numa + "\n" + "Number of i's: " + numi + "\n" + "Number of j's: " + numj + "\n" + "Number of k's: " + numk + "\n" + "Number of l's: " + numl + "\n" + "Number of m's: " + numm + "\n" + "Number of n's: " + numn + "\n" + "Number of o's: " + numo + "\n" + "Number of p's: " + nump + "\n" + "Number of q's: " + numq + "\n" + "Number of r's: " + numr + "\n" + "Number of s's: " + nums + "\n" + "Number of t's: " + numt + "\n" + "Number of u's: " + numu + "\n" + "Number of v's: " + numv + "\n" + "Number of w's: " + numw + "\n" + "Number of x's: " + numx + "\n" + "Number of y's: " + numy + "\n" + "Number of z's: " + numz);
}
public static void main(String[] args)
{
stringinputmethod();
}
}
Correct usage of the substring method:
strs.substring(i, i)
needs to be
strs.substring(i, i + 1)
because the char at lastIndex is not included in the output.
Correct comparison of Strings in Java
Also, as pointed out in the comments to this answer, you are comparing Strings with the == operator.
This will only works as long as both your Strings are the same object. For proper comparison you need to use strs.substring(..).equals()
Proper storing of data
Additionally, as already suggested in a comment to your question, you should start using arrays to save data like this.
Instead of
int numa = 0;
....
int numz = 0;
you should use arrays, or even better Map<Character,Integer>.
strs.substring(i, i) == "a" have two problems:
substring(i, i) creates string from i (inclusive), till i (exclusive) which means it creates empty string ""
this is not how we compare Strings. == may work sometimes if strings are pooled, but for dynamically created strings you need to use equals instead of == because Strings are objects, or even better use charAt(i) to get primitive char which you can be able to compare like strs.charAt(i) == 'a' (notice ' instead of ").
You can also use enhanced for loop on array of characters representing your string to avoid charAt. You should probably also be working on lower case characters as pointed in this comment. So your code can look more like
for (char ch : strs.toLowerCase().toCharArray()){
//do something based on value of `ch`
}
Try this:
It is a little bit shorter than your implementation (which is very good, but still a little bit verbose). Use Java 8 with this code, otherwise it won't compile.
What does it do?
If you understand that a string is nothing more but an array you can iterate over that array and see what kind of value is at the given index. The value at this index is put in a map (remember, a map is a key-value-store). So if you put the Integer 1 in the map where its key is "a", that means "a" occurs 1 time.
By reading the values at the appropriate indexii (very sophisticated plural form of index) with HashMap.get("a") and then incrementing the value by one, we have a nice little letter counter... without the need to predefine numa=0 and so forth. Give it a try and let me know if it werx.
package lettercounter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;
/**
*
* #author edm
*/
public class LetterCounter {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a String");
String strs = scan.nextLine();
//this map will be populated with the occurrence of the letters in the string.
HashMap<String, Integer> countenLetters = new HashMap<>();
//the next line generates a key-value-store whose key is the letter in the string
//and the value is the accumulated occurrence of said letter.
Arrays.asList(strs.split("")).stream().forEach((String letter) -> {
Integer count = 0;
try {
count = countenLetters.get(letter).intValue();
} catch (Exception e) {
//tried to access a non existing value in the map
//this happens if there is a letter which was not set in the map until now.
//i.e. the first time the letter is encountered.
//this is no error. could have done it with an if also.
}
countenLetters.put(letter, ++count);
});
//do with this stuff what you want;
countenLetters.forEach((k,v) -> {
System.out.println("Letter "+k+" occurs "+v+" times in the string.");
});
}
}