473,480 Members | 1,777 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Arrays

2 New Member
Hi
how do i print array contents my code is as follows

import java.util.*;

public class Question1
{
private int[]numbers=new int[19];
private int option;
private int MAX=20;
private int num;
private int count;
public static void main(String[]args)
{
Question1 a= new Question1();
a.menu();
}
public void menu()
{

while(option!=7)
{
System.out.println(" Please select an option");
System.out.println("1. to store number");
System.out.println("2. to print ");
System.out.println("3. to search ");
System.out.println("4. to remove ");
System.out.println("5. to set ");
System.out.println("6. to calculate average ");
System.out.println("7. to quit ");
System.out.println(" option: ");
Scanner scan=new Scanner(System.in);
option=scan.nextInt();
if (option==7)
{
System.exit(0);
}else if (option==1)
{
enterNumber();
}

else if (option==2)
{
printNumbers();
}
//else if(option==3)
// {
// a.searchNumber();
// }
// else if(option==4)
// {
// // a.removeNumber();
// }else if (option==5)
// {
// // a.setNumber
// }else if(option==6)
// {
// // a.calcuate();
// }


}

}

public void enterNumber()
{
while (count!= MAX)
{
System.out.println("Enter intergers between 1 and 20 or (99 to quit)");
Scanner scan=new Scanner(System.in);
int num=scan.nextInt();
count++ ;
if (num==99)
{
System.out.println("Program quit");
System.exit(0);
}else if(numbers .length >MAX)
{
System.err.println("Too much data!!!");
System.exit(0);
}

}
}
// having trouble here
public void printNumbers()
{
for(int a =1;a<=MAX;a++)
{
if(num>0)
{
System.out.println (numbers[a +1]);
}
return 0;
}
}


// public void searchNumber()
//
// {
//
// System.out.println("Please enter a specified number ");
// Scanner scan= new Scanner(System.in);
// int num1=scan.nextInt();
//
// if (numbers[num1]== num1)
// {
// System.out.println("The number " + num1 + " is found ");
// return num1;
//
// }else
// System.out.println("The number " + num1 +" is not found");
// return -1;
// }
}
May 15 '08 #1
1 1164
JosAH
11,448 Recognized Expert MVP
You array indexing technique is goofy: you have an array with 19 elements (see
your own code). Array indexes start at zero (0) in Java so the valid index values
are 0, 1, 2, 3 ... 17, 18. Your code will cause an IndexOutOfBoundsException
at several places.

You can print the contents of an array in an extremely lazy way: check the
methods in the Arrays utility class. You don't even need to craft a loop
for it.

kind regards,

Jos
May 15 '08 #2

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

Similar topics

19
2815
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
5
29227
by: JezB | last post by:
What's the easiest way to concatenate arrays ? For example, I want a list of files that match one of 3 search patterns, so I need something like DirectoryInfo ld = new DirectoryInfo(searchDir);...
1
8680
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
41
4872
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
16
2502
by: mike3 | last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms- windows.programmer.win32 since there's Windows material in here as well as questions related to standard C++. Not sure how that'd go over...
0
7046
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
6908
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
7088
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...
1
6741
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...
0
6956
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5342
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4783
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2997
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.