How to get rid of duplicate local variable input - drjava

Everything else is working in both programs but every time I try to compile and run it gives me this error Duplicate local variable input and I can not seem to fix it. I've looked up solutions none seem to work.
**Scanner scan = new Scanner(System.in); **//Duplicate****
System.out.println("What is the capital of Canada?");
String input = scan.nextLine();
if (input.equalsIgnoreCase("Ottawa")) {
System.out.println("Correct");
} else {
System.out.println("incorrect");
}
Seperate Program
I have tested this code out on a test run and everything runs smoothly when it is not joined with the code posted ahead of this one and I need it for an assignment due tonight.
**Scanner input = new Scanner(System.in); //This is the error. Also Duplicate.**
int num1;
int num2;
int num3;
int sum;
System.out.println("Enter first integer:");
num1 = input.nextInt();
System.out.println("Enter second integer:");
num2 = input.nextInt();
System.out.println("Enter third integer:");
num3 = input.nextInt();
sum = num1 + num2 + num3;
if (num1 > num2 & num2 > num3) {
System.out.println("The number is");
System.out.println(sum);
}

When you join them together, instead of creating a new Scanner object:
Scanner input = new Scanner(System.in);
just do:
input = new Scanner(System.in);

**Scanner scan = new Scanner(System.in); **//Duplicate****
System.out.println("What is the capital of Canada?");
String input = scan.nextLine();
if (input.equalsIgnoreCase("Ottawa")) {
System.out.println("Correct");
} else {
System.out.println("incorrect");
}
**Scanner input2 = new Scanner(System.in); //This is the error. Also Duplicate.**
int num1;
int num2;
int num3;
int sum;
System.out.println("Enter first integer:");
num1 = input2.nextInt();
System.out.println("Enter second integer:");
num2 = input2.nextInt();
System.out.println("Enter third integer:");
num3 = input2.nextInt();
sum = num1 + num2 + num3;
if (num1 > num2 & num2 > num3) {
System.out.println("The number is");
System.out.println(sum);
}

Related

Why is my 'if' statement not executing in this code? (Processing)

void setup(){
size(800,600);
background(0);
fill(255,40,40);
noStroke();
ellipse(400,80,20,20);
ellipse(150,513,20,20);
ellipse(650,513,20,20);
fill(255);
frameRate(10);
}
float traceX = 350;
float traceY = 350;
String vertexPick = "";
float rand = 0;
void draw(){
point(traceX,traceY);
rand = random(1);
if (rand > 2/3) {
vertexPick = "A";
}
else if (rand < 1/3) {
vertexPick = "B";
} else {
vertexPick = "C";
}
if (vertexPick == "A") {
traceX = (traceX+400)/2;
traceY = (traceY+80)/2;
}
else if (vertexPick == "B") {
traceX = (traceX+150)/2;
traceY = (traceY+513)/2;
}
else
{
traceX = (traceX+650)/2;
traceY = (traceY+513)/2;
}
}
I've checked, and every time through the 'draw' function it is generating a new random number between 0 and 1. But instead of assigning vertexPick to a new letter based on the number, it gets stuck on one value of vertexPick and stays there forever. Can anyone tell me why? Why would it not execute the if statement again on every loop?
You have a problem with the division of integers, namely 2/3 is 0 and the condition rand > 2/3 is always true.
To have floating point division use 2.0/3 in place of 2/3.
(see this question Division of integers in Java)

Shorten the process of finding the sum of positive number (users input 6 different intteger)

I nedd to find the fatest and shortest way to calculate the sum of positive integer that the user input in.
else if(num1<0 && num2 >0 && num3>0 && num4>0 && num5>0 &&num6>0){
totalPositiveNumber =num2 + num3 + num4 + num5 + num6;
System.out.println("The sum of positive integer is: " + totalPositiveNumber);
}
You can read the numbers and store them in an array. Later iterate over the array and sum only, when number is greater than 0. Check below for sample code for reading input from System.in using Scanner class.
import java.util.Scanner;
public class Sum {
public static void main(String args[]) {
Scanner read = new Scanner(System.in);
int array[] = new int[6];
int sum = 0;
for (int i = 0; i<6; i++) {
array[i] = read.nextInt();
}
for (int i = 0; i<6; i++) {
if (array[i] > 0) {
sum = sum + array[i];
}
}
System.out.println(sum);
}
}

Better way to Convert multiple byte Binary to Signed Decimal in Objective C

As I know, it is easy to convert single byte to signed decimal by
char source_binary = '\xff' //-1
int signedInt = (int)source_binary //signedInt = -1
But I would like to know if any better way to handle in multiple byte
//Example -2, represent as 0xFFFE. I got two byte as follow:
char highByte = '\xff'
char lowByte = '\xfe'
Here is my solution:
- (int) convertTwoBytesBinaryToSignedInt:(char)highByte :(char)lowByte
{
int retValue = 0;
int hiValue = (int) highByte;
int loValue = 0;
if ((int)lowByte < 0)
loValue = 0 - ((lowByte ^ '\xff') + 1);
else
loValue = 0 - 256 + (int)lowByte;
if (hiValue < 0) {
//Negative Binary
retValue = (((hiValue+1)*256)+ loValue);
} else {
//Postive Binary
retValue = ((int)highByte)*256+(int)lowByte;
}
return retValue;
}
Would you please advise me any better way to convert it to signed decimal?
int word = ((int) highByte << 8) | (unsigned char) lowByte;
EDIT: insufficient testing. :)

Asking the user to enter in numbers, getting min/max/and average

Hi guys I'm have a real hard time for some reason trying to get this code worked out. My guide lines are:
Create a new Scanner object and save it into a variable name of your choice
Declare an integer variable for the current user’s input and initialize it to something that is NOT 0 (we will call it intInput in these instructions – the name is arbitrary, though)
Create a while loop, with the condition being that intInput is not equal to 0
Inside of this loop, call the nextInt() method of your Scanner object and store the value into intInput
I'm not getting any errors but its not working the way I thought it would.
and here is my code:
import java.util.Scanner;
// class name matches the file name
public class Lab5
{
// we must have a main method to run the program
`enter code here`public static void main(String[] args)
{
Scanner scan= new Scanner(System.in);
int userInput = 1;
int minVal = Integer.MIN_VALUE;
int maxVal = Integer.MAX_VALUE;
double average = 0;
int holdNum = 0;
double numSum = 0;
System.out.print ("Please enter numbers and to finish the program your last number should be 0: ");
numSum += userInput;
holdNum++;
while (userInput != 0)
{
userInput = scan.nextInt();
}
if (maxVal > Integer.MAX_VALUE)
{
maxVal = userInput;
}
if (minVal < Integer.MIN_VALUE)
{
minVal = userInput;
}
average = ( numSum ) / ( holdNum );
System.out.println( "Average = " + average );
System.out.println( "Max = " + maxVal );
System.out.println( "Minimum = " + minVal );
}
}
You are adding the user input in the sum and incrementing the number of user inputs only when the input is less than the minimum. I would guess you'll want to do it for all inputs instead.

How to increase an ipv6 address based on mask in java?

i am trying to increment ipv6 address based on mask.
i am getting problem when there is F in place of increment.
could any one plz check this
public String IncrementIPV6ForPrefixLength (String IPv6String, int times) throws UnknownHostException
{
int result , carry = 0, i;
int bits;
int mask=0;
int index=IPv6String.indexOf("/");
mask=Integer.parseInt(IPv6String.substring(index+1, IPv6String.length()));
IPv6String=IPv6String.substring(0, index);
InetAddress iaddr=InetAddress.getByName(IPv6String);
byte[] IPv6Arr=iaddr.getAddress();
if(mask > 128 || mask < 0)
return null;
i = mask/8;
bits = mask%8;
if(bits>0)
{
result = ((int)(IPv6Arr[i]>>(8-bits))) + times;
IPv6Arr[i] =(byte) ((result << (8-bits)) | (IPv6Arr[i] & (0xff >> (bits))));
carry = (result << (8-bits))/256;
times /= 256;
}
i--;
for(;i>=0;i--)
{
result = ((int)IPv6Arr[i]) + ((times + carry)& 0xFF);
IPv6Arr[i] = (byte)(result % 256);
carry = result / 256;
if(carry == 0)
{
iaddr=InetAddress.getByAddress(IPv6Arr);
String s=iaddr.toString();
if(s.indexOf('/') != -1){
s = s.substring(1, s.length()).toUpperCase();
}
StringBuffer buff =new StringBuffer("");
String[] ss = s.split(":");
for(int k=0;k<ss.length;k++){
int Differ = 4 - ss[k].length();
for(int j = 0; j<Differ;j++){
buff.append("0");
}
buff.append(ss[k]);
if(k!=7)buff=buff.append(":");
}
return buff.toString()+"/"+mask;
}
times /= 256;
}
return null;
}
input like this:
FD34:4FB7:FFFF:A13F:1325:2252:1525:325F/48
FD34:41B7:FFFF::/48
FD34:4FBF:F400:A13E:1325:2252:1525:3256/35
output like this
if increment by 1
FD34:4FB8:0000:A13F:1325:2252:1525:325F/48
FD34:41B8:0000::/48
FD34:4FC0:0400:A13E:1325:2252:1525:3256/35
if increment by 2
FD34:4FB8:0001:A13F:1325:2252:1525:325F/48
FD34:41B8:0001::/48
FD34:4FC0:1400:A13E:1325:2252:1525:3256/35
can u plz find where i am doing wrong.
Disregarding the posted code, try to model the operation as a direct numerical operation on the 128-bit number that the IPv6 address really is. Convert to BigInteger and use BigInteger.add.