I'm trying to get my program to repeat, but it's not working out too well. This is what I have so far:
import java.util.*;//to use scanner class
public class ArrayDemo
{
public static void main(String args[])
{
int a[]=new int[5];
Scanner sc=new Scanner(System.in);//use to take input
int i;
for(i=0;i<a.length;i++)//arrayname.length gives the length of array
{
System.out.println("Enter number:");
a[i]=sc.nextInt();
}
int sum=0;
long pro=1;
int max=a[0];
int min=a[0];
for(i=0;i<a.length;i++)
{
sum=sum+a[i];
pro=pro*a[i];
if(a[i]>max)
max=a[i];
if(a[i]<min)
min=a[i];
}
float avg=sum/a.length;
System.out.print("You entered");
for(i=0;i<a.length;i++)
{
System.out.print(", 1"+a[i]);
}
System.out.println("\nThe sum of those numbers is "+sum);
System.out.println("The product of those numbers is "+pro);
System.out.println("The largest number entered is "+max);
System.out.println("The smallest number entered is "+min);
System.out.println("The average of the numbers entered is "+avg);
}
}
I have another program that I'm trying to work into repeating, but it says no main applets or methods to be found, so it doesn't return anything:
import java.util.*;//to use scanner class
public class ArrayDemo
{
public static void myMethod(Scanner scanner)
{
int a[]=new int[5];
int i;
for(i=0;i<a.length;i++)//arrayname.length gives the length of array
{
System.out.println("Enter number:");
a[i]=scanner.nextInt();
}
int sum=0;
long pro=1;
int max=a[0];
int min=a[0];
for(i=0;i<a.length;i++)
{
sum=sum+a[i];
pro=pro*a[i];
if(a[i]>max)
max=a[i];
if(a[i]<min)
min=a[i];
}
float avg=sum/a.length;
System.out.print("You entered");
for(i=0;i<a.length;i++)
{
System.out.print(","+a[i]);
}
System.out.println("\nThe sum of those numbers is "+sum);
System.out.println("The product of those numbers is "+pro);
System.out.println("The largest number entered is "+max);
System.out.println("The smallest number entered is "+min);
System.out.println("The average of the numbers entered is "+avg);
System.out.println();
do {
myMethod (scanner);
System.out.println("You want to continue : (Y/N) ");
} while("Y".equalsIgnoreCase(scanner.next().trim()));
scanner.close ();
}
}
You need a main method like
public static void main(String args[])
Scanner scanner = new Scanner(System.in);
do {
myMethod (scanner);
System.out.println("You want to continue : (Y/N) ");
} while("Y".equalsIgnoreCase(scanner.next().trim()));
scanner.close ();
}
so move this code to a main method from your (my) `myMethod
Your first code has main method:
public static void main(String[] args) {// some code }
but second code has not this method. In the java language a program must start from main method with above signature.
In your 2nd approach you don't have the main method. So you can't run the file.
Another thing is don't put all the logic in main method. Try your best to separate them as much as possible. Then it is more clear.
Put your calculation logic to a method and call it from a while or do while loop
e.g.
class A{
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
int[] a= new int[5];
boolean bRepeat = true;
while(bRepeat){
for(i=0;i<a.length;i++){
System.out.println("Enter number:");
a[i]=sc.nextInt();
}
System.out.println("Sum is " + getSum(a));
System.out.println("Average is " + getAvergae(a));
.....
System.out.println("Do you wanna repeat? T/F");
String response = scanner.nextLine();
if(response.equalsIgnoreCase("T"))
bRepeat = true;
else
bRepeat = false; // or break
}
}
public static int getSum(int[] number){
//logic
}
public static double getAverage(int[] number){
//logic
}
public static int getProduct(int[] number){
//logic
}
public static int getMax(int[] number){
//logic
}
................
}
Related
import java.util.Scanner; //Scanner is imported here
public class Reverse {
public static void main(String[] args) {
int rev=0, rem;
Scanner s = new Scanner(System.in); //Scanner is used here and error is shown in this line
System.out.println("Enter a number ");
int a=s.nextInt();
int b=a;
while(b!=0)
{
rem=b%10;
rev=rev*10+rem;
b=b/10;
}
System.out.println("The reverse of the number is " +rev);
if(a==rev)
{
System.out.println("The number is Palindrome.");
}
else
{
System.out.println("The number is not Palindrome");
}
}
}
use s.close(); at the end within main function.
Your program will look like this:-
public static void main(String[] args) {
int rev=0, rem;
Scanner s = new Scanner(System.in); //Scanner is used here and error is shown in this line
System.out.println("Enter a number ");
int a=s.nextInt();
int b=a;
while(b!=0)
{
rem=b%10;
rev=rev*10+rem;
b=b/10;
}
System.out.println("The reverse of the number is " +rev);
if(a==rev)
{
System.out.println("The number is Palindrome.");
}
else
{
System.out.println("The number is not Palindrome");
}
s.close(); //SCANNER ENDS HERE.
import java.util.Scanner;
public class MethHead
{
public static int calMe(int a, int b)
{
int sum=a+b;
return sum;
}
public static void main(String[] args)
{
do
{
Scanner input=new Scanner(System.in);
System.out.println("Enter two numbers to add");
System.out.println("a: ");
int a=input.nextInt();
System.out.println("b: ");
int b=input.nextInt();
int sum=calMe(a, b);
}
while(sum!=2);
}
}
Ok, stupid question, but alwell. I keep getting
cannot find symbol- variable sum
I'm trying to figure out how to position a loop to continue the program if a certain value is returned from the method.
Thank you in advance!
declare variable sum outside the loop
int sum;
do
{
Scanner input=new Scanner(System.in);
System.out.println("Enter two numbers to add");
System.out.println("a: ");
int a=input.nextInt();
System.out.println("b: ");
int b=input.nextInt();
sum= calMe(a, b);
}
while(sum!=2);
import java.util.Scanner;
public class MethHead
{
public static int calMe(int a, int b)
{
int sum=a+b;
return sum;
}
public static void main(String[] args)
{
int sum;
do
{
Scanner input=new Scanner(System.in);
System.out.println("Enter two numbers to add");
System.out.println("a: ");
int a=input.nextInt();
System.out.println("b: ");
int b=input.nextInt();
sum=calMe(a, b);
}
while(sum!=2);
}
}
How I fixed it... just in case anyone ever comes across the same problem.
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);
}
}
I have a problem statement
Problem
Write a program to calculate the sum of 2 numbers and print the output.
Input
Line 1: An integer.
Line 2: An integer.
Output :The output consists of a single integer which corresponds to sum, followed by a new line
Sample Input I
3
1
Sample Output I
4
Sample Input II
13
10
Sample Output II
23
To which my solution is
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Add {
public static void main(String[] args)throws IOException
{
int a=0, b=0, sum;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the numbers to be summed");
try{
a=sc.nextInt();
sc.nextLine();
b=sc.nextInt();
}
catch(InputMismatchException e){
System.out.println("Please enter an Integer number");
e.printStackTrace();}
catch(Exception e){System.out.println(e);}
sum=a+b;
System.out.println(sum);
sc.close();
}
}
I'm supposed to submit it to an online directory, that I assume tries to execute the program automatically. And when I do, it tells me
Wrong Answer Almost there,think some more
I think pondering over it for an hour is more than enough before you decide to call in for reinforcement.
The output should be "a single integer which corresponds to sum, followed by a new line".
But the output of your program is
Enter the numbers to be summed
<the sum>
remove sc.nextLine(). It makes it move to the next line, but since both integers are on the same line, the value for b remains at 0.
These can be solve by two thing command line arguments or Scanner class or BufferReader.
Using the Command line Arguments.
public Class Sum
{
public static void main(String [] args)
{
int a ,b,c;
a=Integer.parseInt(args[0]); //using Integer wrapper Class to cast object
to primitive Datatype Integer.
b= Integer.parseInt(args[1]) ;
c= a+b;
System.out.println("The Sum of two number is : "+c);
}
}
Using Command Line Arguments with code re usability(Method Sum)
public Class Sum
{
public static long sum(int a,int b)
{
return a+b;
}
public static void main(String [] args)
{
int a ,b;
long c; // for long summation of numbers .
a=Integer.parseInt(args[0]); //using Integer wrapper Class to cast object
to primitive Datatype Integer.
b= Integer.parseInt(args[1]) ;
c= sum(a,b);
System.out.println("The Sum of two number is : "+c);
}
}
Using the External resources from the java.util.Scanner
public Class Sum
{
public static void main(String [] args)
{
int a ,b;
long c;
Scanner scan;
scan = new Scanner(System.in) ; //Taking system Keyboard for input.
System.out.println("Enter the value of A: \n");
a= ss.nextInt() ;
System.out.println("Enter the value of B: \n");
b=ss.nextInt();
c= (long) (a+b);
System.out.println("The Sum of two number is : "+c);
}
}
Try this:
import java.util.Scanner;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num1 = 0;
int num2 = 0;
int sum = 0;
System.out.println("Enter Number: ");
num1 = in.nextInt();
System.out.println("Enter Number2: ");
num2 = in.nextInt();
sum = num1 + num2;
System.out.println(sum);
}
}
package stack;
public class Satck {
public static int MAX=100;
int top;
int [] a= new int [MAX];
boolean empty()
{
return (top<0);
}
Satck()
{
top=-1;
}
void push(int x)
{
a[++top]=x;
}
public int pop()
{
int x=a[top--];
return x;
}
public int peek()
{
int x=a[top];
return x;
}
public static void main(String[] args) {
Satck s=new Satck();
s.push(10);
s.push(11);
s.push(12);
s.push(13);
System.out.println(s.peek());
System.out.println(s.empty());
System.out.println(s.pop());
System.out.println(s.peek());
}
}