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()
{}
Related
A couple days ago I started learning Java and I implemented an algorithm which outputs all results of the MU-puzzle over a certain amount of iterations.
The code worked perfectly fine until I decided to make it so that you have to enter the amount of iterations through a JOptionPane window.
When I tried to import javax.swing.JOptionPane, that didn't work, so I looked up a tutorial, which has helped me before in that matter, however, somehow it didn't work this time (this is the tutorial in case you're curious: https://www.youtube.com/watch?v=GBzwSf8bEdQ).
Or I guess I should really say that it did fix the issue of import javax.swing.JOptionPane not being recognized by Eclipse (i.e. the red, squiggly line underneath it is gone).
But now whenever I try and access any member variables/functions of JOptionPane the program gives me an error and Intellisense doesn't even show the them in the first place, all it shows is class: Class<javax.swing.JOptionPane>.
Now all that happens when I try to compile my code is that I get this error message (even if I revert it back to its previously functional state):
So right now this is what my package (if that's the right term for it) looks like:
This is my code (again please note that I'm a beginner so it's not perfect)
This version is the one with JOptionPane:
(all that was different in the other version that worked (but doesn't anymore), was that the JOptionPane line is missing, the function miuExecute() did not exist and the code inside miuExecute() was put directly into the main function with iterations being just a fixed positive integer)
package miuSystem;
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class Main {
public static void main(String[] args) {
miuExecute();
}
static void miuExecute() {
int iterations = JOptionPane.showInputDialog("Enter the amount of iterations you'd like to perform");
String input = "MI";
ArrayList<String> res = new ArrayList<String>();
ArrayList<String> miuIterations = new ArrayList<String>();
miuIterations.add(input);
for (int u = 0; u < iterations; u++) {
for(int q = 0; q < miuIterations.size(); q++) {
res.add(rule1(miuIterations.get(q)));
res.add(rule2(miuIterations.get(q)));
res.add(rule3(miuIterations.get(q)));
res.add(rule4(miuIterations.get(q)));
}
for (int i = 0; i < res.size(); i++) {
for (int j = 0; j < res.size(); j++) {
if (j == i) {continue;}
else if (res.get(i).equals(res.get(j))) {
res.remove(i);
i--;
break;
}
}
}
System.out.println("Iteration #"+(u+1)+":");
System.out.println(res);
System.out.println();
miuIterations.clear();
for(String s : res) {
miuIterations.add(s);
}
res.clear();
}
}
static String rule1(String input) {
if (input.charAt((input.length())-1) == 'I') {
return input+"U";
}
else {
return input;
}
}
static String rule2(String input) {
String copy = "";
for (int i = 1; i<input.length(); i++)
{
copy = copy + input.charAt(i);
}
return input+copy;
}
static String rule3(String input) {
return input.replace("III", "U");
}
static String rule4(String input) {
return input.replace("UU","");
}
}
As already stated above, I tried following this tutorial (https://www.youtube.com/watch?v=GBzwSf8bEdQ) but it didn't work.
Googling my problem(s) (both on and off of StackOverflow) also yielded either no result at all / results that I don't understand yet and would take too long to understand instead of just asking or results that didn't help me.
Edit: as requested here is what I think was meant by "show us the contents of your module-info.java" (if not please just tell what else you need) the part of the image thats censored is obv. just my name, which i don't like.
enter image description here
I'm trying to write a program (for an assignment) that will – based on two absolute parameters – determine wether or not someone will be eligible for financial support. To accomplish this I have created three new methods, and currently I'm down to 1 error that I simply cannot comprehend. The error says "identifier expected" on this part of the code (in the bottom:
static void metodeTekst(tekst)
Here is my code:
import java.util.Scanner;
public class MinOppgave2a
{
public static void main (String[] args)
{
Scanner in = new Scanner (System.in);
String tastatur;
System.out.println();
System.out.println("\r\n" + "For aa kunne beregne ditt stoettegrunnlag, må du oppgi alderen din: ");
tastatur = in.nextLine();
int alder = Integer.parseInt(tastatur);
System.out.println();
System.out.println("\r\n" + "Bor du hjemme? Skriv 1 for «Ja», eller 2 for «Nei»: ");
tastatur = in.nextLine();
int hjemme = Integer.parseInt(tastatur);
System.out.println();
int i = 0;
while (i < 2)
{
i = i + 0;
}
metodeAlder(alder);
metodeHjemme(hjemme);
System.out.println("Du vil faa full studiestoette!");
}
static void metodeAlder(int alder)
{
if (alder <= 18 || alder >= 45)
i++;
else
metodeTekst(tekst);
}
static void metodeHjemme(int hjemme)
{
if (hjemme == 2)
i++;
else
metodeTekst(tekst);
}
static void metodeTekst(tekst)
{
String tekst = ("Du vil faa redusert eller ingen studiestoette.");
System.out.println(tekst);
}
}
I've tried for a good while to find an answer online, but in most cases where people have gotten this error they have forgotten to write a main method. I have not, as you see. Thanks in advance – I'm really new to Java, and appreciate all help!
Your method metodeTekst is wrong. It should either be
static void metodeTekst()
{
String tekst = "Du vil faa redusert eller ingen studiestoette.";
System.out.println(tekst);
}
or
static void metodeTekst(String tekst)
{
System.out.println(tekst);
}
but then you have to call it like this:
metodeTekst("Du vil faa redusert eller ingen studiestoette.");
Edit for OP's comment:
There are many additional problems with your code. The variable i in metodeAlder and metodeHjemme is outside if the scope of where i is initially defined (in main). If you really need to alter this variable inside your methods you have to declare it as a static field outside of your main.
Besides after everything compiles fine, this part
while (i < 2)
{
i = i + 0;
}
is pointless. You'll end up in an endless loop since you never change i so it will always be < 2. I would remove all occurrences of i since it doesn't do any meaningful and is not at all needed in your program.
Edit 2:
For your question as to why the compiler only now complains about i: It first checked, if all your method signatures were correct. Since it ran into previous error (with the wrong signature), it didn't need to check further. Now that you've fixed it, the compiler could continue to check the syntax "inside" the methods. And since i is only defined outisde of the method's scopes, it didn't know what i is supposed to be, hence the next compiler error.
Edit 3:
To make my explanation clearer look at the following example:
{
// Outer Scope
{
// Inner Scope A
}
{
// Inner Scope B
}
{
// Inner Scope C
}
}
Here, the three inner scopes A, B, C are isolated from each other. So variables defined in inner scope A are not visible to the other inner scopes B and C. In contrast, variables defined in the outer scope are visible to all inner scopes, since they are part of the outer scope as well.
I'm not trying to do anything too fancy here really, there is something very simple here that i'm not noticing.
a little premise, this is a simple grade converter, run the driver, enter a double numberGrade(88.7, 90, 67.2, etc) and it will then go to the service and check the statements like this that i will have for grades A-F
if (enterGrade >= 90 && enterGrade <= 100)
grade = 'A';
keep getting an error though
import java.util.Scanner;
public class CondTest
{
public static void main(String[] args)
{
System.out.print("Please enter your number grade for a letter grade! >");
Scanner scanObj = new Scanner(System.in);
Conditionals condObj = new Conditionals();
int enterGrade = scanObj.nextInt();
condObj.Conditionals(enterGrade);
}
}
and heres my method in the service class to reference
public class Conditionals
{
public double letterGrade(double enterGrade)
{
char grade = 0;
if (enterGrade >= 90 && enterGrade <= 100)
grade = 'A';
return grade;
}
}
After troubleshooting more i thought maybe changing the
condObj.conditionals(enterGrade);
to
double condObj.conditionals(enterGrade);
but no luck.
I keep recieving this same error
CondTest.java:18:error: cannot find symbol
condObj.conditionals(enterGrade);
As far as I can see, there just is no method called conditionals in your Conditionals class - only a method called letterGrade. Maybe you should call this one instead?
Change it to
condObj.letterGrade(enterGrade);
And: Scanner.nextInt() returns an int, do you have a method that accepts an int param?
I get the error message cannot find symbol, symbol: method books(int[], int) when I try to compile the following code.
For further explanation about what I want the code to do, see below the code.
public class books {
public void main(String[] args) {
int searchValue = 0, index;
int refNum[] = new int[4]; // the array
refNum[0] = 4; //numbers to refer to (aka to find)
refNum[1] = 6;
refNum[2] = 10;
refNum[3] = 12;
refNum[4] = 14;
int input = Integer.parseInt(enterValue.getText()); //takes user's input
for (int x = 0; x < refNum.length; x++) {
refNum[x] = input; //Tells refNum value to be
}
searchValue = input;
index = books(refNum, searchValue); //"books" is underlined
if (index != -1) {
binarySearchField.setText("We found: " + index);
} else {
binarySearchField.setText("Sorry! Not Found!");
}
public static Boolean binarySearch(String [] refNum, int left, int right, String search){
//Boolean code for later
}
This program uses binary search to find values stored in array after user inputs number, if they match then the item is successfully found. User inputs desired number in 'enterNumber' which is a TextField. Now in my code )which I'm 78% sure will work if it wasn't for this one little thing) there is an all important that is underlined which shouldn't be, (I've commented beside the line to show)
Now I had thought I was suppose to put the class name there, but apparently since it is underlined that is not the case. Any ideas on what I should be putting there in it's place?
And I apologize for the question may be a bit misleading on what I'm really asking, I just wasn't sure how to word the question.
The line
index = books(refNum, searchValue);
seems to be underlined because you have no method called books that takes an int[] and an int as arguments in your books class definition.
Now I had thought I was suppose to put the class name there Why do you assume you have to put the class name there? Figure out what you are trying to do with this code and then you will understand what goes in that line (at least in pseudocode).
Also it seems like you have a method declared directly inside another method. That is not legal in java. If this is not the case, please show us correct code.
books is your class's name..that might be the reason you are getting this error. You can't call constructor like a method. Change class's name to Books or something else..or change method's name
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]);