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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package plaindromenumbers;
import java.util.Scanner;
/**
*
* #author taha
*/
public class PlaindromeNumbers {
/**
* #param args the command line arguments
*/
int func1(int n)
{
if(n==1)
return 1;
return n*func1(n-1);
}
static boolean check=false;
int func(int no)
{
String a=""+no;
String reverse = new StringBuffer(a).reverse().toString();
if(a.equals(reverse))
{
if(!a.contains("0"))
{
System.out.println("hey");
check=true;
return Integer.parseInt(a);
}
}
// else
// {
func(no++);
if(check==true)
{
return 0;
}
return 0;
}
public static void main(String[] args) {
// TODO code application logic here
Scanner in=new Scanner(System.in);
System.out.println("Enter testcase");
int testcase=in.nextInt();
while(testcase>0)
{
int a=in.nextInt();
PlaindromeNumbers obj=new PlaindromeNumbers();
System.out.println(obj.func(a));
testcase--;
}
}
}
Here is the problem:
func(no++);
Let's say no is 32. This will pass 32 to func and then increment it. Which means that you are once again calling the function with a value of 32. And when that function hits this line, it will, once again, pass that 32 to func. Hence, an infinite recursive loop.
You can fix this bug like this:
func(++no);
Now it will increase no before calling func. no will be 33, it will match its reverse, and all will be well.
You will need to return func(no + 1), this way your code will return the next palindrome number (which I assume is what you want your code to do?). check is not needed. Why include func1 when it's not even used by your code?
By the way, the stack overflow is caused by the infinite recursion of func.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
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.
Improve this question
I tried to write an primenumber generator. The method calcall() should return prime numbers (2,3,5,7...). Unfortunately I get the error, that the method doesn't returns an integer, wich I don't understand. Here is my code:
package primenumber;
public class primecalc {
public static int calcall(int a) { //actual generator
int konstante = a; //is this number a prime num?
int divisor = a-1; //divisor
int var1 = 0; //variable = 0
while(divisor>1) {
int quotient = konstante%divisor; //calc modulo
if(quotient == 0) { //if modulo==0 switch var1 to
var1++; //1 -> no primenumber
break; //stop calculating
} else { //else keep calculculating
divisor--; //until divisor <= 1
}
}
if(var1==0) { //if var1 still 0;
return konstante; //is a primnumber ->
} //return konstante
}
public static void main(String[] args) { //main function
int number = 3; //start with 3
while(True) { //(i'll add 2 manually)
System.out.println(calcall(number)); //print the prime number
number++; //increase number by one
}
}
}
The error is:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
This method must return a result of type int
at primenumber/primenumber.primecalc.calcall(primecalc.java:5)
at primenumber/primenumber.primecalc.main(primecalc.java:28)
What is wrong?
The gray lines on the code you posted are being ignored by the compiler.
The use of /* and */ makes everything between these seen by the compiler as comments. And that is why those lines are grayed out. If you want to comment on the same line as the code, I'd advise you to use //.
Also, it is common practise to use multi-line comments only to describe functions and place them just above the header of the function. Any other comments should be short, concise and describe functionality. Good variable names and well written code should do most of the explaining, and single line comments should be used when it's a bit harder toperceive what's going on.
Cheers
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 5 years ago.
Improve this question
package com.company;
public class Main {
public static int add(int x, int y) {
return x + y;
}
/**
* public static int multiply(int x, int y) {
* int m = 0;
* for (int i = 0; i < x; i++) {
* m = add(m, y);
* }
*/
public static void main(String [] args){
add(0,2);
}
}
I want to:
Know the result for the addition of x + y and display it in screen. I have passed values for x and y in void main. Kindly Tell me how to know result. Thank You.
You need a main method. It does not matter what your class is called; it could be MyMain, or CoolTest, or any valid identifier. What matters is that you have a method with the following signature:
public static void main(String[] args) {
//Your code
}
Whatever you put into this method will be the code run when you run this class.
Without a main method, the code would be ambiguous; the JVM would have no way of knowing which method to run. The main method is the most common way of providing Java with an entry point into your code, telling it where to begin (there are other ways, but they are more complicated and probably not relevant to you at this point as a beginner). After all, what would it even mean to run your application? Should add be called, or multiply? And what values should be added or multiplied? The main method should hold the answer.
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
Trying to call a method within a method.
public static void main(String[] args) {
int sq,cu=0;
//user input 1
sq=Integer.parseInt(JOptionPane.showInputDialog(
"Enter value to be squared"));
//user input 2
cu=Integer.parseInt(JOptionPane.showInputDialog(
"Enter value to be cubed"));
//results
JOptionPane.showMessageDialog(null, sqd(sq));
JOptionPane.showMessageDialog(null, cbd(cu));
}
public static String sqd(int sq){
int sqd=sq*sq;
//sq computation
return sq+" squared is "+sqd;
}
public static String cbd(int cu,int sqd){
int cbd;
cbd=sqd*cu;
//cu computation
return cu+" cubed is "+cbd;
} }
calling sqd value in cbd, but
JOptionPane.showMessageDialog(null, cbd(cu));
prevents me from doing so, it always gives me an error when I run it.
your cbd(int cu, int sqd) method needs 2 input parameters , you are calling it with only one parameter cbd(cu)
two choices :
1 - rewrite your cbd method with 1 parameter then you can call it using cbd(cu);
public static String cbd(int cu){
return cu + " cubed is " + (cu*cu*cu);
}
2 - write it's second parameter while you are using it :
cbd(cu,cu*cu);
Your cbd method, as it's currently defined, takes two arguments - cu and sqd. If you want to keep the way your main calls it, you need to rewrite it with just one argument:
public static String cbd(int cu){
int cbd = cu * cu * cu;
return cu + " cubed is " + cbd;
}
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 8 years ago.
Improve this question
I am trying to write a Tester and a class that can solve quadratic function.
If you are unfamiliar with quadratic function, or need a reminder, here is a quick link to it's Wikipedia article: http://en.wikipedia.org/wiki/Quadratic_function
My tester seems to work fine, however whenever I try to call the method, the program stops, and it doesn't display the output of the method (which is supposed to display the answer).
I am not skilled enough to find out if the error is within the class or the tester.
Tester:
/**
* A Tester to use to solve quadratic formula. Enter your values
* when prompted, and the answer will be displayed on screen.
*
* #author (Austin C.)
* #version (1.0.0)
*/
import java.io.*;
import java.util.*;
public class C_tester
{
public static void main (String args[])
{
Scanner kb = new Scanner(System.in);
System.out.println ("Enter the coefficents in the form of the following:\n1.A\n2.B\n3.C");
System.out.print("Enter the number for A:");
int a = kb.nextInt();
System.out.print("Enter the number for B:");
int b = kb.nextInt();
System.out.print("Enter the number for C:");
int c = kb.nextInt();
QuadraticFunction.quadratic(a,b,c);
}
}
Quadratic Function Class:
/**
* #Params: you must enter the coefficents, A, B, and C, and the program will calculate them to find the answer
* to a quadratic forumla. coefficents must be integers or doubles.
*
* #author (Austin C.)
* #version (1.0.0)
*/
public class QuadraticFunction
{
public void QuadraticFunction()
{
}
public static double quadratic(double a, double b, double c)
{
double topPos;
double topNeg;
double bot;
topPos = -b + Math.sqrt(Math.pow(b,2.0) - 4 * a * c);
topNeg = -b - Math.sqrt(Math.pow(b,2.0) - 4 * a * c);
bot = 2*a;
double ansPos = topPos/bot;
double ansNeg = topNeg/bot;
return ansPos + ansNeg;
}
}
Any help to find the error myself or find it for me is greatly appreciated. Also, if you find a more efficient way to do this, please share! I am always looking for more efficient ways to write code.
If the question is unclear, please say so and I can redo it in a more understandable way.
quadratic has return type double, and there are no print statements in the method. What this means is that it should return a value, but there is no reason that value should be printed. You can fix this by assigning the function result to a variable, and then adding a statement to print it to screen, like so:
double answer = QuadraticFunction.quadratic(a,b,c);
System.out.println(answer);
Also, you should note that a quadratic equation can have 2 real roots (possibly both the same), so you should be returning an array of doubles rather than a single double which is the sum of those 2 roots.
What do you expect it to print? You never tell the program to print anything. You can have it print the result to stdout by changing the last line in the tester to System.out.println(QuadraticFunction.quadratic(a,b,c)); if the program stops and there was no stack trace printed it means that it was executed succesfully
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
I'm making a class involving objects. I'm having trouble with the retrieveMessage method, because when I test it, it returns a NullPointerException.
public class PostOffice
{
public PostOffice(int size)
{
boxbox = new Letter[size];
}
public boolean placeLetter(Letter mail, int boxNum)
{
if(((boxNum>(boxbox.length-1))||(boxNum<0))||(boxbox[boxNum]!=null))
return false;
else{
boxbox[boxNum]=mail;
return true;
}
}
/**Returns the message contained within the Letter located in the specific box number.
* Returns "Empty!" if the post office box specified by the integer does not contain a Letter.
* Returns "Box does not exist!" if there is no box with the specified integer.
* #param boxNum The post office box number to be checked.
*/
public String retrieveMsg(int boxNum)
{
if(boxNum<=boxbox.length-1)
{
String swag = boxbox[boxNum].getMsg();
if(swag!=null && swag.isEmpty()==false)
{
return swag;
}
return "Empty!";
}
return "Box does not exist!";
}
public Letter findSender(String name)
{
String sender;
int index =0;
for(int i = 0; i<boxbox.length; i++)
{
if((boxbox[i].getSender()).equals(name)){
index= i;
}
else{
return null;
}
}
return boxbox[index];
}
}
I guess the NullPointerException is thrown on this line :
String swag = boxbox[boxNum].getMsg();
When created, boxbox have size values, all of them are null.
You should first check if boxbox[boxNum] is null or not. If it is, it means that the post box does not contain a letter.