Adding numbers to an array

Want to add a string of numbers to an array

Page 1 of 1

1 Replies - 7013 Views - Last Post: 01 October 2009 - 10:11 PM Rate Topic: -----

#1 blackopts  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-October 09

Adding numbers to an array

Post icon  Posted 01 October 2009 - 10:09 PM

After I prompt the user for a binary number how do I accept it into an array all at once and put each number into each cell?

The code is working but only if I take each number one at a time which isnt good.

Thanks in advance


import javax.swing.*;

public class Hamming
{
	public static void main(String[] args)
	{
		int i, m;
		int counter = 1;

	   m = Integer.parseInt(JOptionPane.showInputDialog("Enter number of data bits (positive)"));
		int[] number = new int[m];		
		
		for(i = 0; i < number.length; i++)
		{
			number[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter bit string of length " 
			+m +" entering each number one at a time: #" +counter));
			counter++;
		}
				
		System.exit(0);
	}
}

This post has been edited by blackopts: 01 October 2009 - 10:18 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Adding numbers to an array

#2 javafreak  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 97
  • Joined: 21-September 09

Re: Adding numbers to an array

Posted 01 October 2009 - 10:11 PM

code help you in array logic
 
	import java.util.*;
	import java.io.*;
	public class marks_avg
	{
	public static void main(String ar[])
	{	
	try
	{
	  BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
	System.out.println("enter the total no of students");
	int total=Integer.parseInt(input.readLine());
	int avg=0;
	String s;int[] marks = new int[total];
	int sum=0;
	for(int i = 0;i < marks.length;i++)
	{
	System.out.println("enter the Student name");
	s=input.readLine();
	System.out.println("enter the marks  must be numeric press -1 to exit");
	marks[i]=Integer.parseInt(input.readLine());
	sum= marks[i]+sum;
	if(marks[i]==-1)
	{
	break;
	}
	}
	System.out.println("the sum is "+sum);
	avg=sum/total;
	System.out.println("the avg is "+avg);
	}
	   catch (Exception e) {
	 	e.printStackTrace();
   	}
	}
	}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1