473,394 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

why does this have infinite loop?

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3. import java.lang.*;
  4. public class ArrayNo9 {
  5.  
  6.  static Scanner console = new Scanner(System.in);
  7.     public static void main(String[]args) {
  8.  
  9.      int[][] inStock = new int[10][4];
  10.      //inStock[0] = new int[10];
  11.      int[] alpha = new int[20];
  12.      int[] beta = new int[20];
  13.      int[] gamma = {11,13,15,17};
  14.      int[] delta = {3,5,2,6,10,9,7,11,1,8};
  15.  copyGama(inStock,gamma);
  16. }
  17. public static void copyGama(int[][]a,int[]b)
  18.     {
  19.      int index,c,i;
  20.      System.out.printf("\nTry: ");
  21.      for(i = 0;i<a.length;i++)
  22.      {
  23.      for(c = 0;c<b.length;c++)
  24.      {
  25.          a[i][c] = b[c];
  26.          System.out.printf("%3d",a[i][c]);
  27.  
  28.      if(i==9){
  29.      System.out.print("\n");
  30.      }
  31.      }
  32.      }
  33. }
  34.  
  35.  
it makes my computer hang up because of its infinite loop..
Sep 11 '08 #1
3 1346
Nepomuk
3,112 Expert 2GB
It doesn't have an infinite loop. It's not very Java-like coding (do you come from a C background by any chance?), but it works. I get the following output:
Expand|Select|Wrap|Line Numbers
  1. Try:  11 13 15 17 11 13 15 17 11 13 15 17 11 13 15 17 11 13 15 17 11 13 15 17 11 13 15 17 11 13 15 17 11 13 15 17 11
  2.  13
  3.  15
  4.  17
There might be an infinite loop somewhere else, but this code works fine.

A few tips:
  1. You don't have to import java.lang, it's automatically imported.
  2. For such tasks, you normally don't use printf in Java, but just print.
  3. You don't have to declare your variables at the beginning of a method, indeed you should give them the smallest possible scope:
    Expand|Select|Wrap|Line Numbers
    1. for(int i = 0; i < a.length; i++)
    2. {
    3.     for(int c = 0; c < b.length; c++)
    4.     {
    5.         //...
    6.     }
    7. }
Greetings,
Nepomuk
Sep 11 '08 #2
It doesn't have an infinite loop. It's not very Java-like coding (do you come from a C background by any chance?), but it works. I get the following output:
Expand|Select|Wrap|Line Numbers
  1. Try:  11 13 15 17 11 13 15 17 11 13 15 17 11 13 15 17 11 13 15 17 11 13 15 17 11 13 15 17 11 13 15 17 11 13 15 17 11
  2.  13
  3.  15
  4.  17
There might be an infinite loop somewhere else, but this code works fine.

A few tips:
  1. You don't have to import java.lang, it's automatically imported.
  2. For such tasks, you normally don't use printf in Java, but just print.
  3. You don't have to declare your variables at the beginning of a method, indeed you should give them the smallest possible scope:
    Expand|Select|Wrap|Line Numbers
    1. for(int i = 0; i < a.length; i++)
    2. {
    3.     for(int c = 0; c < b.length; c++)
    4.     {
    5.         //...
    6.     }
    7. }
Greetings,
Nepomuk
tnx nepomuk!! i know get it..hehehe
but printf works on java.. :-)
Sep 11 '08 #3
Nepomuk
3,112 Expert 2GB
but printf works on java.. :-)
Well, it works (since Java 1.5), but it's a bit... well... the word "overkill" comes to mind. :-D

You may want to check the PrintStream Documentation (as System.out is a PrintStream), just to see what the best option may be. Java is traditionally more like C++ in this issue (and many others too) - instead of using something like
Expand|Select|Wrap|Line Numbers
  1. double pi = 3.14;
  2. System.out.printf("pi is approx. &d\n", pi);
you normally use something like this:
Expand|Select|Wrap|Line Numbers
  1. double pi = 3.14;
  2. System.out.print("pi is approx. " + pi + "\n");
or even
Expand|Select|Wrap|Line Numbers
  1. double pi = 3.14;
  2. System.out.println("pi is approx. " + pi);
I find the latter easier (especially when the printouts get long), but of course, as long as you're using JRE1.5 or newer, it's your choice entirely. ^^

Greetings,
Nepomuk
Sep 11 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
7
by: Andrej Hocevar | last post by:
Hello, what is the best way of solving cases where one has to wait (forever, until killed or instructed otherwise) for some event to occur? E.g., wait for data, monitoring a file? In fact, that...
1
by: SJ | last post by:
I'm developing a WAP client which presently works fine on most mobile phone browsers, but gives me an Infinite loop error (error 1025) when i try to access it from a couple of phones(motorola for...
34
by: NewToCPP | last post by:
Hi, Why does a C/C++ programs crash? When there is access to a null pointer or some thing like that programs crash, but why do they crash? Thanks.
33
by: dmoran21 | last post by:
Hi all, I am a mathematician and I'm trying to write a program to try out a formula that I've derived. However, it seems that I've got an infinite loop and I don't quite understand why. I was...
19
by: Richard | last post by:
Hi All, I copied a script example from http://www.irt.org/script/640.htm into a local .html file. I opened that file first in HTML-kit, which hung (in an infinite loop, I think) when I...
9
by: shailesh | last post by:
hi all, I m new joiny. I read that most of the compiler auto detect tail recursion in program. can any body tell me that is Code compoaser studio for TI dsp supports? rgds,
44
by: James Watt | last post by:
can anyone tell me how to do an infinite loop in C/C++, please ? this is not a homework question .
5
by: Tinku | last post by:
I am sorry for asking this silly question, but i don't understand why it is happening please suggest me ================================= #include <stdio.h> int main() { static int i=1;...
38
by: larry | last post by:
I am looking at some code which process an uploaded file and it has loops like: for(;;){ // for([two semicolons){ what is the function of the two semicolons? loop until break? I've...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.