import java.util.Scanner;
public class program4
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.println(" Hello and welcome to the program ");
System.out.println("Enter your number.");
int number = input.nextInt();
int integerDoubled;
System.out.println("Your result is" + doubleNumber(number));
System.out.println(" thank you for using my program ");
}
public static int doubleNumber (int x){
int integerDoubled;
return integerDoubled = (x*2);
}
if (integerDoubled < 100 ); {
less_100(integerDoubled);
} else if ;
greater_100(integerDoubled);
public static int less_100 (int integerDoubled)
{
int integerDoubled;
return integerDoubled =(x*2);
}
public static int greater_100 (int integerDoubled)
{
int integerDoubled;
return integerDoubled =(x*3);
}
}
}
I think the error is in the If statement but i don't know how to fix it.
I know integerDoubled is a local variable and i should make it global but I'm not sure how to do that. should i put the if statement together with the doubleNumber function??
If you formatted your code (your IDE will do this for you), you would see that the if statement is not inside a method as it must be. If you use your IDE correctly, finding and correcting such error would be much faster and you barely have to think about it.
I tried to make sense of your program this is what I came up with
import java.util.Scanner;
public class T
{
public static int doubleNumber (int x){
int integerDoubled;
return integerDoubled = (x*2);
}
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.println(" Hello and welcome to the program ");
System.out.println("Enter your number.");
int number = input.nextInt();
int integerDoubled=doubleNumber(number);
System.out.println("Your result is" + integerDoubled);
System.out.println(" thank you for using my program ");
if (integerDoubled < 100 ) {
less_100(integerDoubled);
}
else
greater_100(integerDoubled);
}
public static int less_100 (int integerDoubled)
{
return integerDoubled =(integerDoubled*2);
}
public static int greater_100 (int integerDoubled)
{
return integerDoubled =(integerDoubled*3);
}
}
Related
I need to know how to add the input as the number to be reversed. This questions answer should help anyone who has the need to make an input go into a program and come out modified.
import java.util.Scanner;
public class NumberReverse {
public int reverseNumber(int number){
System.out.print("Enter a number: "); <------ input
Scanner keyboard = new Scanner(System.in);
String input = keyboard.nextLine();
int reverse = 0;
while(number !=0){
reverse = (reverse*10)+(number%10);
number = number/10;
}
return reverse;
}
public static void main(String a[]){
NumberReverse nr = new NumberReverse();
System.out.println("Result: " +nr.reverseNumber(Where I want the input to go / or you can put a number here inside of the program, instead of using the interface.));
}
}
I think best way to handle input in the main method and then trigger to reverseNumber with input value;
public class NumberReverse {
public int reverseNumber(int number){
int reverse = 0;
while(number !=0){
reverse = (reverse*10)+(number%10);
number = number/10;
}
return reverse;
}
public static void main(String a[]){
System.out.print("Enter a number: ");
Scanner keyboard = new Scanner(System.in);
String input = keyboard.nextLine();
try {
NumberReverse nr = new NumberReverse();
System.out.println("Result: " +nr.reverseNumber(Integer.valueOf(input)));
} catch (NumberFormatException nme) {
System.err.println("You entered not numeric value...!");
}
}
}
You can pass the number as the argument of the main:
public static void main(String a[]) {
NumberReverse nr = new NumberReverse();
System.out.println("Result: "+ nr.reverseNumber(Integer.parseInt(a[0])));
}
If you don't want interface, then remove Scanner statements and pass number as argument like below
public int reverseNumber(int number){
int reverse = 0;
while(number !=0){
reverse = (reverse*10)+(number%10);
number = number/10;
}
return reverse;
}
public static void main(String a[]){
reverseNumber nr = new reverseNumber();
System.out.println("Result: " +nr.reverseNumber(52)); //pass the number you wish to reverse
}
Hi guys sorry I'm a newbie to Java, this is one of the exercise in my class.
I supposed to ask user input 5 numbers, then compare them if they are the same number that entered before.
These are my code so far, but I can't get it work.
Thanks.
import java.util.Scanner;
public class Source {
private static int num = 0;
private static int[] enterednum = new int[5];
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
for(int count = 0; count < enterednum.length; count++) {
System.out.println("Enter a number.");
num = input.nextInt();
compare(enterednum);
}
System.out.println("These are the number you have entered: ");
System.out.println(enterednum);
}
public static void compare(int[] enterednum) {
for(int count = 0; count < 6; count++)
if(num == enterednum[count])
System.out.println("The number has been entered before.");
}
}
You may want something like this:
import java.util.Scanner;
public class Source
{
private static int enterednum[]=new int[5];
public static void main(String args[])
{
int num=0; // make this local variable since this need not be class property
Scanner input = new Scanner(System.in);
for(int count=0;count<enterednum.length;count++)
{
System.out.println("Enter a number.");
num = input.nextInt();
compare(num, count);
enterednum[count] = num; // store the input
}
System.out.println("These are the number you have entered: ");
// print numbers in array instead the array
for(int count=0;count<enterednum.length;count++)
{
System.out.println(enterednum[count]);
}
}
// change the method signature to let it get the number of input
public static void compare(int num, int inputcount)
{
for(int count=0;count<inputcount;count++)
{
if(num==enterednum[count])
System.out.println("The number has been entered before.");
}
}
}
You can do this way if you need.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Source {
public static void main(String[] args) throws IOException {
// I used buffered reader because I am familiar with it :)
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
// Create a Set to store numbers
Set<Integer> numbers = new HashSet<>();
for (int i = 0; i < 5; i++) {
System.out.print("Enter a number: ");
String line = in.readLine();
int intValue = Integer.parseInt(line);
// You can check you number is in the set or not
if (numbers.contains(intValue)) {
System.out.println("You have entered " + intValue + " before");
} else {
numbers.add(intValue);
}
}
}
}
I'm trying to create a simple program to output the number of stars entered by user. I'm trying to learn how to use more than one method to do this
Here's my code
import java.util.Scanner;
public class Alpha
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
int n;
System.out.println("Enter no. of stars");
n = input.nextInt();
}
public static void Loop ()
{
for (int counter = 1; counter <= n; counter++)
{
System.out.println("*");
}
}
}
The problem I'm facing is that in the Loop method, I am unable to use the variable n
Is there a way to use a variable which is in the main method, in another one?
Ty
-Pingu
import java.util.Scanner;
public class Alpha
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
int n;
System.out.println("Enter no. of stars");
n = input.nextInt();
Loop(n); //calls Loop function and passes parameter n
}
public static void Loop(int n) //this function now expects a number n
{
for (int counter = 1; counter <= n; counter++)
{
System.out.println("*");
}
}
}
simply pass it as parameter:
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
int n;
System.out.println("Enter no. of stars");
n = input.nextInt();
Loop(n);
}
public static void Loop (int count)
{
for (int counter = 1; counter <= count; counter++)
{
System.out.println("*");
}
}
Pass it as a paramteer
import java.util.Scanner;
public class Alpha
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
int n;
System.out.println("Enter no. of stars");
n = input.nextInt();
loop(n); // added this
}
public static void loop (int n) // changed here
{
for (int counter = 1; counter <= n; counter++)
{
System.out.println("*");
}
}
}
I think you should use it as a instance variable and for better understanding name your class like StarClass it can provide better understanding. Good programming practice.
But you should avoid unneccesserily making instance variable without any logic behind it.
I also think you could declare n as a public variable.
That should make it accessible throughout the code.
public int n;
But I guess that passing it as parameter is a better practice, since you don't create a deppendance inside your code. What I mean is, if something changes with the variable you break the function. It's good practice to always keep things "modular" in your code, so it makes it more resilient to changes and debugging.
It's better if you get used to it from the beggining =)
Two ways.. one has been posted already as answer and the other one would be using the variable as a field. This way you can access (and modify) it in every method without having to pass it on.
public class Alpha
{
static int n;
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.println("Enter no. of stars");
n = input.nextInt();
loop();
}
public static void loop ()
{
for (int counter = 0; counter < n; counter++)
{
System.out.println("*");
}
}
}
And please start method names with lowercase and counting with 0. It's common practise and it helps a lot to use the standards right from the beginning.
Like this you can use variable from different methods in different methods
public static void main(String[] args) {
// TODO Auto-generated method stub
int sum=0;
int a=Sum(sum);
System.out.println("The Average of the numbers is: "+a);
}
public static int Sum(int sum) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the total count of number for Average: ");
int a=sc.nextInt();
for(int i=1;i<=a;i++) {
System.out.println("Enter the"+i+"Number: ");
int b=sc.nextInt();
sum+=b;
}
int avg=sum/a;
return avg;
}
So I'm learning Java in class and I'm really loving it so far but its really hard to understand sometimes. Right now I'm trying to understand how methods work. My question is why my code is not working. I am trying to read in an integer from user input then square it.
Here is my code:
package freetime;
import java.util.Scanner;
public class methods {
public static void main(String []args){
Scanner input = new Scanner(System.in);
System.out.println( " enter a number ");
int number = input.nextInt();
square(number);
}
public static int square(int number){
int num;
num = number * number;
return (num);
}
}
Let's say I input 5 on the console, the program immediately terminates and I cannot figure out why.
As mentioned by others, you don't print the value and the console will close as soon as the program ends. So you could try something like this
public class ScannerTest {
public static void main(String []args){
while(true){
Scanner input = new Scanner(System.in);
System.out.println( " enter a number (-1 to stop)");
int number = input.nextInt();
if(number == -1){
break;
}
int output = square(number);
System.out.println(output);
}
}
public static int square(int number){
int num;
num = number * number;
return (num);
}
}
This will print the result and loop ask for new input as long as you don't stop the program.
In Java, when main method comes to end and if there aren't any non-deamon threads running, the JVM ends. Your program came to an end without printing out the result of the square() call.
/*here is your solution :*/
import java.util.*;
import java.lang.*;
import java.io.*;
/*in java everything has to be in a class */
class SquareNumber
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner input = new Scanner(System.in);
System.out.println( " enter a number ");
int number = input.nextInt();
System.out.println(square(number));
/*something to print the squared number*/
}
public static int square(int number){
int num;
num = number * number;
return (num);
}
}
Your program is terminated because there is no other statement after square(number); statement. So your program executes square(...) method and after then it found end of main function so the program is terminated. To see some output you must print result of square(...) method.
package freetime;
import java.util.Scanner;
public class methods {
public static void main(String []args){
Scanner input = new Scanner(System.in);
System.out.println( " enter a number ");
int number = input.nextInt();
int result=square(number);//executing square(...) method and store the returned value of square method to result variable
System.out.println("Square of "+number+" is : "+ result);//printing result
}
public static int square(int number){
int num;
num = number * number;
return (num);
}
}
I wanted to program a java app that can print as many stars as the user want.
The programm will ask the user how many starts he want to print.
Here is my code :
import java.util.Scanner;
public class lab {
public static void main(String[] args){
int StarsN;
Scanner input;
input = new Scanner(System.in);
System.out.println("How many stars do you need?");
StarsN= input.nextInt();
}
public static void loopz(String[] args) {
int loopEnd = StarsN;
int loopStart;
for (loopStart = 0;loopStart==loopEnd;loopStart++) {
System.out.print("*");
}
}
}
First thing to note.. I don't know why you are sending your loopz method a String[].. Here is what i would do differently in the loopz method:
public static void loopz(int numOfStars)
{
for(int i = 0; i < numOfStars; i++)
System.out.print("*");
}
Also call loopz in main and send it the parameter.
your for loop : loopStart = 0 then it says is loopStart == loopEnd , and it won't enter in the loop because loopStart don't equals loopEnd so you should change "==" in your loop to "<" .
Here is the answer:
import java.util.Scanner;
public class lab {
public static void main(String[] args){
int StarsN;
Scanner input;
input=new Scanner(System.in);
System.out.println("How many starts do you need ?");
StarsN= input.nextInt();
int loopEnd = StarsN;
int loopStart;
for (loopStart = 0;loopStart<loopEnd;loopStart++) {
System.out.print("*") ;
}
}
}
I really would like to teach you how to fish, instead of just giving you the fish, but I think that you need too much theory before this. Try to find some book or a good and complete tutorial to follow, I'm sorry but I don't know neither of both to say to you.
Change for (loopStart = 0;loopStart==loopEnd;loopStart++) to for (loopStart = 0;loopStart < loopEnd;loopStart++).
And don't forget to call loopz() from main():
public static void main(String[] args){
Scanner input = null;
try {
input=new Scanner(System.in);
System.out.println("How many starts do you need ?");
int StarsN= input.nextInt();
loopz(StarsN); //Add this
} finally {
if( input != null )
input.close();
}
}
public static void loopz(int numStars) { //You don't need the String[] args here since you never use it
for (int loopStart = 0; loopStart < numStars;loopStart++) {
System.out.print("*") ;
}
}