local variable initialization were wrong java [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am new to java, why a, b,c initialization are wrong in the following code.
public static void main(String[] args) {
if (args.length < 2)
throw new IllegalArgumentException ("we need 2 argumeents");
else {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = a+b;
}
System.out.println(a + " + " + b + " = " + c);
}

Java works differently compared to JavaScript. Every {} block has an own variable scope. Variables defined inside a block are not visible outside.
public static void main(String[] args) {
{
int x=1;
System.out.println(x); // prints 1
}
{
int x=2;
System.out.println(x); // prints 2
}
// System.out.println(x); // error: cannot find symbol
}

Related

Is it possible to use a String if statement to change an integer? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm just having a practise at some java coding and was wondering if there was a way to use a String if else statement to change change an integer, as the following doesn't like the c = a * b
int a, b, c;
Scanner sc = new Scanner(System.in);
System.out.println("Enter first number");
a = sc.nextInt();
System.out.println("Enter * + - or / ");
String d = sc.nextLine();
System.out.println("Enter second number");
b = sc.nextInt();
if (d.equals("*")){
c = a * b;
}
System.out.println(a + " " + d + " b " + " = " + c);
This happens because the variable "c" is initialized only if the conditional if (d.equals("*")) {...} is true, to solve this you have two options:
Just initialize "c" :
int a, b, c = 0;
or add else statement
if (d.equals("*")){
c = a * b;
}else {
c = 0;
}

What do I need to fix to get the code to compile? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 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
Write a Java program with a recursive method that accepts a value i as input and computes the sum:
m(i) = 1/3 + 2/5 + 3/7 + 4/9 + 5/11 + ... + i/(2i + 1)
So far I have:
import java.util.Scanner;
public class Recursion {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter an i: ");
int i = sc.nextInt();
if (i == 0) {
System.out.println(0);
}
else {
System.out.println(i / (2 * i + 1) + (main(i-1)));
}
}
}
The code isn't compiling. Please help!!
Ok I think I finally understand what you're trying to do! The problem is you're actually trying to build a string! The method is doing what it should, your return just isn't really doing what it should.
import java.util.Scanner;
public class Recursion
{
private static String result = null;
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter an i: ");
int i = sc.nextInt();
System.out.println(recursion(i));
}
public static String recursion(int i)
{
if(i != 0)
{
if(result == null)
{
result = (i + "/" + (2 * i + 1));
recursion(i - 1);
}
else
{
result = result.concat(" + ").concat(i + "/" + (2 * i + 1));
recursion(i - 1);
}
}
return result;
}
}
Please let me know if this does what you want it to!
You shouldn't use recursion on the main function. Just create a second function that isn't the main and it should work:
import java.util.Scanner;
public class Recursion {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter an i: ");
int i = sc.nextInt();
System.out.println(recursion(i));
}
public int recursion(int i){
if (i == 0) {
return 0;
}
else {
return (i / (2 * i + 1) + (recursion(i-1)));
}
}
}

C to Java (binary numbers programm) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I made this program on C,and I have no idea of Java (new-yοung developer).Can you help me to "translate" it on Java?
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
void printbin(int num)
{
int binnum;
binnum=num%2;
num=num/2;
if (num!=0) printbin(num);
printf("%d",binnum);
return;
system("pause");
}
int main(void)
{
int posnumber,binnum;
printf("Give a number : ");
scanf("%d",&posnumber);
printbin(posnumber);
printf("\n");
return 0;
}
You can learn from this code, hope this can help you:
//here is comments in java
package help;
import java.util.Scanner;
//this is the way how you can all the libraries in java so
/*
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
*/
//here name of your class
public class Help {
//here name of your methode
public static void printbin(int num) {
int binnum;
binnum = num % 2;
num = num / 2;
if (num != 0) {
printbin(num);
}
System.out.println(binnum);
}
//here is the main methode in java
public static void main(String args[]) {
int posnumber, binnum;
Scanner scan = new Scanner(System.in);
System.out.println("Give a number : ");
posnumber = scan.nextInt();
System.out.println(posnumber);
}
}
Good luck, and good start with java :)

How to store multiple variables in this code? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm writing a program to find out if a number is even or odd and I have all of the code working, but I don't know how to write in other examples. Here is the code I have so far:
import static java.lang.System.*;
public class numberverify
{
public static boolean isOdd( int num)
{
return ((num % 2) == 1);
}
public static boolean isEven( int num)
{
return ((num % 2) == 0);
}
}
and the runner:
import static java.lang.System.*;
import java.util.Scanner;
public class numberverifyrunner
{
public static void main ( String[] args )
{
Scanner keyboard = new Scanner(in);
System.out.print("Enter an Integer :: ");
int num = keyboard.nextInt();
System.out.println( num + " is odd :: " + numberverify.isOdd(num));
System.out.println( num + " is even :: " + numberverify.isEven(num) + "\n");
//add in more test cases
}
}
Do this:
while(keyboard.hasNextInt())
{
int num = keyboard.nextInt();
System.out.println( num + " is odd :: " + numberverify.isOdd(num));
System.out.println( num + " is even :: " + numberverify.isEven(num) + "\n");
}
The program should quit if you put anything other than an integer in.
You could simplify this code somewhat (and make it easier to maintain) by only having a single method that performs the check. The second method can simply call the first one, for example:
public static boolean isOdd(int num) {
return ((num % 2) == 1);
}
public static boolean isEven(int num) {
return !isOdd(num);
}
For that matter, using a bitwise AND operation here is more efficient:
public status boolean isEven(int num) {
return (num & 1) == 0;
}

String Var with int var [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to have a string variable showing next to my int variable but it doesn't seem to work.
public class ThreeLittleNumbers {
public static void main(String[] argts) {
String as = "Number 1: ";
String bs = "Number 2: ";
String cs = "Number 3: ";
int a = 1;
int b = 3;
int c = 5;
String tot = "Total: ";
System.out.println(as+a);
System.out.println(bs+b);
System.out.println(cs+c);
System.out.println(tot);
System.out.print(a+b+c);
}
}
If I understand your question, then this
System.out.println(tot);
System.out.print(a + b + c);
should be
System.out.print(tot);
System.out.println(a + b + c);
When I make the change above, I get output like you seem to be asking for -
Number 1: 1
Number 2: 3
Number 3: 5
Total: 9
Another option would be to use printf and something like
System.out.printf("%s %d%n", tot, a + b + c);

Categories

Resources