thank you in advance for helping out with this relatively simple (I hope) problem that I seem to be encountering. whenever I try to compile my programming assignment, I am met with a "cannot find symbol error." I point out where the error occurs in the code itself. Thanks again!
public class SSN
{
private int one;
private int two;
private int three;
public SSN(int first, int second, int third) throws Exception
{
if(first > 99 || first < 1 || second > 999 || second < 1 || third > 9999 || third < 1)
{
}
else
{
one = first;
two = second;
three = third;
}
}
//method that turns ###-##-#### string into 3 int SSN object
public static SSN valueOf(String ssn)
{
String firstpart;
firstpart = ssn.substring(0, 2);
String secondpart;
secondpart = ssn.substring(4, 5);
String thirdpart;
thirdpart = ssn.substring(7, 10);
int One = Integer.parseInt(firstpart);
int Two = Integer.parseInt(secondpart);
int Three = Integer.parseInt(thirdpart);
System.out.println(firstpart);
//This is where the cannot find symbol error occurs (return SSN(One, Two, Three), //and I am clueless as to why.
//Any insight as to why this error is occurring would be much appreciated!
return SSN(One, Two, Three);
}
public String toString()
{
return one + "-" + two + "-" + three;
}
}
return new SSN(One, Two, Three);
^^^
You're trying to create a new SSN(...) by calling the constructor.
The compiler si looking for a method named "SSN" but there is not such method ( the compiler can't find that symbol ) You were trying to create a new object not invoking a method thus you need to inlcude the new keyword as Erik and SLaks said.
return new SSN( One, Two, Three );
Related
public class GameEntry {
private String name;
private int score;
public GameEntry(String n, int s){
name = n;
score = s;
}
public String getName(){
return name;
}
public int getScore(){
return score;
}
public String toString(){
return "(" + name + ", "+ score + ")";
}
}
public class Scoreboard {
private int numEntries = 0;
public GameEntry[] board;
public Scoreboard(int capacity){
board = new GameEntry[capacity];
}
**public void add(GameEntry e){**
//System.out.println(board[numEntries - 1].getScore());
int newScore = e.getScore();
//Is the new entry really a high score
//*****This is the line i refer to as******
if (numEntries < board.length || newScore > board[numEntries - 1].getScore()) {
if (numEntries<board.length) {
numEntries++;
}
//shift any lower scores rightward to make room for the new entry
int j = numEntries - 1;
while(j>0 && board[j-1].getScore()<newScore){
board[j] = board[j-1]; //shift entry from j-1 to j
j--; // and decrement j
}
board[j] = e; // when done add a new entry
}
}
}
I would like to draw your attention inside the Scoreboard class, to its add method.
My question is why this code does not fail.
The first time the add method runs, the numEntries is equal to 0. So inside the if statement the board[numEntries - 1].getScore should get an IndexOutOfBounds.
When i put it before the if i get the proper exception. Does the if catch the exception?
I have printed the value of (numEntries - 1) and i get -1. But yet inside the if ot does not seem to bother it.
The line i refer to is inside the add method the first if.
if (numEntries < board.length || newScore > board[numEntries - 1].getScore())
Simple Answer: Short-circuit evaluation of logical or.
When the first part of the condition, i.e. numEntries < board.length evaluates to true, the second part after || is not evaluated at all.
You are checking the following expression first:
numEntries < board.length
Then you have an OR (||) followed by the expression you are asking about.
The compiler checks the expression from left to right. So if the above expression is true, it just enters the if and starts executing it's contents without checking other expressions.
I think I'm missing something fundamental. I'm trying to display a character and an integer into a string while using a conditional.
Code:
public String display()
{
String Term = Character.toString(B) + Integer.toString(C);
int length = Term.length();
if (length == 1) {
Term = Character.toString(B);
}
if (length > 1) {
Term = Character.toString(B) + Integer.toString(C);
}
return Term;
}
public char getB()
{return B;
}
public int getC()
{return C;
}
Where B is a character and C is an integer. So the error that keeps coming up looks like this:
A.display() should return "A" expected: < A [] > but was: < A[1] >
I've been trying to fix this for hours now and to no avail. How should I go about fixing this error? Thanks.
First, this code won't run as it is because you should declare your character and integer variables.
Plus, you're getter-methods are used in case you have another interfering class with this class.
And if you want a code for such a problem don't hesitate to ask me.
I'm currently "learning" JavaScript + Android Studio for school and I got a little problem for which I can't find the right answer on Google:
I want to know if an int variable has a specific number, for example, I'm looking for the number 7 now int numberOne = 25824 doesn't have a 7 inside, but int numberTwo = 12387 does have one. Is there a way to search for a specific number in int variables?
I tried converting the int into a new string variable, but somehow this doesn't work :(
Here's some code I'm working with:
public int round = 1;
public String nummerSieben = "" + round;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (round % 7 == 0 || nummerSieben.contains("7")==true) {
....
} else {
....
}
}
});
Thank you for your help!
public int round = 1;
public String nummerSieben = "" + round; // nummerSieben is now "1"
You're hard-coding the value of nummberSieben. You need presumably get some value from the view, and test that. If you get it as in int, use
Integer.toString(i).contains("7") // i is whatever number you get from your view.
If you get it as a String, then half the work is already done, and you just need
i.contains("7")
As noted above, this has nothing to do with JavaScript - both your example and my answer are in Java.
Couple of things:
Your comparison is not right, method String:contains() returns a boolean,
Module % does not assert you the number will contain 7 or one of it's multiples.
Integer.toString(value) converts easily your int to String.
Knowing this, you can do:
if (Integer.toString(round).contains("7")) {
// IT CONTAINS THE NUMBER!
} else {
// IT DOES NOT CONTAIN THE NUMBER
}
Here is perfect solution of your problem
public class Finder {
static int round = 123456789;
static String str = String.valueOf(round);
public static void main(String... args) {
if (str.contains("7")) {
System.out.println("Found");
} else {
System.out.println("Can't found...");
}
}
}
Just convert your integer to String and then try to found the specific value from that string.
You don't have to convert to string in order to search specific digit in integer.
You can use math for that purpose.
Here is the code:
private static boolean isFound(int round) {
while (round > 0) {
if (round % 10 == 7)
return true;
round /= 10;
}
return false;
}
basically what this code do is checking each last digit if it's equals to 7 if not he divides the num by 10 and remove the last digit and after checking again, it will do so until no digit left (num=0) or he will find 7.
I'm pretty new to java, but I'm trying to make a simulation of the finger game, 'Sticks', using my limited knowledge. This may not be the neatest, but if you're going to make a suggestion on me to do something, link a page explaining what that thing is, and I'll read it.
Ok, so the issue comes up basically when I call a method to decide who's turn it is and trying to return the value for the "count" up to 5, but it's not returning to main()
public static int TurnCalcBB(int PLH, int PRH, int BRH, int BLH, int Death)
{
//Attacking with bot Right hand
Random botAtk = new Random();
if(botAtk.nextInt(2) == 1 && PRH <= 5)
{
PRH = BRH + PRH;
JOptionPane.showMessageDialog(null,"Your right hand is now at " + PRH);
return PRH;
} else if(botAtk.nextInt(2) == 0 && PLH <= 5){
PLH = BRH + PLH;
JOptionPane.showMessageDialog(null, "Your left hand is now at " + PLH);
return PLH;
}
return Death;
}
Death is there because I was getting an error telling me that I always need to return SOMETHING so I'm returning a static value.
Basically, the problem is getting PLH (player left hand) or PRH (player right hand) to return to main. If I'm not wrong, they should return as their initial variable name (PL, and PR) with the returned value correct? If not, what can I do to fix this?
The code is a lot larger than this, and this issue is happening throughout the whole program, so I'm showing just 1 method and assuming they're all the same issue; the methods are almost all the same.
Also, while I'm typing a question already, is nextInt() the best way to do a random number generator? When I had it as nextInt(1) it was exclusively attacking the left hand, and when I switched it to nextInt(2) now it's attacking both, but occasionally the code... "crashes" (what I mean by crashes is that it generates a number outside of what the If statements are looking for). I obviously need to to generate either a 1 or a 2 (or 0 and 1 if 0 counts).
You can change your code to
public static Integer TurnCalcBB(int PLH, int PRH, int BRH, int BLH, int Death)
{
//Attacking with bot Right hand
Random botAtk = new Random();
if(botAtk.nextInt(2) == 1 && PRH <= 5)
{
PRH = BRH + PRH;
JOptionPane.showMessageDialog(null,"Your right hand is now at " + PRH);
return PRH;
} else if(botAtk.nextInt(2) == 0 && PLH <= 5){
PLH = BRH + PLH;
JOptionPane.showMessageDialog(null, "Your left hand is now at " + PLH);
return PLH;
}
return null;
}
NOTE: make sure you first check for null values where you call this function.
You are generating random number twice, this is why you can observe "strange" behvior.
Random botAtk = new Random();
if(botAtk.nextInt(2) == 1 && PRH <= 5) {
...
}
else if(botAtk.nextInt(2) == 0 && PLH <= 5) {
...
}
Try generating random only once:
Random botAtk = new Random();
boolean right = botAtk.nextInt(2) == 1; // flip coin only once
if(right && PRH <= 5) {
...
}
else if(!right && PLH <= 5) {
...
}
I know the answer will not get accepted, because there is an accepted one, but nevertheless:
I suspect that you have a wrong understanding of method parameter passing in Java.
What I read from your question and comments is that you expect this to work:
public static int psInt = 0;
static void main() {
int someNumber = 1;
int someOtherNumber = 5;
method1( someNumber, someOtherNumber );
// You expect "someNumber" to be 6 right now.
// But in fact, the value will be unchanged.
// What WILL work: psInt is 0 now
method3(); // this method will modify the static class var
// psInt is 5 now.
}
static void method1( int numParam, int someothervalue ){
numParam = numParam + someothervalue;
}
static void method2( int someNumber, int someothervalue ){
someNumber = someNumber + someothervalue; // <- same name won't work either!
}
public static void method3(){
psInt = 5;
}
But in Java method arguments are passed by value. That is: a copy!
So no matter how you name the variables and arguments, you will never have an "out" argument here.
What you can do:
In a static method, you can use and modify static class variables.
In a non-static method, you can use and modify non-static and static class variables.
You can pass a State-Object, of which you can modify field values.
You can return a value.
... there are more possibilites. These just to start with.
In your case, 4. does not make so much sense, because you wouldn't know if it is the new right or left hand value.
This is a first for using a binary search for me, so I've run into a small issue (hopefully!) first the program, it allows the user to type in a random number, and if that number matches any book it outputs the title.
class b {
String book1, book2;
b () {
book1 = "Wicked Awesome Title";
book2 = "How to Read a Book";
public static Book getBook(Book [] A, int left, int right, String bookTitle) {
int middle;
Book found = null;
/**Your average Joe binary search...*/
while (found == null && left <= right) {
//If middle item == 0, returns true
middle = (left + right)/2;
int compare = A[middle].sameTitle(bookTitle);
if (compare == 0) {
found = A[middle];
} else {
if (compare >0) {
right = middle -1;
} else {
left = middle + 1;
}
}
}
return found;
}
Now this is the problem, after pressing the "find" book button,
private void findActionPerformed(java.awt.event.ActionEvent evt) {
String book1 = "Wicked Awesome Title";
String book2 = "How to Read a Book";;
Book b = getBook(book1, book2); //this entire line is underlined,
if (b != null){
itsATextField.setText("You've found the book " + b);
}
so what am I missing to make this work? Any ideas?
Your getBook function is declared as:
public static Book getBook(Book [] A, int left, int right, String bookTitle) {
When you try to call it only with two String arguments:
Book b = getBook(book1, book2);
If you want to call a function, you must call it with the expected arguments.
Also, not sure if related or not but you'r missing } at the end of the constructor.
BTW, adding the error you get will help us help you.