I'm running this simple program to play around with arrays and nested for loops. For some reason my compiler can't identify the variable "r"? I don't know why it is doing this. Any suggestions?
public class ForLoop {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] mat = new int[4][8];
for(int r=0;r<mat.length;r++);
{
for(int c=0;c<mat[r].length;c++)
{
mat[r][c]=r*c+c/2+r*(c+1);
}
System.out.println(mat[0][2]);
}
}
}
The semicolon terminates the for body immediately (as an empty expression) here
for(int r=0;r<mat.length;r++);
{ //<-- not part of the for.
remove the semicolon so that the next block is part of the for loop
for(int r=0;r<mat.length;r++) {
Related
import java.util.Scanner;
public class KillBill {
public KillBill() {
// TODO Auto-generated constructor stub
}
public static void Main(String[] args) {
// TODO Auto-generated method stub
Scanner Scan = new Scanner (System.in);
}
}
IN 11th LINE .IT SAYS THE VALUE OF LOCAL VARIABLE SCAN IS NOT USED
If a variable is set and not used, it gives an error. You could correct the warning by using Scan, with something like:
public static void Main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner (System.in);
String text = scan.nextLine();
System.out.println(text);
}
The idea here is that every variable is being used now.
Well, to be entirely honest, you're Scanner object ISN'T BEING USED elsewhere in the code you've provided. But don't worry, this is just a warning (yellow jagged underline) and not an error (red jagged underline). In this case, it's just telling you that your program, at the moment, has an unused variable, whose deletion won't affect the code than what it currently is. Remember, a computer still has to read each line, and reading unused variables, especially objects, might slow your program down, even though by only a fraction of a millisecond. So it is advised to delete this unnecessary lines of code.
But remember, this is all relevant for the CURRENT situation, and not if you're gonna make changes to your program, by literally USING your unused variable.
Try this.
public class KillBill {
public KillBill(Scanner scan) {
// TODO Auto-generated constructor stub
int value = scan.nextInt();
System.out.println(value);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner Scan = new Scanner (System.in);
new KillBill(Scan);
}
}
That problem/warning is caused by unused variable. Not a serious issue.
This is not a problem to declare Scanner and not use it, the real problem is the signature of your main method signature, it should not UpperCase you have to use :
public static void main(String[] args) {
// ^^-----------------In your program it is M
Note java use CamelCase your names of variables shold start with lower case for the good practice (Scan should be scan)
public class practiceclock{
public static void main(String[] args) {
// TODO Auto-generated method stub
int x = 5;
addTonum(x);
System.out.println(gby);
}
public static void addTonum(int gby) {
gby = gby + 1;
}
}
Why doesn't this print out 6? I'm trying to print out the integer gby from my "addTonum" function. I know it's pretty basic.
In java, when you pass a variable as an argument into a method, you are passing a copy of it, not the original instance. To fix this, you can either make the variable global, or you can have the method return the integer.
Also, use
gby +=1;
As a shortcut.
I have a simple JForm that has some text fields.The problem is, my evaluate() method is not cycling through the text fields as I expected.All I get is the first text fields, of getText() that are executed. I'm stuck in this. Help would be great. Here is the code. Also here is a screen capture of the program: https://www.dropbox.com/s/w5ie4dfnc9wi216/Capture.JPG?dl=0
public MAppGest() {
initComponents();
}
public double a=2.6;
public double index0;
public Iterator itr;
public void getText(String ind, String inde){
evaluate();
index0=Double.parseDouble(txtNewIndex.getText())-Double.parseDouble(txtOldIndex.getText());
txtTotalIndex.setText(Double.toString(index0));
txtRoom1.setText(Double.toString(index0));
txtTotal1.setText(Double.toString((index0*a)));
}
public void evaluate(){
if(Boolean.valueOf(txtNewIndex.getText())&&Boolean.valueOf(txtOldIndex.getText())==true){
getArray();
}
else if(Boolean.valueOf(txtNewIndex2.getText())&&Boolean.valueOf(txtOldIndex2.getText())==true){
getArray();
}
else if(Boolean.valueOf(txtNewIndex3.getText())&&Boolean.valueOf(txtOldIndex3.getText())==true){
getArray();
}
else if(Boolean.valueOf(txtOldIndex4.getText())&&Boolean.valueOf(txOldIndex4.getText())==true){
getArray();
}
else if(Boolean.valueOf(txtNewIndex5.getText())&&Boolean.valueOf(txtOldIndex5.getTex())==true){
getArray();
}
else{
JOptionPane.showMessageDialog(MAppGest.this,"An error occured");
}
}
public void getArray() {
ArrayList<String> al=new ArrayList<>();
al.add(txtNewIndex.getText());
al.add(txtOldIndex.getText());
al.add(txtNewIndex2.getText());
al.add(txtOldIndex2.getText());
al.add(txtNewIndex3.getText());
al.add(txtOldIndex3.getText());
al.add(txtOldIndex4.getText());
al.add(txOldIndex4.getText());
al.add(txtNewIndex5.getText());
al.add(txtOldIndex5.getText());
for (int i = 0; i< al.size(); i++) {
String fields []=null;
fields[i] = al.get(i);
}
}
private void btnGrandTotalActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
getText(txtNewIndex.getText(),txtOldIndex.getText());
getText(txtNewIndex2.getText(),txtOldIndex2.getText());
getText(txtNewIndex3.getText(),txtOldIndex3.getText());
getText(txtOldIndex4.getText(),txOldIndex4.getText());
getText(txtNewIndex5.getText(),txtOldIndex5.getText());
}
If your problem is that evaluate() is only completing the first if statement, it's because you are using if-else statements when you want to just use if. If you use if-else, once the first if is verified true it will skip the rest. If my answer is not what you are looking for, then I advise you to ask a better question. Like the comments have said, give an SSCCE so the answerers aren't left guessing.
A side note: Boolean.valueOf() returns a boolean, so your ==true is unnecessary.
I have to do my homework in Greenfoot.
This part means that I have to save the position of Chess and then click reset.
Then, I have to choose load to put back the pieces of chess in the position they had before reset.
Since I don't know the exact size of the array, I know I can use List but it isn't allowed in the homework.
Nothing showed up on the screen but there is no error message.
Assume I have a class called Chess.
static Actor[] allChess;
public void save() // this is the save
{
Actor[] allChess = GWorld.getAllObjects("Chess");
}
public void load() // this is the load
{
if (allChess != null)
{
for (int i=0; i < allChess.length; i++)
{
Chess ch = (Chess) allChess[i];
GWorld.addOneObject(new Chess(ch.color, ch.rank), ch.getX(), ch.getY());
}
}
}
Thanks a lot!
allChess is redeclared as a local variable in save(). Do like this :
public void save() // this is the save
{
allChess = GWorld.getAllObjects("Chess");
}
My Main Code is :
public class Arrays {
public static void main(String[] args) {
//////////////// Sorting Arrays
String [] castNames = new String [6];
castNames[0] = "Zareyee Merila";
castNames[1] = "Hosseini Shahab";
castNames[2] = "Bayat Sareh";
castNames[3] = "Peyman Moadi";
castNames[4] = "Hatami Leila";
castNames[5] = "Farhadi Sarina";
Arrays.sort(castNames);
for (int number = 0 ; number < 6 ; number++) {
System.out.println(number + " : " + castNames[number]);
}
}
}
How can I fix this line of code:
Arrays.sort(castNames);
Without having to write this one:
private static void sort(String[] castNames) {
// TODO Auto-generated method stub
}
Okay, this question is a bit vague, but I expect you're just running into an issue with name collision.
You've named your class Arrays. This class has no static method called sort. There is, however, a utility in Java called java.util.Arrays which does implement a static sort method.
Your code is not calling the Java utility class, unless there's an import statement you haven't included.
Try changing the line to this: java.util.Arrays.sort(castNames);
Otherwise, you might consider renaming your Arrays class to something else.