Error in my Java Program '.class' expected - java

class TwoDimArryAlloc {
public static void main(String[] args) {
int itemno[][] = new int[][] { {2234,2235,2236,2237,2238} , {3334,3335,3336,3337,3338} };
int it;
String itemdesc[][] = new String[][] {{"Womans Item1","Womans Item2","Womans Item3","Womans Item4","Womans Item5"},{"Mans Item1","Mans Item2","Mans Item3","Mans Item4","Mans Item5"}};
for (int i=0; i<2; i++)
{
for(int j=0; j<5; j++)
{
System.out.println(itemno[][]);
}
}
}
}
In the above program, I got the following error:
C:\Java Programs\TwoDimArryAlloc.java:18: '.class' expected
System.out.println(itemno[][]);
^
1 error
Process completed.
Can anyone help me solve this error?

You need to provide the array index:
System.out.println(itemno[i][j]);
^ ^

Instead of
System.out.println(itemno[][]);
you want to say
System.out.println(itemno[i][j]);
Without specifying which indexes you want, it is bad syntax, which confuses the compiler so much that it emits an impenetrable error message.
(The first pass of compilation is to construct a syntax tree, and this happens before the compiler figures out which names denote types and which ones denote variables. So when it sees itemno[][] it reasons that this could be the beginning of a valid expression if only itemno were the name of a type, but in that case the full expression would have to be itemno[][].class -- so that's what it asks for even though you meant something entirely different).

{
int itemno[][] = { {2234,2235,2236,2237,2238} , {3334,3335,3336,3337,3338} };
int it;
String itemdesc[][] = {{"Womans Item1","Womans Item2","Womans Item3","Womans Item4","Womans Item5"},{"Mans Item1","Mans Item2","Mans Item3","Mans Item4","Mans Item5"}};
for (int i=0; i<2; i++)
{
for(int j=0; j<5; j++)
{
System.out.println(""+itemno[i][j]);
}
}
}

You forgot to provide index in SYSO statement. Make it as follow:
System.out.println(itemno[i][j]);

You forgot to use the indexes:
System.out.println(itemno[i][j]);

Related

Calling method in main function

I'm just trying to call nested loops method to main function. I got 2 errors cannot be resolved or is not a field and the method patternA() is undefined for the type classNested. I know it's really common error but I still couldn't find a way to solve it.
Here's my main class
package nomerTuhuh;
import java.util.Scanner;
public class nestedLoops {
public static void main(String[] args) {
// get the total number of lines n.
classNested result = new classNested();
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of lines:");
result.n=sc.nextInt(); <-- error "cannot be resolved or is not a field"
result.patternA(); <-- error "the method patternA() is undefined for the type classNested"
}
}
And here's the method
package nomerTuhuh;
import java.util.Scanner;
public class classNested {
public int n;
void patternA(){
// Loop through the lines from 1 to n
System.out.println("Pattern A");
for (int i = 1; i <= n; i++) {
// Printing number increamentally from 1 to line number j
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
1st Error:
Description Resource Path Location Type
n cannot be resolved or is not a field nestedLoops.java /chapter1/src/nomerTuhuh line 14 Java Problem
2nd Error:
Description Resource Path Location Type
The method patternA() is undefined for the type classNested nestedLoops.java /chapter1/src/nomerTuhuh line 16 Java Problem
Can anyone please tell me what's wrong? Thank you.
Turns out my code works... I haven't tried to run it. I just stuck at the error reported but I'm not trying to ignore it and just run it. When I run it, the error magically disappear.
I'm using eclipse btw.
n variable should be static so that it can be accessed.
and your patternA() should be declared public something like this
public static int n;
public void patternA()
{}

Changing an array attempt

I posted this question up earlier and it was pretty much a lazy post as I didn't provide the code I had and as a result got negged pretty badly. Thought I'd create a new one of these ....
I have an array dogArray which takes ( name, secondname, dogname )
I want to be able to change the dogname:
here's my attempt :
public void changeName(Entry [] dogArray) {
Scanner rp = new Scanner(System.in);
String namgeChange = rp.next(); {
for (int i = 0; i < dogArray.length; i++){
for (int j = 0; j < dogArray[i].length; j++){
if (dogArray[i][j] == name){
dogArray[i][j] = nameChange;
}
}
}
}
For a start it doesn't like the fact I've used ' name ' although it is defined in the dogArray. I was hoping this would read input so that users are able to change 'name' to whatever they input. This will change the value of name in the array.
Do you guys think I'm getting anywhere with my method or is it a pretty stupid way of doing it?
Only one loop is necessary, also move your call to next.
public void changeName(Entry [] dogArray) {
Scanner rp = new Scanner(System.in);
for (int i = 0; i < dogArray.length; i++){
String nameChange = rp.next(); {
if(!dogArray[i].name.equals(nameChange)){
dogArray[i].name = nameChange;
}
}
}
You might want to make this function static as well and use accessors and mutators to change and get the name. Might want to tell the user what you want them to type as well. Also there is no point in testing if the name changed, if it changed then set it, if it didnt change then set it (it doesnt hurt). Just get rid of if and keep the code inside.

Java symbol not found when it should be

here's my code:
for(int i=0; i<plainTextUpper.length()-1; i++)
{
System.out.println(charCodeAt(i));
}
It won't compile though, because it says charCodeAt's symbol was not found. Am I missing a library? The only one I have imported right now is java.util.*
Assuming that this is Java and not Javascript, you probably want this:
for(int i=0; i<plainTextUpper.length()-1; i++)
{
System.out.println(plainTextupper.codePointAt(i));
}
Note that this will not process the last character of plainTextUpper. You probably also want to either get rid of the -1 or change the comparison operator to <= in the for termination test.

Ambiguous java error

public static int intersectionSizeMergeAndSort(studentList L1, studentList L2) {
int intersectionSize = 0;
int[] C = new int[L1.studentID.length+L2.studentID.length];
for(int i = 0; i<L1.studentID.length; i++){
C[i] = L1.studentID[i];
}
for(int i = 0; i<L2.studentID.length; i++){
C[i+L1.studentID.length] = L2.studentID[i];
}
Arrays.sort(C);
int pointer = 0;
while(pointer<((L1.studentID.length)+(L2.studentId.length))){
if(C[pointer] = C[pointer+1]){
intersectionSize = intersectionSize + 1;
pointer = pointer + 2;
}
else {
pointer == pointer + 1;
}
return intersectionSize;
}
}
I have this algorithm I am writing for an assignment. Every time I compile my code, I get an error that I cannot understand in order to debug.
Error is as follows:
Error: /Users/nah/Desktop/studentList.java:137: operator < cannot be applied to int,<nulltype>
the error is pointing to the while loop statement
Not sure if this is directly related to the same error but your line
while(pointer<((L1.studentID.length)+(L2.studentId.length))){
has mis-spelt the second "studentId" and should be "studentID"
while(pointer<((L1.studentID.length)+(L2.studentID.length))){
that is of course unless you happen to have two arrays defined for your studentID class, each with a different case.
I also agree with the other answers related to the "==" comparison operator, but you're telling us the error is related to the while statement
The only error I can see is:
if(C[pointer] = C[pointer+1]){
this should be:
if(C[pointer] == C[pointer+1]){
The following will not work in Java:
if (C[pointer] = C[pointer+1])
This is assignment, its type is int, it cannot be used in the if statement
While loop ? Ok. Debugging is done by simplifying expressions.
The only thing where we see < is the loop condition. Instead of
int pointer = 0;
while(pointer<((L1.studentID.length)+(L2.studentId.length))){
do
int pointer = 0;
int len = ((L1.studentID.length)+(L2.studentId.length));
while(pointer< len){
If error again, simplify the error-raising expression.
I think that this question is mistitled. It is ** ambigous only because you do not attempt to localize it. Post a question only if "<" does not work for two integers. The current question title must be: "how do you debug Java programs?"

Very simple code for number search gives me infinite loop

I am a newbie Computer Science high school student and I have trouble with a small snippet of code. Basically, my code should perform a basic CLI search in an array of integers. However, what happens is I get what appears to be an infinite loop (BlueJ, the compiler I'm using, gets stuck and I have to reset the machine). I have set break points but I still don't quite get the problem...(I don't even understand most of the things that it tells me)
Here's the offending code (assume that "ArrayUtil" works, because it does):
import java.util.Scanner;
public class intSearch
{
public static void main(String[] args)
{
search();
}
public static void search()
{
int[] randomArray = ArrayUtil.randomIntArray(20, 100);
Scanner searchInput = new Scanner(System.in);
int searchInt = searchInput.nextInt();
if (findNumber(randomArray, searchInt) == -1)
{
System.out.println("Error");
}else System.out.println("Searched Number: " + findNumber(randomArray, searchInt));
}
private static int findNumber(int[] searchedArray, int searchTerm)
{
for (int i = 0; searchedArray[i] == searchTerm && i < searchedArray.length; i++)
{
return i;
}
return -1;
}
}
This has been bugging me for some time now...please help me identify the problem!
I don't know about the infinite loop but the following code is not going to work as you intended. The i++ can never be reached so i will always have the value 0.
for (int i = 0; searchedArray[i] == searchTerm && i < searchedArray.length; i++)
{
return i;
}
return -1;
You probably mean this:
for (int i = 0; i < searchedArray.length; i++)
{
if (searchedArray[i] == searchTerm)
{
return i;
}
}
return -1;
I don't know what is the class ArrayUtil (I can not import is using my Netbeans). When I try to change that line with the line int[] randomArray = {1 , 2, 3, 5, 7, 10, 1 , 5}; It works perfectly.
And you should change the loop condition. I will not tell you why but try with my array and you will see the bug soon. After you see it, you can fix it:)
There are 4 basic issues here.
1. Putting searchedArray[i] == searchTerm before i < searchedArray.length can result in an out-of-bounds exception. You must always prevent that kind of code.
2. Your intention seems to be the opposite of your code. Your method name implies finding a search term. But, your code implies that you want to continue your loop scan until the search term is not found, although your loop won't do that either. Think of "for (; this ;) { that } " as "while this do that".
3. Place a break point at the beginning of "search". Then, with a small array, step through the code line by line with the debugger and watch the variables. They don't lie. They will tell you exactly what's happening.
4. Please use a standard IDE and compiler, such as Eclipse and Sun's JDK 6 or 7. Eclipse with JDK 7 is a serious combination that doesn't exhibit a strange "infinite loop" as you describe above.

Categories

Resources