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 am trying to make a game in java but have run into what seems like a simple problem. I need some way to use a loop to print out multiple things but not the traditional way. Basically what i need to do is this:
instead of:
for(int i=0;i<5;i++)
{
e.get(i);
}
i need to do this:
for(int i=0;i<5;i++)
{
e.get(0);
e.get(1); //but 1 and above can only be there after a number has been increased past 0
e.get(2);
e.get(3);
e.get(4);
}
where changing i would also change how many "e.get()"s you have.
Any ideas?
to clear things up:
this will not work:
public static void main(String[] args)
{
int l=5;
for(int i=0;i<l;i++)
{
for(int o=0;o<l;o++)
{
e.get(o);
}
}
}
but something along the lines of this will:
public static void main(String[] args)
{
e.get(0);
e.get(1); //but 1 and above can only be there after a number has been increased past 0
e.get(2);
e.get(3);
e.get(4);
}
I have tried the nested for-loop but it does not work for my program.
for my program to work, each "e.get(0);" needs to physically be there.
sorry if im making this unclear, i have been programming for like 6 hours straight and am reaching a wall :/
int num = 5;
for (int i=0; i<num; i++)
for (int j=0; j<num; j++)
e.get(j);
Edit:
Do you mean this?
int num = 5;
e.get(0);
for (int i=1; i<num; i++)
for (int j=0; j<num; j++)
e.get(j);
for(int i = 0; i < 5; i++)
{
for (int j = 0; j < i; j++)
{
e.get(j);
}
}
Something like this?
I would try e.length;
for(int i=0;i<e.length;i++)
{
e.get(i);
}
So something like this?
for (int i = 0; i < 5; i++) {
e.get(0);
if (i > 0)
e.get(1); //but 1 and above can only be there after a number has been increased past 0
if (i > 1)
e.get(2);
if (i > 2)
e.get(3);
if (i > 3)
e.get(4);
}
That would make certain all of the e.get() calls are in your code, and I believe would call them correctly if I understand what you want (which I probably don't!).
Related
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
just getting into java.
What is the best way to encounter this issue?
I understand what is happening, and I am asking for the best way to solve it, cause my solution is a bit ugly I might say.
public static int jumpingOnClouds(List<Integer> c) {
int size = c.size();
int jumpx = 0;
for(int i = 0; i<size; i++){
jumpx++;
if(c.get(i+2)==0)i++; //Last 2 iteration will gives the error.
}
return jumpx;
}
My newbie solution
public static int jumpingOnClouds(List<Integer> c) {
int size = c.size();
int jumpx = 0;
for(int i = 0; i<size; i++){
jumpx++;
if (!(i+1>=size || i+2>=size)){ //adding this to avoid c.get(i+2) to be executed.
if(c.get(i+2)==0)i++;
}
if(i+2>=size || i+1>=size)break; //adding this to avoid jumpx++ to be executed.
}
return jumpx;
}
I might be a bit blur, but I hope someone can explain and shows the best one (with the code).
Thank you.
You can validate the i + 2 value to be inside the range before trying to perform c.get(i+2) to avoid the exception
public static int jumpingOnClouds(List<Integer> c) {
int size = c.size();
int jumpx = 0;
for(int i = 0; i<size; i++){
jumpx++;
if(i+2 <size && c.get(i+2)==0)i++;
}
return jumpx;
}
A simpler way to modify the original code (without encountering an IndexOutOfBoundsException) would be:
public static int jumpingOnClouds(List<Integer> c) {
int size = c.size();
int jumpx = 0;
for(int i = 0; (i + 2) < size; i++){
jumpx++;
if(c.get(i+2)==0)i++;
}
return jumpx;
}
Since you are modifying i, and need to access from the (i + 2)-th index, you need to ensure that (i + 2) < size, since size - 1 is the last valid index in the List
// here you are looping through all elements in the list
for(int i = 0; i<size; i++){
jumpx++;
//here you are getting the "i + 2"th element.
//This is going to be out of bounds if i is the 2nd
//to last or the last element
if(c.get(i+2)==0)i++;
}
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 4 years ago.
Improve this question
so my real question is, how can i make this code identify all the "look alike" numbers while theire running from 1 to 99, for example :11,22,33,44,...
and while the program identify them it sends a message.
package doodle;
int num2=11;
for (int i=1; i<100; i++) {
System.out.println(i);
int num1=i;
if(num1==num2) {
System.out.println("WOW");
}
}
Thanks
I would do using a String
for (int i = 11; i < 100; i++) {
StringBuffer orig = new StringBuffer();
String left = orig.append(i).toString();
if (orig.reverse().toString().equals(left)) {
System.out.println(left);
}
}
or if you really wanted to use an int with flaky logic
int start = 11;
for (int i = 11; i < 100; i++) {
if (i == start) {
System.out.println(start);
start += 11;
}
}
Edit
As #mark has rightly pointed out, these solution only work whilst the range is up to 100
int num2=11;
for (int i=1; i<100; i++) {
if(i%num2==0) { //<---- look alike
System.out.println("WOW");
}
I would do it using String conversion and codePoint comparison
for (Integer number = 0; number < 1000; number++) {
System.out.println(number);
String stringnumber = String.valueOf(number);
if (stringnumber.length() > 1 && stringnumber.codePoints().allMatch((digit) -> digit == stringnumber.codePointAt(0))) {
System.out.println("WOW");
}
}
length check (length() > 0) is needed to exclude all numbers with only one digit, otherwise, the program would print "WOW" for all numbers from 0 - 9 too.
All numbers from 0 to Integer.MAX_VALUE can be handled.
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
I wanted the output to be 1, 4, 9, 16, 25.... etc.
Here's my code:
public class ArrayDemo{
public static main(String[]args){
int [] value = new int[20];
int copy = value[1];
for(int i = 0; i <values.length; i+=3)
{
i = i + 1;
System.out.println(i);
}
I know I'm doing something wrong.. just can't seem to figure out what. It just prints out 1, 4, 7, .. etc. -___- any help would be greatly appreciated!
to print out squares of numbers try
for (int x = 1; x <= 5; x++) {
System.out.println (x*x);
}
Change this:
for (int i=0; i<value.length; i+=3)
{
i = i + 1;
System.out.println(i);
}
To This:
for (int i=1,j=3; i<value.length; i+=j,j+=2)
{
System.out.println(i);
}
Of course, you can run a standard loop and print i*i, but I suppose you wanted to do it differently.
By the way, you may as well get rid of that value array, and use 20 instead of value.length.
for ( int i = 0; i < 20; i++) {
System.out.println(i*i);
}
And that's all
First of all, it is not clean code, the array "value" and the variable "copy" don't have any real purpose.
Secondly, it will not even compile, as you have array named "value" and the "for" loop refers to "values".
Thirdly, you want to print squares of integers from 1 to 20. Better way to do it would be:
for (int i = 1; i <= 20; i++)
System.out.println (i * i);
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
I have written this java program for sorting some numbers. But it doesn't stop executing. Can you help me with it?
package inplacesort;
import java.util.*;
public class InplaceSort
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
Vector <Integer> intList = new Vector <Integer> ();
//getting the numbers from the user
char ans = 'y';
while (ans == 'Y' || ans == 'y')
{
System.out.print("Enter a Number: ");
intList.addElement(console.nextInt());
System.out.print("Do You Want to Continue?(Y/N)");
ans = console.next().charAt(0);
}
System.out.println("Before Sorting the Numbers: " + intList);
for (int i = 1; i < intList.size(); i++)
{
int j = i - 1;
while (j > 0 && intList.elementAt(i) < intList.elementAt(j))
{
j--;
}
for (int k = intList.size() - 1; k >= j; k--)
{
intList.insertElementAt(intList.elementAt(k),k + 1);
}
intList.insertElementAt(intList.elementAt(i+1),j);
intList.removeElementAt(i+1);
}
System.out.print(intList);
}
}
Your problem is with intList.size() in the k & i loops. This is not be as what you would expect it. When I debugged your code the value of k was 425996.
Edit :
When i debugged it more I saw that because of you mutating the vector within it self it keeps increasing in size. If you let your program run for a few minutes you will get out of memory error.
Please don't mutate the object you are looping though it. Either make a copy of it and the loop though one of them and mutate another or start with a fresh one & keep adding the values to it while looping over the older one.
System.out.println("Before Sorting the Numbers: " + intList);
List<Integer> sortList = new ArrayList<Integer>();
int minVal;
int index=0;
int size = intList.size();
for (int i = 0; i < size; i++)
{ minVal=Integer.MAX_VALUE;
for (int j = 0; j < intList.size(); j++)
{
if(intList.get(j) < minVal){
index=j;
minVal=intList.get(j);
}
}
intList.remove(index);
sortList.add(minVal);
}
System.out.print("After Sorting the Numbers: "+ sortList);
The reason is because your value for j is ALWAYS 1 less than the value for i. Therefore, infinite while loop.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
simple beginner Java question. I just learned how to use arrays and it's still a little confusing to me. My goal is to simply roll a number of dice a number of times and then project it as a sum, frequency, and percentage. I'm sure I'm doing something dumb, but I'm overlooking it!
import javax.swing.JOptionPane;
import java.util.Random;
public class Lab1 {
private static int N = 0;
private static int M = 0;
private static int total = 0;
private static Random rnd = new Random();
public Lab1(){
}
public static void main(String[] args) {
N = Integer.parseInt(JOptionPane.showInputDialog("How many dice would you like to roll?"));
System.out.println("Dice: "+N);
M = Integer.parseInt(JOptionPane.showInputDialog("How many times would you like to roll?"));
System.out.println("Rolls: "+M);
int total[] = new int[(6*Lab1.N)+1];
for (int i=0; i<=total.length; i++)
total[i] = 0;
for (int roll=1; roll<=M; roll++){
N = 1+rnd.nextInt(6);
total[N]++;
}
System.out.printf("%3s%12s%12s\n", "Sum","Frequency", "Percentage " );
for(int k=2; k<total.length; k++);{
int percent = total[k]/(360);
System.out.printf("%3s%12s%12s\n", k, total[k], percent);
}
}
}
From what I can see the question is how can you store the previous roles of the dice. And I believe your problem is with this method:
for (int roll=1; roll<=M; roll++){
N = 1+rnd.nextInt(6);
total[N]++;
}
I would change this to
for (int roll=1; roll<=M; roll++){
total[roll] = rnd.nextInt(6);
}
This will build up an array storing each dice roll - if that is of course what you are looking for...
Two things.
First, this loop will inevitably throw ArrayIndexOutOfBoundsException ("element" total[total.length] is out of bounds)
for (int i=0; i<=total.length; i++)
total[i] = 0;
You should use < instead of <=.
for (int i=0; i<total.length; i++)
total[i] = 0;
Second, this line here:
for(int k=2; k<total.length; k++);{
You have an empty loop here. You should remove the semicolon before the {:
for(int k=2; k<total.length; k++){
Now your code compiles, doesn't throw exceptions on the start, and prints a pretty table.
That's a start.
for(int k=2; k<total.length; k++);{
You need to remove the ; symbol from your loop as 'k' will not be resolved in the loop as you have terminated it. The format is for(x, x, x) {
The next thing to look at now is:
Dice: 1
Rolls: 1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at Lab1.main(Lab1.java:26)
Hint:
total[i] = 0; // this line is the problem.
Look at your <= in the loop.
for (int i=0; i<total.length; i++)
Simply chaging it to < results in this:
Dice: 1
Rolls: 1
Sum Frequency Percentage
2 1 0
3 0 0
4 0 0
5 0 0
6 0 0