There's a while loop in my program that will loop infinitely because the for loop inside won't run twice. I want to find the number of combinations that will have a sum of 4 from input and here's my code:
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
ArrayList<Integer> taxi = new ArrayList<Integer>();
for (int i=0; i<n; i++) {
taxi.add(scan.nextInt());
}
int i = 0;
int total=0;
int tax=0;
int num = 0;
while (num<n) {
i=0;
for (i=0; i<n; i++) {
if (total+taxi.get(i)<=4) {
total+=taxi.get(i);
System.out.println(total);
num++;
}
}
tax++;
}
System.out.println("Taxis= " + tax);
looking though you're code it seems your issue lies in the usage of some of your variables such as I multiple times which causes the code to not compile. try using better names or more unique names in order to prevent these issues in future. as well as that I would also advise not zeroing all of your variables so often in such a manner. Finally your issue has to do with your usage of the variable n as it remains a fixed number which will not change in the while loop meaning it will always be true. the best way to fix this is to utilize the
break;
command to exit the while loop or to use a counted loop like a for loop.
In my honest opinion i would request that when asking these questions next time check all the variables with a print line in the appropriate spot.
Related
I assume that if I can't convert my while loop into a for loop, then I don't fully understand the concepts here. Here's my working while loop:
(I am trying to implement a program, which calculates the sum 1+2+3+...+n where n is given as user input.)
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number: ");
int number = Integer.valueOf(scanner.nextLine());
int sum = 0;
int i = 0;
while (i < number) {
i++;
sum += i;
}
System.out.println(sum);
I was looking at this user's answer here: https://stackoverflow.com/a/36023451/11522261
and tried to implement it, but it's not working.
.....
for (i = 0; i < number; i++){
sum += i;
}
System.out.println(sum);
It looks like the conditions of For Init expression statement and ForUpdate are set right, but I'm having trouble understanding the differences here.
Also, it looks like this exercise is trying to teach loops to solve iterative problems. But I suspect that this isn't helping me practice recursion. Perhaps there is a recursive solution to this problem which would be better. Just thinking out loud here. Thanks!
The difference is that in your while loop, you're incrementing i before adding it to sum, but in your for loop, it's after.
If the while is producing the right result, you can adjust your for by starting at 1 instead of 0 and continuing while <= number instead of < number.
For completeness, here's how your current for loop works:
i = 0
If i < number is not true, exit the loop; otherwise, continue to Step 3
sum += i (i is still 0)
i++
Goto Step 2
On the second pass, i < number is 1 < number so still true if number is greater than 1, so you go to Step 3, do sum += i while i is 1, then continue.
Eventually i < number isn't true anymore, and the loop exits.
For problems like this, the best approach is usually to use the debugger built into your IDE to step through the code statement by statement, looking at the values of variables as you go. That can reveal how things work really well. This article may be helpful: How to debug small programs
Since you are incrementing the value of i before adding it to sum in the while loop, the same thing needs to be done in case of for loop as well. Given below is the implementation using the for loop:
for (i = 0; i++ < number; ){
sum += i;
}
System.out.println(sum);
Use <= instead of <. This will solve Your problem, and make sure you understand why will the following code would work. I would recommand you to use a paper and pencil and start writing down the values of i and sum after every iteration.
for (i = 1; i <= number; i++) {
sum += i;
}
I am trying to take input into the variable n. However, it gets infinitelly stuck when trying to take input.
import java.util.*;
public class Hello {
public static void main(String[] args) {
//Your Code Here
int i,r=0,max=0;
Scanner s=new Scanner(System.in);
//int i,r=0,max=0;
int n=s.nextInt();
for(i=1;i<=n;i++)
{
while(i>0)
{
r+=i%10;
i/=10;
}
if(max<=r)
max=r;
r=0;
}
System.out.print(max);
}
}
I want the program to take one input, then proceed further in the program and compute the result. But, instead it gets stuck while trying to recieve input.
The problem is that the inner while loop will terminate when i becomes zero. But, the outer for loop will happily keep iterating. So the infinite loop is actually the for loop, which will never end. Without knowing exactly what your code is supposed to be doing, I can suggest that you use another variable instead of i directly:
for (i=1; i <= n; i++) {
int j = i;
while (j > 0) { // use variable j to do the math; don't change loop counter i
r += j % 10;
j /= 10;
}
if (max <= r) { // possibly record a new max value
max = r;
}
r = 0; // always reset r
}
You should use while(i>1) instead. Using while(i>0) doesn't work because you're trying to reach 0 through division, so the loop becomes infinite.
int[] a = new int[101];
int n = in.nextInt();
for(int i = 0; i < n; i++){
a[in.nextInt()]++;
}
a is an array that is initialized; the next line get's you the amount of numbers that will follow after that.
Assuming that in is an initialized Scanner, nextInt() will read the next int from the console. Usually on websites like codingames.com or apparently hackerrank use this construct to put in data for your code to test it.
In the loop the code will read an index to be used in the pre-initialized array to increase the value by one at that given index.
That is basically it. To make it clearer, you could write:
int[] a = new int[101];
int numberOfIntsToFollow = in.nextInt();
for(int i = 0; i < numberOfIntsToFollow; i++){
int x = in.nextInt();
a[x]++;
}
I been trying to write a program that can display the fibonacci sequence. However any number I input will output 0. I suspect that it has either to do with scope of the variable or something wrong with my return. could someone look at my code and figure out if it is really those problems? I'm sorta new to java, so even the basic is difficult for me.
public static void main(String args [])
{
Scanner in = new Scanner(System.in);
int number = 0;
do{
System.out.print("Which Fibonacci Number would you like? ");
fib = in.nextInt();
}while(number < 0 || number > 71);
System.out.print("Fibonacci #"+number+" is "+fibcalc(fib)+"\n");
}
public static double fibcalc(double number)
{
double prevNumber1 = 0;
double prevNumber2 = 1;
double fib = 0;
for(int i =0; i < number; i++){
fib = prevNumber1;
prevNumber1 = prevNumber2;
prevNumber2 = fib + prevNumber2;
}
return fib;
}
made some revisions, down to one error.
error: cannot find symbol
System.out.print("Fibonacci #"+number+" is "+fibcalc(fib)+"\n");
symbol: variable fib
I got a qucik question.
can one method call upon another method for a variable? the variable is inside of the curly braces. kind of what I have in my code.it seems like most of my errors were similar to this one.
It's nothing to do with scope. Look at this code:
while(i < fib){
fib = prevNumber1;
...
}
You're using the fib variable as both "the number of iterations to execute" and "the current result". Those are two separate concepts, and should be stored in separate variables.
In particular, on the first iteration, fib will be set to 0 and i will be incremented to 1... so your loop will then terminate, returning 0.
I'd also suggest changing your parameter type to int and using long or BigInteger for the Fibonacci number variables (depending on what range you want to support). There's no part of this problem which needs non-integer values, so you shouldn't be using double at all.
Finally, I'd suggest using a for loop instead of a while loop - you want to perform a given number of iterations, and the idiomatic way of writing that in Java is:
for (int i = 0; i < count; i++) {
// whatever
}
I'm having trouble setting up and placing values into an array using a text file containing the floating point numbers 2.1 and 4.3 each number is separated by a space - below is the error I'm getting:
Exception in thread "main" java.util.NoSuchElementException
import java.util.*;
import java.io.*;
public class DoubleArray {
public static void main(String[] args) throws FileNotFoundException {
Scanner in = new Scanner(new FileReader("mytestnumbers.txt"));
double [] nums = new double[2];
for (int counter=0; counter < 2; counter++) {
int index = 0;
index++;
nums[index] = in.nextDouble();
}
}
}
Thanks, I'm sure this isn't a hard question to answer... I appreciate your time.
You should always use hasNext*() method before calling next*() method
for (int counter=0; counter < 2; counter++) {
if(in.hasNextDouble(){
nums[1] = in.nextDouble();
}
}
but I think you are not doing the right, I'd rather
for (int counter=0; counter < 2; counter++) {
if(in.hasNextDouble(){
nums[counter] = in.nextDouble();
}
}
NoSuchElementException is thrown by nextDouble method #see javadoc
I would suggest printing the value of index out immediately before you use it; you should spot the problem pretty quickly.
It would appear you're not getting good values from your file.
Oli is also correct that you have a problem with your index, but I would try this to verify you're getting doubles from your file:
String s = in.next();
System.out.println("Got token '" + s + "'"); // is this a double??
double d = Double.parseDouble(s);
EDIT: I take this partly back...
You simply don't have tokens to get. Here's what next double would have given you for exceptions:
InputMismatchException - if the next token does not match the Float
regular expression, or is out of range
NoSuchElementException - if the input is exhausted
IllegalStateException - if this scanner is closed
I did not understand what you are trying to do in your loop ?
for (int counter=0; counter < 2; counter++) {
int index = 0;
index++; <--------
nums[index] = in.nextDouble();
}
You are declaring index = 0 then incrementing it to 1 and then using it.
Why are you not writing int index = 1; directly ?
Because it is getting declared to be zero each time loop is run and then changes value to 1.
Either you should declare it out side the loop.
You should initialize index outside of your for loop.
int index = 0;
for (int counter=0; counter < 2; counter++)
{
index++;
nums[index] = in.nextDouble();
}
Your index was getting set to zero at the beginning of each iteration of your for loop.
EDIT:
You also need to check to make sure you still have input.
int index = 0;
for (int counter=0; counter < 2; counter++)
{
if(!in.hasNextDouble())
break;
index++;
nums[index] = in.nextDouble();
}
Every time the cycle does an iteration, it's declaring the variable index and then you increase index with index++. Instead of using index, use counter, like this: num [counter] = in.nextDouble().
Check your mytestnumbers.txt file and ensure that the data that you are trying to scan is in the correct format. The exception that you are getting implies it is not.
Keep in mind that in.nextDouble() will be searching for double numbers separated by white space. In other words, "4.63.7" is not equal to "4.6 3.7" — the space is required. (I do not remember off the top of my head, but I believe that nextDouble() will only search for numbers containing a decimal point, so I do not believe that "4" is equal to "4.0". If you are seeking decimal numbers with this method, then you should have decimal numbers in your file.)