The code in the for loop is not executing - java

I created a while loop calculate the area and compare the price of that area with the given price and if the given price is lower then our result is the calculated area. I need to print all the result in the end when the while loop break. Therefore I am storing the result in an array to print it when the while loop breaks but the for at the end is not executing.
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
String[] result = new String[t];
while (t != 0) {
int j = 0;
int n = sc.nextInt();
int b = sc.nextInt();
int w[] = new int[n];
int h[] = new int[n];
int p[] = new int[n];
int area[] = new int[n];
for (int i = 0; i < n; i++) {
w[i] = sc.nextInt();
h[i] = sc.nextInt();
p[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
area[i] = w[i] * h[i];
System.out.println("Area: " + area[i]);
}
int count = 0;
for (int i = 0; i < n; i++) {
if (b >= p[i]) {
count++;
}
}
System.out.println("count: " + count);
if (count == 0) {
result[j] = "no tablet";
} else {
int check[] = new int[count];
for (int i = 0; i < count; i++) {
if (b >= p[i]) {
check[i] = area[i];
}
}
Arrays.sort(check);
result[j] = "" + check[count - 1];
}
j++;
t--;
}
for (int i = 0; i < t; i++) {
System.out.println("Result: " + result[i]);
}

Here is the correct code. I have removed the error and put some prompt for user to enter values. your last for loop was not running because in while loop your value of 't' becomes 0, and it the end you were using that t in the for loop condition, that's why it was not running. I have used another variable for that. so Your problem is resolved now. Here is the complete correct code.
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
String[] result=new String[t];
int x = t;
while(t!=0) {
int j=0;
int n=sc.nextInt();
int b=sc.nextInt();
int w[]=new int[n];
int h[]=new int[n];
int p[]=new int[n];
int area[]=new int[n];
for(int i=0;i<n;i++) {
System.out.println("Enter Width for w["+i+"]: ");
w[i]=sc.nextInt();
System.out.println("Enter Height for h["+i+"]: ");
h[i]=sc.nextInt();
System.out.println("Enter Price for w["+i+"]: ");
p[i]=sc.nextInt();
}
for(int i=0;i<n;i++) {
area[i]=w[i]*h[i];
System.out.println("Area A["+i+"]: " +area[i]);
}
int count=0;
for(int i=0;i<n;i++) {
System.out.println("b = "+b+" and p["+i+"] = "+p[i]);
if(b>=p[i]) {
count++;
}
}
System.out.println("count: "+count);
if(count==0) {
result[j]="no tablet";
}else {
int check[]=new int[count];
for(int i=0;i<count;i++) {
if(b>=p[i]) {
check[i]=area[i];
}
}
Arrays.sort(check);
result[j]=""+check[count-1];
}
j++;
t--;
}
for(int i=0;i<x;i++) {
System.out.println("Result: "+result[i]);
}

you must check your list grater than notation
like that
while(t>0)
and area you must declaration outside while loop
i think like that
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
String[] result=new String[t];
List area=new List<int>();
while(t>0) {
int j=0;
int n=sc.nextInt();
int b=sc.nextInt();
int w[]=new int[n];
int h[]=new int[n];
int p[]=new int[n];
for(int i=0;i<n;i++) {
w[i]=sc.nextInt();
h[i]=sc.nextInt();
p[i]=sc.nextInt();
}
for(int i=0;i<n;i++) {
area.Add(w[i]*h[i]);
System.out.println("Area: "+area[i]);
}
int count=0;
for(int i=0;i<n;i++) {
if(b>=p[i]) {
count++;
}
}
System.out.println("count: "+count);
if(count==0) {
result[j]="no tablet";
}else {
int check[]=new int[count];
for(int i=0;i<count;i++) {
if(b>=p[i]) {
check[i]=area[i];
}
}
Arrays.sort(check);
result[j]=""+check[count-1];
}
j++;
t--;
}
for(int i=0;i<t;i++) {
System.out.println("Result: "+result[i]);
}
}
}

Related

Having trouble searching array

So I have everything working fine up until the point in which I need to search. I'm really new to this so my code is probably awful I'm sorry in advance. Anyway, its a user input array, and the user should be able to search for a number in an array. Im getting the error for a duplicate variable on line 50 (int i, get 1).
import java.util.Scanner;
class SearchingSorting {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
System.out.println ("How many numbers would you like to input?");
int num = input.nextInt();
double[] array = new double[num];
for (int i = 0; i < num; i++) {
System.out.println ("Input number " + (1 + i) + ":");
array[i] = input.nextDouble();
}
for (double temp1 : array){
System.out.print (temp1 + "\t");
}
input.close();
int pass;
int i;
int hold;
for(pass = 1; pass < array.length; pass++)
{
for(i = 0; i < array.length - 1; i++)
{
if(array[i] > array[i+1])
{
hold = (int) array[i];
array[i] = array[i+1];
array[i+1] = hold;
}
}
System.out.println("\nSorted number is: ");
for(i = 0; i < array.length; i++)
System.out.print(" " + array[i]);
}
int i, get1;
Scanner keyboard = new Scanner(System.in);
int[] numbers = new int[10];
for(i = 0; i < numbers.length; i++)
{
numbers[i] = i * 10;
}
System.out.print("Enter search number: ");
get1 = keyboard.nextInt();
SearchMethod(numbers, get1);
}
public static void SearchMethod(int[] num, int get2)
{
int i ;
boolean j = false;
for(i = 0; i < num.length; i++)
{
if(num[i] == get2)
{
j = true;
break;
}
}
if(j == true)
System.out.println(get2 + " is found at num[" + i + "]");
else
System.out.println(get2 + " is not found in an array");
}
}
You are trying to declare a new variable with the same name ("i") in the same scope.
Rename your variable i on line 50.

Using for loop to find an element that appears multiple times in a random, unsorted array

My goal is to print out a user input value (and its corresponding index) if it appears 1 or more times in a random generated array of 50 integers. If I search the array for a value, and it happens to appear more than once, however, only one location of the element is printed, and then the if-else statement is executed. If I remove the break at the end of the third for loop, the whole thing falls apart. I've attached an image but here is the part of it that is giving me an issue. Apologies if the code is not clean, I'm very new.
public static void main(String[] args) {
System.out.println("IDS201 HW3:\n");
System.out.println("1. Generate 50 random integer unsorted list.\n");
Scanner stdin = new Scanner(System.in);
int[] randomNumbers = new int[50];
for(int index = 0; index < randomNumbers.length; index++) {
randomNumbers[index] = (int) (Math.random()*100);
}//end for
int count = 0;
for(int i = 0; i < randomNumbers.length; i++) {
System.out.print(randomNumbers[i] + ",");
count++;
if(count == 10) {
System.out.println();
count = 0;
}
}//end for
System.out.println("\nSearch value?");
int x = stdin.nextInt();
int i;
for(i = 0; i < randomNumbers.length; i++) {
if(randomNumbers[i] == x)
break;}
if (i != randomNumbers.length) {
System.out.println("\nFound " + x + " in array [" + i + "]");}
else {
System.out.println(x + " is not in the list");}
{int temp;
int size = randomNumbers.length;
for(i = 0; i<size; i++ ){
for(int j = i+1; j<size; j++){
if(randomNumbers[i]>randomNumbers[j]){
temp = randomNumbers[i];
randomNumbers[i] = randomNumbers[j];
randomNumbers[j] = temp;
}
}
}
System.out.println("\nSmallest element of the array is: " + randomNumbers[0]);}
System.out.println("\n3. Sort the list:");
int size = randomNumbers.length;
for(i=0; i<size; i++)
{
for(int j=i+1; j<size; j++)
{
if(randomNumbers[i] > randomNumbers[j])
{
int temp = randomNumbers[i];
randomNumbers[i] = randomNumbers[j];
randomNumbers[j] = temp;
}
}
}
System.out.print("Now the Array after Sorting is :\n\n");
int count1 = 0;
for(i=0; i<size; i++)
{
System.out.print(randomNumbers[i]+ ",");
count++;
if(count == 10) {
System.out.println();
count = 0;
}
}
}
}
if statement in for loop
Implementing the comments to your question:
import java.util.Scanner;
public class NumCount {
private static final int RANDOM_NUMBER_COUNT = 50;
private static void display(int[] randomNumbers) {
int count = 0;
for (int i = 0; i < RANDOM_NUMBER_COUNT; i++) {
System.out.print(randomNumbers[i] + ",");
count++;
if (count == 10) {
System.out.println();
count = 0;
}
}
}
private static int[] generateRandomNUmbers() {
int[] randomNumbers = new int[RANDOM_NUMBER_COUNT];
for (int index = 0; index < RANDOM_NUMBER_COUNT; index++) {
randomNumbers[index] = (int) (Math.random() * 100);
}
display(randomNumbers);
return randomNumbers;
}
private static int search(int[] randomNumbers, int x) {
int i;
int count = 0;
for (i = 0; i < randomNumbers.length; i++) {
if (randomNumbers[i] == x) {
System.out.println("\nFound " + x + " in array [" + i + "]");
count++;
}
}
return count;
}
private static int[] sort(int[] randomNumbers) {
int size = randomNumbers.length;
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
if (randomNumbers[i] > randomNumbers[j]) {
int temp = randomNumbers[i];
randomNumbers[i] = randomNumbers[j];
randomNumbers[j] = temp;
}
}
}
return randomNumbers;
}
/**
* Start here.
*/
public static void main(String[] args) {
System.out.println("IDS201 HW3:\n");
System.out.println("1. Generate " + RANDOM_NUMBER_COUNT + " random integer unsorted list.\n");
int[] randomNumbers = generateRandomNUmbers();
System.out.print("\n2. Search value? ");
Scanner stdin = new Scanner(System.in);
int x = stdin.nextInt();
int count = search(randomNumbers, x);
if (count == 0) {
System.out.println(x + " is not in the list");
}
System.out.println("\n3. Sort the list:");
sort(randomNumbers);
System.out.print("Now the Array after Sorting is :\n\n");
display(randomNumbers);
}
}
Of-course there is no need to search for the smallest number because it will be the first element in the sorted array. Hence I removed that part of your code.

Can you help me with my codes about methoding?

I need some help with my homework, so basically we need to create a code in which we input 10 numbers in an array, then search the array if my inputted number is there, most of the code is done. I just need to method the loop for searching if the inputted number is in the array Thanks for the help!!!
public static void main(String[] args) {
int n, x, flag = 0, i = 0;
Scanner s = new Scanner(System.in);
int a[] = new int[10];
System.out.println("Enter all the values:");
for(i = 0; i < 10; i++)
{
System.out.print("Value "+(i+1)+": ");
a[i] = s.nextInt();
}
System.out.print("Enter the value you want to find: ");
x = s.nextInt();
for(i = 0; i < 10; i++)
{
if(a[i] == x)
{
flag = 1;
break;
}
else
{
flag = 0;
}
}
if(flag == 1)
{
System.out.println("The value "+x+" is found at index "+(i+1));
}
else
{
System.out.println("The value "+x+" is found at index "+(-1));
}
}
public static void main(String[] args) {
int n, x, flag = 0, i = 0;
Scanner s = new Scanner(System.in);
int a[] = new int[10];
System.out.println("Enter all the values:");
for(i = 0; i < 10; i++)
{
System.out.print("Value "+(i+1)+": ");
a[i] = s.nextInt();
}
System.out.print("Enter the value you want to find: ");
x = s.nextInt();
i = find(a, 10, x);
if(i != -1)
{
System.out.println("The value "+x+" is found at index "+(i+1));
}
else
{
System.out.println("The value "+x+" is found at index "+(-1));
}
}
private static int find(int a[],int n, int x) {
int i, flag = 0;
for(i = 0; i < n; i++)
{
if(a[i] == x)
{
flag = 1;
break;
}
else
{
flag = 0;
}
}
if(flag == 1) {
return i;
} else {
return -1;
}
}
public class Main {
public static int findIndexOfNumber(int numberToSearch, int[] numbers) {
for(int i=0; i<numbers.length; i++) {
if(numbers[i]==numberToSearch) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
int n, x, flag = 0, i = 0;
Scanner s = new Scanner(System.in);
int a[] = new int[10];
System.out.println("Enter all the values:");
for(i = 0; i < 10; i++) {
System.out.print("Value "+(i+1)+": ");
a[i] = s.nextInt();
}
System.out.print("Enter the value you want to find: ");
x = s.nextInt();
flag = findIndexOfNumber(x, a);
System.out.println("The value "+x+" is found at index "+flag);
}
}
You can try this also

Duplicate even number from first array into another array

I have an issue where it seems like when I try to display my new array full of even numbers from the first array, it only outputs the last value? I don't see where the problem lies within the nested for loop inside my GetEven method?
package allevenproj;
import java.util.*;
public class AllEvenProj {
static int Read(int[] arr) {
Scanner scan = new Scanner(System.in);
int count = 0;
for (int i = 0; i < arr.length; i++) {
System.out.printf("Enter arr[%d]: ", i);
arr[i] = scan.nextInt();
if (arr[i] % 2 == 0) {
count++;
}
}
System.out.printf("There are %d even numbers\n", count);
return count;
}
static int[] GetEven(int[] arr, int count) {
int[] evenArr = new int[count];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < count; j++) {
if (arr[i] % 2 == 0) {
evenArr[j] = arr[i];
}
}
}
return evenArr;
}
static void Print(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter size of array: ");
int n = scan.nextInt();
int[] arr = new int[n];
int a = Read(arr);
Print(GetEven(arr, a));
}
}
You'are getting only the last value because due to the inner for loop in the GetEven method: every time you do a full inner loop (you do it for every number in arr) you rewrite the whole evenArr.
So the fix is removing the inner loop:
static int[] getEven(int[] arr, int count) {
int[] evenArr = new int[count];
int j = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 == 0) {
evenArr[j++] = arr[i];
}
}
return evenArr;
}
By the way, methods name should be lowercase. Naming conventions
As Ruben said the problem is in your loop. I took the liberty to refactor your code a bit just to show you a better approach to the problem
package allevenproj;
import java.util.*;
public class AllEvenProj {
static int[] read(int size) {
Scanner scan = new Scanner(System.in);
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
System.out.printf("Enter arr[%d]: ", i);
arr[i] = scan.nextInt();
}
return arr;
}
static void print(int[] arr) {
System.out.printf("There are %d even numbers\n", arr.length);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter size of array: ");
int size = scan.nextInt();
print(Arrays.stream(read(size)).filter(x -> x % 2 == 0).toArray());
}
}

Memory Function Knapsack

Ok, I am having a problem with the Memory Function implementation of the Knapsack problem. My implementation if giving me the incorrect answer in Java but in C++ the same implementation is giving me the correct answer.
import java.io.*;
import java.util.*;
public class KnapsackMF {
public static void main(String args[]){
int wt;
int vl;
long start, end, runTime;
Scanner input = new Scanner(System.in);
int n, W;
System.out.println("Please enter the number of items: ");
n = input.nextInt();
System.out.println("Please enter the capacity of the knapsack: ");
W = input.nextInt();
int[][] V = new int[n+1][W+1];
int[] Wt = new int[n];
int[] Vl = new int[n];
for(int i = 0; i < n; i++){
System.out.println("Enter the weight and the value of item " + i + ": ");
wt = input.nextInt();
vl = input.nextInt();
Wt[i] = wt;
Vl[i] = vl;
}
for(int i = 0; i <= n; i++ ){
for(int j = 0; j <= W; j++){
if(j == 0 || i == 0){
V[i][j] = 0;
}
else{
V[i][j] = -1;
}
}
}
/*
for(int i= 0; i <= n; i++){
for(int j = 0; j <= W; j++){
System.out.print(V[i][j] + " ");
}
System.out.println();
}
*/
start = System.nanoTime();
System.out.println("The highest value is: " + MFKnapsack(n, W, V, Vl, Wt));
end = System.nanoTime();
runTime = (end - start);
System.out.println("Nanoseconds: " + runTime);
for(int i= 0; i <= n; i++){
for(int j = 0; j <= W; j++){
System.out.print(V[i][j] + " ");
}
System.out.println();
}
//system("PAUSE");
}
public static int MFKnapsack(int i, int j, int[][] V, int[] Vl, int[] Wt){
int value = 0;
if(V[i][j] < 0){
if(j < Wt[i-1]){
value = MFKnapsack(i-1, j, V, Vl, Wt);
}
else{
value = Math.max(MFKnapsack(i-1, j, V, Vl, Wt), Vl[i-1] + MFKnapsack(i-1, j-Wt[i-1], V, Vl, Wt));
}
}
V[i][j] = value;
return V[i][j];
}
}
The example I used is out of my book, the inputs should be:
4
5
2 12
1 10
3 20
2 15
The answer to this should be 37 but something is going on that I am not seeing and it is giving me 35.

Categories

Resources