Related
This question already has answers here:
Plus one leetcode
(7 answers)
Closed 4 months ago.
I am trying to solve Plus One on Leetcode
The description is as follows:
You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's.
Increment the large integer by one and return the resulting array of digits.
Example 1:
Input: digits = [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Incrementing by one gives 123 + 1 = 124.
Thus, the result should be [1,2,4].
My code is:
class Solution {
public int[] plusOne(int[] digits) {
long temp=0;int c=0;
for(int i:digits)
{
temp=temp*10+i;
//System.out.println(temp);
}
//System.out.println(temp);
temp+=1;
//System.out.println(temp);
long copy=temp;
while(copy>0)
{
copy/=10;
c++;
}
int[] result=new int[c];
for(int i=c-1;i>=0;i--)
{
//System.out.println((int)temp%10);
result[i]=(int)temp%10;
// System.out.println(result[i]);
temp/=10;
}
return result;
}
}
The problem is when I am trying to extract the number in the unit's digit it is givig 9 instead of 1.
Expected output:[9,8,7,6,5,4,3,2,1,1]
My Output:[9,8,7,6,5,4,3,2,1,9]
There is nothing really wrong with your code...it just needs a little tweak.
The problem you are having is within the for loop that converts the incremented number held in temp to an int[] Array. Casting the temp % 10 to an int as in result[i] = (int) temp % 10; is going to produce an undesirable result. temp is a long and therefore shouldn't be cast to int until is has finished its modulo calculation. So, the simple solution would be to wrap the equation within parentheses, for example:
result[i] = (int) (temp % 10);
That should solve the immediate problem. An even bigger problem is the fact that you are limited to a numerical value that can only fit into a long data type. An int[] Array can hold an extremely large number if you were to take each element of that array and sequentially combine the digits. A long data type for example can only hold an unsigned value of 9223372036854775807. As you can see, this would create an int[] Array of 19 elements and that's a small Array.
What if the Array contained 100 elements (digits)? That's a pretty big number but not necessarily out of the realm of use. This is why you have been advised (in comments) to utilize the java.math.BigInteger class. With BigInteger, you can work with any size of integer number which is basically what you really need. Here is how you could carry out the same task using BigInteger:
public int[] plusOne(int[] digits) {
java.math.BigInteger num = java.math.BigInteger.ZERO;
// Convert int[] Array to a BigInteger number...
for (int b : digits) {
num = num.multiply(java.math.BigInteger.valueOf(10))
.add(java.math.BigInteger.valueOf(b));
}
// Add 1 to the determined BigInteger number
num = num.add(java.math.BigInteger.valueOf(1));
// Convert BigInteger number back to an Array
digits = num.toString().chars().map(c -> c - '0').toArray();
return digits;
}
As an example, let's run this method against an int[] Array of 100 elements. That would be an integer number consisting of 100 digits. Now that's a large number:
/* Auto-create an int[] array containing 100 elements of random digits
0 to 9 (the first digit of the Array will never be a 0). */
int[] array = new int[100];
for (int i = 0; i < 100; i++) {
int n = new java.util.Random().nextInt(9);
// Make sure the first digit isn't a 0.
if (i == 0 && n == 0) {
i--;
continue;
}
array[i] = new java.util.Random().nextInt(9);
}
System.out.println("Original: -> " + Arrays.toString(array));
/* Cycle the plusOne() method 12 times and display the array
within each cycle... */
for (int i = 0; i < 12; i++) {
array = plusOne(array);
// Display The array:
System.out.println("Plus One: -> " + Arrays.toString(array));
}
The Console Window should display something like this. You will need to scroll to the end to see the incrementing (plus one):
Original: -> [7, 8, 8, 3, 7, 3, 2, 1, 4, 6, 8, 8, 0, 7, 5, 8, 1, 7, 7, 0, 2, 2, 3, 2, 7, 2, 2, 2, 3, 4, 6, 7, 4, 6, 1, 4, 2, 4, 3, 1, 0, 4, 2, 8, 3, 4, 3, 8, 3, 6, 2, 5, 6, 6, 3, 5, 1, 3, 0, 7, 0, 0, 8, 7, 0, 0, 2, 4, 8, 5, 0, 6, 0, 2, 3, 5, 0, 1, 3, 3, 4, 1, 2, 8, 8, 1, 8, 3, 0, 5, 8, 8, 2, 2, 6, 6, 3, 5, 2, 6]
Plus One: -> [7, 8, 8, 3, 7, 3, 2, 1, 4, 6, 8, 8, 0, 7, 5, 8, 1, 7, 7, 0, 2, 2, 3, 2, 7, 2, 2, 2, 3, 4, 6, 7, 4, 6, 1, 4, 2, 4, 3, 1, 0, 4, 2, 8, 3, 4, 3, 8, 3, 6, 2, 5, 6, 6, 3, 5, 1, 3, 0, 7, 0, 0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 7, 0, 0, 8, 7, 0, 0, 2, 4, 8, 5, 0, 6, 0, 2, 3, 5, 0, 1, 3, 3, 4, 1, 2, 8, 8, 1, 8, 3, 0, 5, 8, 8, 2, 2, 6, 6, 3, 5, 3, 7]
Plus One: -> [7, 8, 8, 3, 7, 3, 2, 1, 4, 6, 8, 8, 0, 7, 5, 8, 1, 7, 7, 0, 2, 2, 3, 2, 7, 2, 2, 2, 3, 4, 6, 7, 4, 6, 1, 4, 2, 4, 3, 1, 0, 4, 2, 8, 3, 4, 3, 8, 3, 6, 2, 5, 6, 6, 3, 5, 1, 3, 0, 7, 0, 0, 8, 7, 0, 0, 2, 4, 8, 5, 0, 6, 0, 2, 3, 5, 0, 1, 3, 3, 4, 1, 2, 8, 8, 1, 8, 3, 0, 5, 8, 8, 2, 2, 6, 6, 3, 5, 3, 8]
This is pretty straight forward solution. Just what you do in the school: increment right to left with keeping carry for the next digit.
class Solution {
public int[] plusOne(int[] digits) {
boolean carry = true;
for (int i = digits.length - 1; carry && i >= 0; i--) {
carry = digits[i] == 9;
digits[i] = carry ? 0 : digits[i] + 1;
}
if (carry) {
int[] tmp = new int[digits.length + 1];
tmp[0] = 1;
System.arraycopy(digits, 0, tmp, 1, digits.length);
digits = tmp;
}
return digits;
}
}
I have a class ("TestProject") that is supposed to print the output of another class to the console, each time using a different input text file (e.g. "input01.txt", "input02.txt").
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class TestProject {
public static void main(String[] args) throws IOException {
for(int inputNumber = 2; inputNumber <= 7; inputNumber++) {
String[] arguments = new String[] {"input" + String.format("%02d", inputNumber) + ".txt", "1"};
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream newPS = new PrintStream(baos);
PrintStream oldPS = System.out;
System.setOut(newPS);
Project.main(arguments);
System.out.flush();
System.setOut(oldPS);
newPS.close();
System.out.println(baos.toString());
}
}
}
This works correctly for the first input file, as seen below:
arguments: "input02.txt" 0
1, 3, 80.5, 20, 60, 0, Stone, 0, 0
2, 3, 80.5, 20, 60, 0, Flame, 2, 2
1, 4, 80.0, 20, 60, 0, Stone, 0, 1
2, 4, 80.0, 20, 60, 0, Flame, 2, 2
1, 5, 79.5, 20, 60, 0, Stone, 0, 2
2, 5, 79.5, 20, 60, 0, Flame, 2, 2
1, 6, 79.0, 20, 60, 0, Stone, 1, 1
2, 6, 79.0, 20, 60, 0, Flame, 2, 2
But after this, every subsequent test prints only the final output of the first test:
arguments: "input03.txt" 0
1, 6, 79.0, 20, 60, 0, Stone, 1, 1
2, 6, 79.0, 20, 60, 0, Flame, 2, 2
arguments: "input04.txt" 0
1, 6, 79.0, 20, 60, 0, Stone, 1, 1
2, 6, 79.0, 20, 60, 0, Flame, 2, 2
arguments: "input05.txt" 0
1, 6, 79.0, 20, 60, 0, Stone, 1, 1
2, 6, 79.0, 20, 60, 0, Flame, 2, 2
arguments: "input06.txt" 0
1, 6, 79.0, 20, 60, 0, Stone, 1, 1
2, 6, 79.0, 20, 60, 0, Flame, 2, 2
arguments: "input07.txt" 0
1, 6, 79.0, 20, 60, 0, Stone, 1, 1
2, 6, 79.0, 20, 60, 0, Flame, 2, 2
I thought that this might have something to do with calling the main() method of another class multiple times, but nothing online seems to suggest that this is an issue.
When running the main program separately, the output works fine for all input files. This leads me to think that the problem exists in this short excerpt.
Thanks in advance, to all.
txt file containing the characteristics(estrato, size, age, bedrooms, bathrooms, floor, and label) of 205 apartments in Medellin, Colombia and i need to load the file and save each of the characteristics in their own specific variable so i can pass them through a conversions method to normalize the data to later be used in a distance formula for KNN classification. I have the current code:
import java.util.*;
import java.io.*;
/**
* collection of properties
* using arraylist
*/
public class Apartments
{
ArrayList<Property> apartmentList = new ArrayList<>();
String estrato = " ";
String size = " ";
String age = " ";
String beds = " ";
String baths = " ";
String floor = " ";
String label = " ";
public void convertFile () throws FileNotFoundException {
Scanner input = new Scanner(new File("ApartmentsFullList.txt"));
while(input.hasNextLine()){
String line = input.nextLine();
Scanner lineScan = new Scanner(line);
for(int i = 0; i < line.length(); i++){
char start = line.charAt(i);
if(start == '{'){
estrato = lineScan.next();
size = lineScan.next();
age = lineScan.next();
beds = lineScan.next();
baths = lineScan.next();
floor = lineScan.next();
}
}
System.out.println(estrato + " " + size + " " + age + " " + beds + " " +
baths + " " + floor + " " + label);
}
}
}
and the following .txt file:
01-Los Balsos 1380m {6, 430, 4, 3, 4, 22, Luxury},
02-El Tesoro 1100m {6, 263, 2, 3, 4, 21, Luxury},
03-Las Lomas 1030m {6, 270, 2, 3, 4, 16, Luxury},
04-Las Lomas 1020m {6, 270, 2, 3, 6, 18, Luxury},
05-Las Lomas 2600m {6, 780, 3, 7, 9, 33, Luxury},
06-Los Balsos 1100m {6, 380, 3, 4, 5, 14, Luxury},
07-Laureles 1350m {6, 494, 2, 3, 5, 17, Luxury},
08-Los Balsos 1100m {6, 445, 3, 4, 5, 10, Luxury},
09-Alejandria 1584m {6, 288, 1, 3, 4, 25, Luxury},
10-La Florida 1200m {6, 425, 3, 4, 6, 12, Luxury},
11-San Lucas 1200m {6, 200, 1, 3, 4, 19, Luxury},
12-Los Balsos 1250m {6, 347, 3, 4, 6, 23, Luxury},
13-Las Lomas 1300m {6, 350, 2, 6, 6, 9, Luxury},
14-El Tesoro 1350m {6, 240, 1, 3, 5, 11, Luxury},
15-San Lucas 2800m {6, 760, 1, 5, 8, 31, Luxury},
16-El Tesoro 1300m {6, 315, 2, 4, 5, 8, Luxury},
17-Las Palmas 1820m {6, 318, 1, 4, 7, 24, Luxury},
18-Las Lomas 1500m {6, 429, 1, 3, 5, 13, Luxury},
19-El Tesoro 2000m {6, 500, 3, 5, 7, 11, Luxury},
20-San Lucas 1800m {6, 603, 3, 5, 5, 17, Luxury},
21-El Tesoro 1300m {6, 315, 1, 3, 5, 5, Luxury},
22-El Tesoro 1300m {6, 294, 1, 4, 5, 16, Luxury},
23-San Lucas 2400m {6, 300, 1, 5, 5, 21, Luxury},
24-El Poblado 1180m {6, 382, 2, 3, 4, 12, Luxury},
25-Castropol 1190m {6, 302, 1, 3, 4, 26, Luxury},
26-El Poblado 2500m {6, 500, 1, 5, 8, 30, Luxury},
27-La Calera 1600m {6, 520, 3, 7, 7, 21, Luxury},
28-Castropol 1190m {6, 302, 1, 4, 4, 32, Luxury},
29-Las Lomas 1530m {6, 338, 2, 3, 5, 24, Luxury},
30-Superior 5100m {6, 578, 1, 4, 6, 27, Luxury},
31-Alejandria 930m {6, 186, 1, 4, 4, 19, Upper},
32-Lleras 940m {6, 175, 2, 2, 3, 11, Upper},
33-El Poblado 900m {6, 181, 1, 3, 3, 6, Upper},
34-San Lucas 800m {6, 175, 2, 3, 4, 7, Upper},
35-El Poblado 890m {5, 175, 3, 2, 3, 5, Upper},
36-San Lucas 900m {6, 148, 1, 3, 4, 19, Upper},
37-El Tesoro 800m {6, 180, 2, 3, 5, 21, Upper},
38-Los Balsos 930m {6, 186, 2, 3, 4, 12, Upper},
39-Los Balsos 990m {5, 300, 4, 3, 4, 4, Upper},
40-El Poblado 960m {6, 153, 1, 2, 3, 12, Upper},
41-El Poblado 900m {6, 186, 2, 3, 4, 6, Upper},
42-El Poblado 970m {6, 225, 4, 3, 5, 2, Upper},
43-San Lucas 920m {5, 168, 1, 3, 4, 4, Upper},
44-Laureles 920m {5, 230, 3, 3, 5, 22, Upper},
45-Superior 880m {5, 227, 2, 3, 4, 12, Upper},
46-El Poblado 980m {5, 298, 4, 4, 5, 11, Upper},
47-La Calera 890m {6, 251, 2, 3, 3, 9, Upper},
48-Los Balsos 950m {6, 285, 3, 4, 7, 3, Upper},
49-San Lucas 860m {6, 162, 1, 3, 4, 13, Upper},
50-Envigado 750m {5, 153, 1, 3, 3, 7, Upper},
51-M de Oro 830m {6, 209, 2, 3, 5, 18, Upper},
52-Superior 850m {6, 220, 2, 4, 4, 15, Upper},
53-Inferior 840m {6, 171, 1, 4, 5, 14, Upper},
54-Inferior 860m {5, 300, 4, 4, 6, 8, Upper},
55-San Lucas 850m {6, 175, 3, 3, 3, 11, Upper},
56-El Poblado 680m {6, 290, 3, 4, 3, 17, Upper},
57-Tomatera 805m {6, 251, 2, 4, 4, 4, Upper},
58-El Poblado 850m {6, 260, 2, 4, 4, 7, Upper},
59-Vizcaya 875m {5, 265, 4, 3, 5, 16, Upper},
60-Castropol 750m {5, 298, 4, 4, 5, 11, Upper},
61-Los Balsos 550m {5, 113, 1, 2, 3, 13, UM},
62-El Poblado 599m {6, 130, 1, 3, 2, 3, UM},
63-El Poblado 550m {6, 120, 1, 3, 3, 7, UM},
64-La Linde 520m {6, 109, 1, 2, 3, 5, UM},
65-El Poblado 560m {5, 120, 2, 2, 3, 7, UM},
66-El Poblado 530m {6, 172, 4, 3, 4, 8, UM},
67-El Poblado 610m {6, 144, 2, 3, 4, 7, UM},
68-El Tesoro 555m {6, 152, 2, 3, 4, 11, UM},
69-Villa Paula 520m {6, 165, 3, 3, 4, 1, UM},
70-La Calera 630m {6, 135, 1, 4, 4, 9, UM},
71-Simesa 521m {6, 124, 2, 3, 2, 16, UM},
72-El Tesoro 550m {6, 215, 4, 3, 3, 2, UM},
73-La Linde 520m {6, 160, 3, 3, 4, 4, UM},
74-La Florida 650m {5, 290, 4, 4, 4, 1, UM},
75-El Poblado 650m {6, 187, 2, 4, 4, 8, UM},
76-San Lucas 520m {6, 277, 4, 3, 4, 1, UM},
77-Castropol 550m {6, 117, 1, 3, 4, 5, UM},
78-San Lucas 570m {6, 140, 3, 4, 4, 7, UM},
79-Astorga 650m {6, 187, 2, 3, 5, 7, UM},
80-El Tesoro 545m {6, 136, 2, 2, 3, 6, UM},
81-Las Lomas 590m {6, 178, 3, 3, 4, 10, UM},
82-San Lucas 650m {6, 128, 1, 2, 3, 1, UM},
83-La Florida 650m {6, 230, 4, 4, 4, 8, UM},
84-Alejandria 600m {6, 210, 3, 3, 4, 1, UM},
85-Las Lomas 550m {5, 139, 1, 3, 3, 9, UM},
86-Los Parra 545m {6, 125, 1, 2, 3, 14, UM},
87-El Poblado 500m {6, 239, 4, 3, 3, 1, UM},
88-Alejandria 595m {6, 113, 1, 2, 3, 4, UM},
89-Castellana 600m {5, 200, 4, 3, 3, 2, UM},
90-Envigado 540m {5, 125, 1, 2, 3, 7, UM},
91 – {5, 109, 3, 4, 3, 8, Middle},
92- {5, 97, 3, 3, 3, 9, Middle},
93- {4, 93, 4, 3, 2, 6, Middle},
94- {6, 126, 3, 2, 3, 6, Middle},
95- {6, 124, 4, 3, 2, 1, Middle},
96- {5, 127, 3, 3, 3, 3, Middle},
97- {5, 115, 4, 2, 1, 2, Middle},
98- {5, 110, 4, 4, 2, 1, Middle},
99- {4, 72, 3, 3, 2, 5, Middle},
100- {5, 71, 1, 2, 1, 10, Middle},
101- {4, 72, 1, 2, 2, 19, Middle},
102- {4, 69, 3, 3, 2, 12, Middle},
103- {4, 90, 1, 3, 3, 24, Middle},
104- {3, 68, 4, 3, 2, 10, Middle},
105- {5, 76, 3, 1, 3, 13, Middle},
106- {6, 142, 4, 3, 4, 1, Middle},
107- {6, 71, 4, 3, 2, 2, Middle},
108- {3, 116, 4, 3, 5, 1, Middle},
109- {6, 55, 1, 2, 2, 3, Middle},
110- {6, 150, 4, 3, 3, 8, Middle},
112- {4, 80, 3, 2, 3, 4, Middle},
113- {3, 62, 3, 3, 2, 2, Middle},
114- {6, 154, 4, 3, 3, 9, Middle},
115- {4, 70, 3, 3, 2, 5, Middle},
116- {6, 119, 4, 3, 4, 3, Middle},
117- {3, 55, 3, 3, 2, 2, Middle}
118- {4, 72, 1, 2, 2, 4, Middle},
119- {5, 170, 3, 4, 3, 8, Middle}
120- {5, 98, 1, 3, 2, 17, Middle},
121- {5, 75, 4, 2, 2, 4, Middle},
122- {6, 76, 3, 2, 2, 3, Middle},
123- {3, 98, 4, 3, 2, 1, Middle},
124- {4, 64, 1, 2, 2, 9, Middle},
125- {5, 108, 4, 3, 2, 3, Middle},
126- {5, 125, 4, 4, 3, 1, Middle},
127- {5, 124, 3, 3, 3, 3, Middle},
128- {5, 106, 3, 3, 3, 5, Middle},
129- {5, 118, 4, 3, 3, 6, Middle},
130- {5, 88, 3, 3, 3, 1, Middle},
131- {5, 79, 3, 3, 2, 6, Middle},
132- {5, 120, 3, 3, 2, 4, Middle},
133- {6, 78, 1, 2, 2, 29, Middle},
134- {5, 95, 1, 3, 3, 12, Middle},
135- {5, 78, 4, 3, 2, 3, Middle},
136- {5, 80, 3, 3, 2, 4, Middle},
137- {5, 78, 3, 2, 2, 1, Middle},
138- {5, 120, 4, 3, 3, 4, Middle},
139- {5, 92, 3, 3, 2, 7, Middle},
140- {5, 110, 3, 3, 3, 2, Middle},
141- {6, 124, 1, 3, 3, 1, Middle},
142- {5, 96, 3, 3, 2, 9, Middle},
143- {5, 98, 4, 3, 3, 3, Middle},
144- {5, 92, 3, 2, 3, 8, Middle},
145- {4, 32, 4, 1, 1, 2, LM},
146- {3, 45, 3, 2, 1, 1, LM},
147- {3, 50, 2, 3, 1, 2, LM},
148- {3, 48, 2, 3, 2, 3, LM},
149- {2, 48, 1, 3, 2, 1, LM},
150- {2, 48, 1, 3, 2, 2, LM},
151- {4, 45, 2, 2, 1, 13, LM},
152- {4, 45, 3, 3, 1, 11, LM},
153- {4, 30, 1, 1, 1, 24, LM},
154- {3, 45, 2, 3, 1, 13, LM},
155- {3, 60, 2, 3, 1, 4, LM},
156- {3, 50, 2, 2, 1, 1, LM},
157- {3, 48, 2, 3, 1, 3, LM},
158- {3, 30, 1, 1, 1, 2, LM},
159- {4, 30, 3, 2, 1, 1, LM},
160- {3, 37, 1, 2, 1, 11, LM},
161- {3, 54, 2, 3, 2, 10, LM},
162- {3, 42, 1, 3, 1, 4, LM},
163- {3, 45, 2, 3, 1, 3, LM},
164- {3, 54, 3, 3, 2, 10, LM},
165- {3, 54, 2, 3, 2, 9, LM},
166- {3, 54, 1, 2, 1, 4, LM},
167- {3, 80, 4, 3, 2, 2, LM},
168- {3, 50, 1, 3, 2, 13, LM},
169- {3, 34, 2, 1, 1, 3, LM},
170- {3, 55, 1, 3, 1, 1, LM},
171- {2, 45, 1, 2, 1, 5, LM},
172- {3, 42, 3, 2, 1, 12, LM},
173- {3, 39, 1, 1, 1, 3, LM},
174- {4, 45, 3, 2, 1, 7, LM},
175- {3, 38, 1, 2, 1, 3, LM},
176- {2, 42, 1, 2, 1, 4, Lower},
177- {1, 43, 1, 3, 1, 4, Lower},
178- {2, 25, 1, 1, 1, 1, Lower},
179- {2, 42, 1, 2, 1, 1, Lower},
180- {1, 49, 1, 2, 1, 1, Lower},
181- {1, 46, 2, 3, 1, 2, Lower},
182- {2, 18, 1, 1, 1, 4, Lower},
183- {2, 45, 3, 3, 1, 2, Lower},
184- {3, 18, 1, 1, 1, 3, Lower},
185- {1, 40, 2, 2, 1, 2, Lower},
186- {2, 40, 2, 2, 1, 4, Lower},
187- {2, 41, 2, 3, 1, 2, Lower},
188- {3, 50, 3, 2, 1, 1, Lower},
189- {1, 44, 4, 2, 1, 1, Lower},
190- {1, 55, 3, 3, 1, 2, Lower},
191- {2, 17, 1, 1, 1, 1, Lower},
192- {1, 40, 3, 2, 1, 1, Lower},
193- {1, 41, 4, 2, 1, 4, Lower},
194- {1, 40, 2, 2, 1, 3, Lower},
195- {3, 16, 1, 1, 1, 2, Lower},
196- {3, 45, 1, 2, 1, 1, Lower},
197- {3, 48, 1, 3, 1, 2, Lower},
198- {2, 43, 1, 3, 1, 4, Lower},
199- {1, 50, 4, 2, 1, 1, Lower},
200- {1, 60, 4, 2, 1, 1, Lower},
201- {1, 40, 1, 2, 1, 2, Lower},
202- {3, 32, 1, 1, 1, 1, Lower},
203- {2, 44, 1, 2, 1, 3, Lower},
204- {1, 40, 4, 2, 1, 1, Lower},
205- {2, 36, 2, 2, 1, 5, Lower}
but I am getting all text when I print and all i need is what is inside the curly brackets
If anyone can please answer my question and/or has any guidance on KNN java implementation in my specific case it would be of great help!
Thanks
Delete this line
Scanner lineScan = new Scanner(line);
Move it to
if (start == '{') {
Scanner lineScan = new Scanner(line.substring(i));
estrato = lineScan.next();
size = lineScan.next();
age = lineScan.next();
beds = lineScan.next();
baths = lineScan.next();
floor = lineScan.next();
break; // consider adding a break here
}
The problem is you made the scanner for the whole line, before you even search where the { character is.
I have an array of Objects which is [36] long. I have to randomly distribute these objects between owners, one object can only have one owner. The user can set 2 - 5 owners and it goes like this:
2 owners - 14 Object / owner - 8 Object without owner
3 owners - 10 / owner - 6 empty
4 owners - 8 / owner - 4 empty
5 owners - 6 / owner - 6 empty
I want to ask for example in case 2 how can i set 6 random Objects owner to 0 (without owner), and 10 random Objects for each owner?
It's not exactly clear from the question what you are trying to accomplish, but you can achieve some shuffling with the Collections.shuffle() function.
int[] shuffle = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 };
List<Integer> intList = new ArrayList<Integer>();
for (int i : shuffle) {
intList.add(i);
}
Collections.shuffle(intList);
System.out.println(intList);
One possible result:
[1, 2, 0, 2, 2, 1, 1, 1, 2, 2, 2, 1, 0, 1, 1, 0, 0, 2, 1, 2, 2, 1, 0, 1, 0, 0, 2, 1, 2, 2, 1, 2, 1, 2, 0, 1]
References:
shuffle
Problem Solved
First i created a 2d array which contains the 4 different case
public int[][] shuffle = {{ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
{ 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
{ 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4},
{ 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5}};
Then in a method i copied out the needed row to a new array based on user input. I used Fisher - Yates shuffle on the primitive 1d array and looped threw the Objects where i called a setter methor for each owner.
Looks like this:
for(int i = 0; i < objectArray.length; i++){
int helper = shuffle1d[i];
objectArray[i].setObjectOwner(helper);
}
This pertains to a problem that I solved with far more elegant code. I was fooling around and trying to solve the problem in a different way using very basic code. The problem is that the number I am getting (2091059712) is not correct. The correct answer is 235146240000. This is driving me insane. Here is the broken code. I am trying to find the 13 adjacent digits within a 1000 digit number that have the largest product.
public class AdjacentProducts {
public static void main(String[] args) {
int[] x = new int[] { 7, 3, 1, 6, 7, 1, 7, 6, 5, 3, 1, 3, 3, 0, 6, 2,
4, 9, 1, 9, 2, 2, 5, 1, 1, 9, 6, 7, 4, 4, 2, 6, 5, 7, 4, 7, 4,
2, 3, 5, 5, 3, 4, 9, 1, 9, 4, 9, 3, 4, 9, 6, 9, 8, 3, 5, 2, 0,
3, 1, 2, 7, 7, 4, 5, 0, 6, 3, 2, 6, 2, 3, 9, 5, 7, 8, 3, 1, 8,
0, 1, 6, 9, 8, 4, 8, 0, 1, 8, 6, 9, 4, 7, 8, 8, 5, 1, 8, 4, 3,
8, 5, 8, 6, 1, 5, 6, 0, 7, 8, 9, 1, 1, 2, 9, 4, 9, 4, 9, 5, 4,
5, 9, 5, 0, 1, 7, 3, 7, 9, 5, 8, 3, 3, 1, 9, 5, 2, 8, 5, 3, 2,
0, 8, 8, 0, 5, 5, 1, 1, 1, 2, 5, 4, 0, 6, 9, 8, 7, 4, 7, 1, 5,
8, 5, 2, 3, 8, 6, 3, 0, 5, 0, 7, 1, 5, 6, 9, 3, 2, 9, 0, 9, 6,
3, 2, 9, 5, 2, 2, 7, 4, 4, 3, 0, 4, 3, 5, 5, 7, 6, 6, 8, 9, 6,
6, 4, 8, 9, 5, 0, 4, 4, 5, 2, 4, 4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6, 0, 5, 8, 8, 6, 1, 1, 6, 4, 6, 7, 1, 0, 9, 4, 0, 5, 0, 7,
7, 5, 4, 1, 0, 0, 2, 2, 5, 6, 9, 8, 3, 1, 5, 5, 2, 0, 0, 0, 5,
5, 9, 3, 5, 7, 2, 9, 7, 2, 5, 7, 1, 6, 3, 6, 2, 6, 9, 5, 6, 1,
8, 8, 2, 6, 7, 0, 4, 2, 8, 2, 5, 2, 4, 8, 3, 6, 0, 0, 8, 2, 3,
2, 5, 7, 5, 3, 0, 4, 2, 0, 7, 5, 2, 9, 6, 3, 4, 5, 0 };
int a = 0;
int b = 1;
int c = 2;
int d = 3;
int e = 4;
int f = 5;
int g = 6;
int h = 7;
int i = 8;
int j = 9;
int k = 10;
int l = 11;
int m = 12;
long result = 0;
long largest = 0;
while (m < 1000) {
result = x[a] * x[b] * x[c] * x[d] * x[e] * x[f] * x[g] * x[h]
* x[i] * x[j] * x[k] * x[l] * x[m];
if (result > largest) {
largest = result;
a++;
b++;
c++;
d++;
e++;
f++;
g++;
h++;
i++;
j++;
k++;
l++;
m++;
} else {
a++;
b++;
c++;
d++;
e++;
f++;
g++;
h++;
i++;
j++;
k++;
l++;
m++;
}
}
System.out.println(largest);
}
}
Every multiplication is another chance for overflow. Your answer overflows before it ever gets put in result. One solution would be to change the definition of your array to
long[] x = new long[]
The same problem shows up in Puzzler #3 in the Java Puzzlers book:
public class LongDivision {
public static void main(String[] args) {
final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000;
final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;
System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY;
}
}
Here's the explanation Bloch and Gafter give:
The problem is that the computation of the constant MICROS_PER_DAY does overflow. Although the result of the computation fits in a long with room to spare, it doesn't fit in an int. The computation is performed entirely in int arithmetic and only after the computation completes is the result promoted to a long. By then, it's too late.
...
So why is the computation performed in int arithmetic? Because all the factors that are multiplied together are int values. When you multiply two int values you get another int value. Java does not have target typing, a language feature wherein the type of the variable in which a result is to be stored influences the type of the computation.
(Java uses target typing in certain contexts, in array initializers and now with lambdas, just not in general.)
The same text also points out that only the first value in the expression being multiplied needs to be a long, so you could keep the declaration of the array using ints and only change this line:
result = 1L * x[a] * x[b] * x[c] * x[d] * x[e] * x[f] * x[g] * x[h]
* x[i] * x[j] * x[k] * x[l] * x[m];