Initializing an Big integer array [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
My code:
BigInteger R;
int Height;
BigInteger[][] results = new BigInteger[String.valueOf(Height)][R];
Getting an error for this please help me out.

Why are you doing
String.valueOf(Height)
???
Height is an int.
This code works :
BigInteger R = new BigInteger("5");
int Height = 5;
BigInteger[][] results = new BigInteger[Height][R.intValue()];
R is a BigInteger and the [] needs an Integer so we call the inValue method to turn it into an int.

Array sizes in java are ints. So assuming R is initialized:
BigInteger[][] results = new BigInteger[Height][R.intValue()];

Related

row insertion error when adding data to table(DefaultTable) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 days ago.
Improve this question
when i add data to the table from textfield i get a replacement error, no new row.
enter image description here
enter image description here
i already tried deleting the table and re-creating it but still the error.How can I solve this?
My code
private void xuLyThemVaoGioHang() throws Exception {
DefaultTableModel dtmGioHang = (DefaultTableModel) tblGioHang.getModel();
dtmGioHang.setRowCount(0);
String ma = txtMaSPBanHang.getText();
String ten = txtTenSPBanHang.getText();
String donGia = txtDonGiaBanHang.getText();
int soLuong = Integer.parseInt(txtSoLuongBanHang.getText());
donGia = donGia.replace(",", "");
int donGiaSP = Integer.parseInt(donGia);
spBUS.docListSanPham();
loadDataTableSanPhamBan();
dtmGioHang.addRow(new Object[]{
ma,ten,soLuong,donGiaSP, dcf.format(soLuong * donGiaSP)
});
new MyDialog("Thêm thành công!!", MyDialog.SUCCESS_DIALOG);
}

I am missing something here? NetBeans is saying that it's expecting a "(" or "[" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
public void getComputerPlayerTurn(String pid, ScopaCard card) {
//checkPlayerTurn(pid);
getPlayerHand(pid);
int tempInt = playerHand.size();
ArrayList<Rank> tempRanks = new ArrayList<Rank>
for (int i = 0; i < tempInt; i++) {
card = playerHand.get(i).get(i);
tempRanks.add(card.getRank());
}
}
That's my code for a method that I'm trying to create. The aim of this for loop is to get the cards in a players hand, and add their ranks to a different ArrayList. Everything is fine apart from the top line of the for loop (the one that has for (int i = 0; ...)). Here it is saying that a ( or [ is expected. I have no idea where though.
Cheers.
ArrayList<Rank> tempRanks = new ArrayList<Rank>
You forgot to put the constructor's brackets on the ArrayList. It should look like this:
List tempRanks = new ArrayList<>();
Your are missing the () at the end of array list declaration:
Syntax to declare array list ->
List<Rank> tempRanks = new ArrayList<Rank>();

Unable to print values of an array in Tabular format in Java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Trying to print out values of an arrays in tabular format (in the pic) but it doesn't seem to be working. I know it has something to do with the for loop and but I'm not sure how to code it properly so it shows.
The array is user inputed so it could vary on how many "gifts" there are.
If I forgot to include anything, sorry this is my first post
public static void printGiftsReport(String[] giftArray, double[] priceArray,
String[] whomArray, double overUnderAverage)
{
//
{
System.out.printf("%-15s%10s%15s%15s%25s" ," #.", "For", "Gift",
"Price", "Over/ Under Average\n");
System.out.printf("%-15s%10s%15s%15s%25s", "---", "---", "----",
"-----", "-------------------\n");
for(int i = 0; i > whomArray.length ; i++)
{
if (priceArray[i] > overUnderAverage)
{
System.out.printf("%-10s%10s%25s%10.2f%25s", (i+1),
whomArray[i], giftArray[i], priceArray[i], "over\n");
}
else
{
System.out.printf("%-10s%10s%25s%10.2f%25s", (i+1),
whomArray[i], giftArray[i], priceArray[i], "under\n");
}
you're doing i>whomArray.Length which will never run since i is initialized to 0. what you want instead is i<whomArray.Length.

JAVA Project Euler P. #8, Find greatest product from 1000 digit number [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
https://projecteuler.net/problem=8 link to problem
Find the thirteen adjacent digits in the 1000-digit number that have the greatest product.(number is in the code as String num)
The answer i am getting is "9205903071867879424" (wrong answer)
Please point out the mistakes in my code and also suggest your solution to solve the problem efficiently as possible.
I read the other threads about this problem but couldn't understand them and also there weren't enough efficient solutions.
public static void main(String[] args){
String num = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
long product=1;
long greatestp=0;
long limit=13;
for (int i=0; i<num.length()-13; i++){
product = 1;
for (int j=0; j<limit; j++ ){
product = (long) (product * (int) (num.charAt(j+i)));
}
if (greatestp<product){
greatestp = product;
}
}
System.out.println(greatestp);
}
P.S. I'm a beginner at JAVA, I would appreciate if you explain your solution in detail.
charAt gives you the ascii charcode, not the actual digit. Therefore the fastest way is to subtract the value of '0' from it:
product = product * (num.charAt(i+j) - '0');

int A does not equal int B [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have two integers that I want to compare. The first integer is created from a byte of utf-8 and the second integer is the one I want to check to see if it equals.
int a = 106;
int b = (int)byteArray[0]; //which actually equals 106 when you printstatement it
but....
(a==b) //always equals false
int i = 0; While(b != i) { println(i); i++;} //eventually escapes the loop and is true
Are primitives also referenced when created? And why does a never equal b unless I count all the way up to 106?
Is there a better way to compare bytes? because I've tried all forms of variables and they do not work either.
The problem is somewhere else in your code (in the part that you are not showing). This is why you are being suggested to provide an SSCCE.
The following works as expected (i.e. prints true):
public class Test
{
public static void main(String[] args) throws Exception
{
byte[] byteArray = new byte[] { 106 };
int a = 106;
int b = (int) byteArray[0];
if (a == b)
System.out.println("true");
}
}
Most probably, in your code byteArray[0] does NOT contain 106. An SSCCE would show this.

Categories