Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

how to input into array from keyboard

java array

  • Please log in to reply
30 replies to this topic

#1 atoivan

atoivan

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 73 posts

Posted 03 December 2010 - 07:05 PM

i want to write a a program that will accept student name and grades and output it.use an array data structure in the implementation this program and implement the following

write method that will traverses the array and allow the user to perform the following processes

a) search for a particular student and his or her marks
b)find the average of all the student in the class
c)find all student with a mark below 50 and output them.

i started off with this codes

import java.util.Scanner;
//import java.lang.String;
public class Main {
public static int n;
//public static String names;
//private static String[] nameS;
//private static String[] nameS;
/**
* @param args the command line arguments
*/

public static void main(String[] args/*,String[] nameS*/) {
Scanner in=new Scanner(System.in);
int Da=0;
int num []=new int[n];
//int x=0;


System.out.printf("%s\n","Enter the Number of student taking This Couse");
n=in.nextInt();
String name[]=new String [n];
System.out.printf("%s %s \n", "index","value");
for (; Da < name.length; Da++ )/*Da 4 Display array*/ {
System.out.printf( "%5d%8d\n", Da, name[Da] );
}
System.out.println("Enter the names of the student");
String names;
names=in.nextLine();
name[n] = new String(names);
for (; Da < name.length; Da++ ){
System.out.printf( "%5d%8d\n", Da, name[Da] );
}




}
}

i can not input the names into the array( name) i created
pls help me

Edited by Alexander, 03 December 2010 - 10:51 PM.
Please use [code][/code] tags!

  • 0

#2 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 04 December 2010 - 02:35 AM

You should start like this:
 public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.printf("%s\n", "Enter the Number of student taking This Couse");
        n = in.nextInt();
        int num[] = new int[n];
        String name[] = new String[n];
You did
int num []=new int[n];
Before n had a value, so that could never work... What's the num array doing anyway?

Then you do (i put Da in the for-loop. You shouldn't let variabels start with a capitalised letter by the way.
System.out.printf("%s %s \n", "index", "value");
        for (int da =0; da < name.length; da++)
            System.out.printf("%5d%8[COLOR="red"][B]s[/B][/COLOR]\n", da, name[da]);
        }
This code is pretty stupid as the whole name array contains null. It's just created, it's all null. Not even empty Strings.

Then you need to input the names. You want to ask a name n times so create a loop that loops n times, then asks a name and put it in the name array.
for(int i=0 ; i<n ; i++){
  System.out.println("Enter the name of the student");
  names[i] = in.nextLine();
}

And then you can print it if you like:
for (int da=0; da < name.length; da++) {
      System.out.printf("%5d%8[COLOR="red"][B]s[/B][/COLOR]\n", da, name[da]);
}

Your code said "Enter the names of the student". Does that mean the user has to give all the names at once. Separated by a comma or so?
  • 0

#3 atoivan

atoivan

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 73 posts

Posted 04 December 2010 - 04:45 AM

the user must enter one name at a time
example when you enter the name "Ivan" the program will prompt u to enter the grade for that name
and u move to the next name and so on
get it?

the loop does not allow it to start the storing from the zeroth element;
it start from 1.
it must begin the storing from the zeroth element.
i even modified the loop to

for(int i=0 ; i<=n ; i++)
and it will not display the result
  • 0

#4 atoivan

atoivan

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 73 posts

Posted 04 December 2010 - 04:46 AM

and thanks any way u have been of great help
  • 0

#5 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 04 December 2010 - 04:49 AM

the loop does not allow it to start the storing from the zeroth element;
it start from 1.

I don't understand what you mean there.

for(int i=0 ; i<=n ; i++)

will crash with an IndexOutOfBoundsException


for(int i=0 ; i<n ; i++){
  System.out.println("Enter the name of the student");
  names[i] = in.nextLine();
  System.out.println("Enter the grade for " + names[i]);
  num[i] = in.nextInt();
}
will ask a student + its grade.
  • 0

#6 atoivan

atoivan

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 73 posts

Posted 04 December 2010 - 10:43 AM

what i am trying to say is the storing of the names in the array do not start from the zeroth element it start from index 1 instard of zero
  • 0

#7 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 05 December 2010 - 01:51 AM

Then do
for(int i=1 ; i<n ; i++)
If you want to store the same amount of names. Then create your array with a size of n+1.

What will go in the 0th element?
  • 0

#8 atoivan

atoivan

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 73 posts

Posted 05 December 2010 - 02:29 AM

thanks. now i wan to write a method that will traverse the Array
  • 0

#9 atoivan

atoivan

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 73 posts

Posted 05 December 2010 - 02:53 AM

public class pdemo {
public void trav()
{
for (int i=0;i<names[i].size();i++){
}
}
}


i tried the method above and the array nemes [i] is underlined what seem to be the problem
  • 0

#10 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 05 December 2010 - 02:54 AM

You ask the size of names[i] names[i] is a String. Strings have a length and not a size().
What you want is the size of the array, so jus tremove the [i]
  • 0

#11 atoivan

atoivan

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 73 posts

Posted 05 December 2010 - 03:11 AM

this is the new codes i have i modified the old one

import java.io.*;
public class Main {
public static int n;


/**
* @param args the command line arguments
*/

private static BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) );
public static void main(String[] args)
throws IOException

{
System.out.printf("%s\n","Welcome Enter the Number of student taking This Couse");
String input = stdin.readLine();
int nameCount = Integer.parseInt(input);
String[] names = new String[nameCount];
for(int i=0; i < nameCount; i++)
{
System.out.print("Enter name: ");
names[i] = stdin.readLine();
}
System.out.println("Enter the grade ");
for(int i=0; i<nameCount ; i++)
{
input = stdin.readLine();
}
for (int da=0; da < names.length; da++)
{
System.out.printf("%5d%8s%8s\n", da, names[da],input);
}


and did a simple method that traverses the array
this the traverse code
public class pdemo {
public void trav()
{
for (int i=0;i<names.size;i++);
}
}


the traverse will not work i do not know why is keeps highlighting the (names ) array
pls correct it for me
  • 0

#12 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 05 December 2010 - 03:21 AM

Ah, names doesn't exist in the pdemo class.
A variable only exists between the { } it is created in.
private void method()[COLOR="red"][B]{[/B][/COLOR]
    int number = 2;
[B][COLOR="red"]}  'number' dies here[/COLOR][/B]  

public void otherMethod(){
     if(true)[COLOR="red"][B]{[/B][/COLOR]
        int number = 5;
     [COLOR="red"][B]} number dies here[/B][/COLOR]
     number = 0; [COLOR="red"][B]--> FAILS[/B][/COLOR]
}

Change it like:
public static void main(String[] args) throws IOException {
    Pdemo demo = new Pdemo();
    demo.inputNames();
    demo.inputGrades();
    demo.trav();
}

public class Pdemo {
    private String[] names;
    private int nameCount;
    private BufferedReader stdin;

    public Pdemo(){
        stdin = new BufferedReader( new InputStreamReader( System.in ) );
        System.out.printf("%s\n","Welcome Enter the Number of student taking This Couse");
        String input = stdin.readLine();
        int nameCount = Integer.parseInt(input);
        String[] names = new String[nameCount];
    }

    public void inputNames(){
        for(int i=0; i < nameCount; i++) {
            System.out.print("Enter name: ");
            names[i] = stdin.readLine();
        }
    }

    public void inputGrades(){
        for(int i=0; i<nameCount ; i++) {
            input = stdin.readLine();
        }  
    }

    public void trav() {
        for (int i=0;i<names.size;i++);
    }
}

  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download