I am new to java programming, I have programmed in C and C++ but have moved to Java recently, So I am a bit confuse about how things are in Java. I am calling a function inside my main but the program gets terminated, I don't know why its happening and can't figure it out. Here is my program
package Prime;
import java.util.Scanner;
public class isprime
{
public static boolean isPrime (int n)
{
int flag=0;
for (int i=2;i<=n;i++)
{
if(i%n==0)
{
flag=1;
}
}
if(flag==1)
{
return false;
}
else
{
return true;
}
}
public static void main(String[] args)
{
int n;
System.out.println("Please enter a number you want to test");
Scanner sc = new Scanner(System.in);
sc.close();
isPrime(n);
}
n = sc.nextInt();
}
Your n = sc.nextInt(); is outside the scope of main() function. Moreover your are closing the scanner first.
You called isPrime(n); which returns boolean but you did not catch the return value.
You want to do something like:
public static void main(String[] args) {
int n;
System.out.println("Please enter a number you want to test");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
if (isPrime(n)) {
System.out.println("prime");
} else {
System.out.println("not prime");
}
sc.close();
}
Finally, your prime calculation is wrong. A prime number is divisible by itself, so change
for (int i=2;i<=n;i++)
to
for (int i=2;i < n;i++)
^^^
Related
I'm trying to test if an input is odd or even. The program says that if the input is even keep going, until the input is odd. When it is odd, it stops the program. But the program isn't stopping. Has anyone any idea?
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
while (isEven()) {
if (isEven()) {
isEven();
} else {
System.out.println(" You added an odd number, done!");
return;
}
}
}
public static boolean isEven(){
int a = scanner.nextInt();
if (a%2 ==0){
System.out.println("You added an even number, go on");
}
return true;
}
}
You'd better simplify all of this, you don't need to call your method multiple times :
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (writeAndCheckEven(Integer.parseInt(scanner.nextLine()))) {
System.out.println("You added an even number, go on");
}
System.out.println("You added an odd number, done!");
}
private static boolean writeAndCheckEven(int number) {
return number % 2 == 0;
}
you don't need to use a return if there is no more code after
you can directly use the scanner in the parameter
don't use both while and if it won't do what you want
You should rewrite this code, because code contains incorrect logic.
Correct code for your requirements:
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
int number = scanner.nextInt();
if (isEven(number)) {
System.out.println("You added an even number, go on");
} else {
System.out.println(" You added an odd number, done!");
return;
}
}
}
private static boolean isEven(int number) {
return number % 2 == 0;
}
I need help making it so that the entire script will loop from the beginning if the user types y at the end and the script ends if the user types n at the end. Thanks for any help.
package test123;
import java.util.Scanner;
public class test321 {
public static void main(String[] args) {
int n = 1;
int c;
String playAgain;
do {
System.out.println("Enter a number");
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
if ( n < 0 )
System.out.println("Number cant be a negative.");
else
{
int x=n*(n-1)*(n-2);
System.out.println("Factorial= "+x);
System.out.println("Do you want to continue? (y/n):");
playAgain = scanner.nextLine();
}while (!playAgain.equals("y"));
}
}
}
package test123;
import java.util.Scanner;
public class test321 {
public static void main(String[] args) {
int n = 1;
int c;
String playAgain;
do {
System.out.println("Enter a number");
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
if ( n < 0 )
System.out.println("Number cant be a negative.");
else
{
int x=n*(n-1)*(n-2);
System.out.println("Factorial= "+x);
System.out.println("Do you want to continue? (y/n):");
playAgain = scanner.nextLine();
} // end else
} // end do
while (playAgain.equals("y"));
}// end main
} // end class
Change
while (!playAgain.equals("y"));
to
while (!playAgain.equals("n"));
I want to display the statement inside the else whenever the user type a negative number, I know this while loop will be infinite loop but I don't know how to break it. I try to type in "break", but it shows error:
need a return statement.
Should I use only if-statement or am I writing the right code?
import java.util.Scanner;
class number{
public static void main(String[] args){
workers();
int numEmployee;
}
public static int workers(){
System.out.println("How many employees do you have?");
Scanner input = new Scanner(System.in);
int number = input.nextInt();
int numEmployee;
while(true){
if(number >= 0){
numEmployee = number;
return numEmployee;
}else{
System.out.println("Please enter a positive number");
}
}
}
}
Suggested changes:
import java.util.Scanner;
// Always capitalize your public class name (and the corresponding source file)
public class Number{
public static void main(String[] args){
// Get #/employees
int numEmployee = Number.workers();
System.out.println ("#/employees=" + numEmployee);
}
public static int workers(){
// Loop until we get a valid number
int number;
do {
System.out.println("How many employees do you have?");
Scanner input = new Scanner(System.in);
number = input.nextInt();
} while (number <= 0)
// Return the final value
return number;
}
}
First thing you want is to ask for input every loop. Also, you should have a return in your method for every condition. A compiler doesn't know your loop is infinite so it expects a return:
import java.util.Scanner;
class Number{
public static void main(String[] args){
int numEmployee = workers();
System.out.println("Number of empoyees: " + numEmployee);
}
public static int workers(){
int number = 0;
System.out.println("How many employees do you have?");
Scanner input = new Scanner(System.in);
while(true){
number = input.nextInt();
if(number >= 0){
return number;
}else{
System.out.println("Please enter a positive number");
}
}
return 0; //Added this
}
}
Your method should be:
public static int workers() {
System.out.println("How many employees do you have?");
Scanner input = new Scanner(System.in);
int number;
boolean isValid = false;
int numEmployee = 0;
while (!isValid) {
number = input.nextInt();
if (number >= 0) {
numEmployee = number;
isValid = true;
} else {
System.out.println("Please enter a positive number");
}
}
return numEmployee;
}
in this code i want to take no. of testcases by keyboard input and rest is the same problem. only what i am doing is like. if i take 2 test cases then it shud be print the result based on both cases after taking the complete input. For example: INPUT testcases : 2 //case1// 5(no of building) 7 5 2 11 1 //case2// 3(no. of building) 1 2 3 OUTPUT 7//OUTPUT FOR 1ST CASE// 0//OUTPUT FOR 2ND CASE// HOPE, NOW PROBLEM IS CLEAR
import java.util.Scanner;
public class Komal {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("enter the test cases");
int T=sc.nextInt();
for(int i=1;i<=T;i++)
{
System.out.println("total no of building");
int n=sc.nextInt();
int ar[]=new int[n];
for(int j=0;j<n;j++)
{
System.out.println("enter the heights");
ar[j]=sc.nextInt();
}
for(int j=1;j<ar.length;j++)
{int sum=0;
if(ar[0]<ar[i])
{
break;
}else
{
sum += (ar[0]-ar[i]);
}
System.out.println(sum);
}
}
}
}
Here is the complete code. Luckily i had my laptop running and this isn't a very difficult program.
public class TillGreater{
public static void main(String args[]){
int[] ar = {5,4,2,7,1};
int sum=0;
for(int i = 1 ; i < ar.length;i++){
if(ar[0]<ar[i]){
break;
}else{
sum = sum + (ar[0]-ar[i]);
}
}
System.out.println(sum);
}
}
try this:
public static int SumUntilBigger(int[] a)
{
int sum=0;
for(int i=1;i<a.length;i++)
{
if(a[i]<=a[0])
sum+=a[0]-a[i];
else
return sum;
}
return sum;//will reach this statement if all the elements are bigger than the first one
}
this compares each element with the first element and when the element is bigger it just return the sum and exits from method, if all elements are bigger than the first one just return the sum of all differences.
you can use it in main like this:
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("enter the size of the array");
int size=s.nextInt();
int[] a=new int[size];
System.out.println("enter the array values");
for(int i=0;i<size;i++)
a[i]=s.nextInt();
System.out.println("The differences sum : "+SumUntilBigger(a));
}
import java.util.Scanner;
public class linecounter {
public static void main(String[] args) {
System.out.print("Enter a line of integers ");
Scanner chopper = new Scanner(System.in);
int x = chopper.nextInt();
while (chopper.hasNextInt()) {
System.out.println(chopper.nextInt());
}
}
}
I am in a CS1 class learning the basics of Java and have a quick question, on this code could anyone tell me how i could get it to keep count of how many integers were typed in?
Thank you
above your while loop, declare:
int count = 0;
then in your while loop use
count++;
This will start you at 0 and every time it increments the count
You could add a counter to the while loop.
int counter = 0;
while (chopper.hasNextInt()) {
counter++;
System.out.println(chopper.nextInt());
}
System.out.println(counter);
In the cases that you have integer numbers, double numbers and you only need count the integer numbers, you can use:
public class linecounter {
public static void main(String[] args) {
System.out.print("Enter a line of integers ");
Scanner chopper = new Scanner(System.in);
int x = chopper.nextInt();
int counter = 0;
while (chopper.hasNextInt()) {
System.out.println(chopper.nextInt());
String myCurrentArg = chopper.nextInt();
if(isInteger(myCurrentArg) ){
counter++;
}
}
System.out.println("The number of integer arguments are: " + counter);
}
public static boolean isInteger(String s) {
return isInteger(s,10);
}
}