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

#13 atoivan

atoivan

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 73 posts

Posted 05 December 2010 - 04:30 AM

do u want me to change the old code to yours

ur code are giveing alot ok error form the IO
  • 0

#14 wim DC

wim DC

    Roar

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

Posted 05 December 2010 - 04:38 AM

Apart from missing import statements and throw statements it should be ok (and 3 other things :P). Here's the whole working thing:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

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

    public Pdemo() throws IOException {
        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);
        names = new String[nameCount];
    }

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

    public void inputGrades() throws IOException {
        for (int i = 0; i < names.length; i++) {
            String input = stdin.readLine();
        }
    }

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

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

  • 0

#15 atoivan

atoivan

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 73 posts

Posted 05 December 2010 - 03:13 PM

public class pdemo {
private String[] names;
private int nameCount; ---------> not an array tried to make it an array
private String inp;
private BufferedReader stdin;
Scanner on = new Scanner(System.in);
public static final int NOT_FOUND = -1;

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

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

public void inputGrades() throws IOException {

for(int i=0; i<nameCount ; i++)
{System.out.println("Enter the grade ");
inp = on.nextLine();
}
}
public void trav()throws IOException {
for (int da=0;da<names.length;da++)

{
//for(int oi=0;oi<inp.length();oi++)
System.out.printf("%d%5s%5s\n", da, names[da],inp);
}
}
public void sele(int i)throws IOException
{char a=0,b,c,x;
System.out.println("CHOSE FORM THE LIST OF OPERATION");
String in = stdin.readLine();
if(a==a){
System.out.println("What is the name of the student u are looking for?");
names[i] = stdin.readLine();
System.out.println(names);




}


}
}

  • 0

#16 atoivan

atoivan

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 73 posts

Posted 05 December 2010 - 03:15 PM

how can i make it an array.

to store the grades
  • 0

#17 atoivan

atoivan

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 73 posts

Posted 05 December 2010 - 11:49 PM

let forget about the not an array
i have Declared the grade array now inputing into the array is the problem now ;
these are the new codes i modified from yours,pls correct the grade array input for me.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class pdemo {
private String[] names;
private double[] grade;
public int Nos;
private String num;
private BufferedReader stdin;
Scanner on = new Scanner(System.in);
public static final int NOT_FOUND = -1;

public pdemo() throws IOException {
stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.printf("%s\n", "Welcome Enter the Number of student taking This Couse");
num = stdin.readLine();
Nos = Integer.parseInt(num);
names = new String[Nos];
grade=new double[Nos]; ----------> i think problem from there;
}

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

public void inputGrades() throws IOException {

for(int i=0; i<grade.length ; i++)
{System.out.println("Enter the grade ");
grade[i] =Double.parseDouble(num);---------------------------> i think promble from here too;
}
}
public void trav()throws IOException {
for (int da=0;da<names.length;da++)

{
//for(int oi=0;oi<inp.length();oi++)
System.out.printf("%d%5s%5s\n", da, names[da],grade);
}
}

  • 0

#18 wim DC

wim DC

    Roar

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

Posted 05 December 2010 - 11:56 PM

in the inputGrades method you aren't reading anything. You just ask the question and then put the number of students as grade.
You need to read the grade as you did when you asked the number of students.
num = stdin.readLine();
Nos = Integer.parseInt(num);

I also noticed you added a Scanner. You either use a scanner OR you use a bufferedreader. Don't use both.
I would prefer the scanner because it has methods like nextInt(), nextDouble(), where the bufferedreader just reads a string and you need to parse it yourself.
  • 0

#19 atoivan

atoivan

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 73 posts

Posted 06 December 2010 - 12:03 AM

but it is grade i am inputing not num or Nos
  • 0

#20 atoivan

atoivan

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 73 posts

Posted 06 December 2010 - 12:21 AM

let forget about the last question.i used Scanner and it worked just fine, thanks
  • 0

#21 atoivan

atoivan

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 73 posts

Posted 06 December 2010 - 02:19 AM

i have created some new methods that will select form a list of options , search ,find average and find all names with grade below 50

1)i am lost with the search algorithm and how to out put the result.
2)how to output the average and the grade below 50

here are the codes the ones in red i am having problems with.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class pdemo {
private String[] names;
private double[] grade;
public int Nos;
private String num;
public char a=0,b=0,c=0,x=0;
private BufferedReader stdin;
Scanner on = new Scanner(System.in);
public static final int NOT_FOUND = -1;

public pdemo() throws IOException {
stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.printf("%s\n", "Welcome Enter the Number of student taking This Couse");
num = stdin.readLine();
Nos = Integer.parseInt(num);
names = new String[Nos];
grade=new double[Nos];
}

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

public void inputGrades() throws IOException {

for(int i=0; i<grade.length ; i++)
{System.out.println("Enter the grade ");
// grade[i] =Double.parseDouble(num);
grade[i]=on.nextDouble();
}
}
public void trav()throws IOException {
for (int da=0;da<names.length;da++) {
System.out.println( da+ ":"+ names[da]+" :"+grade[da]);

}
}


public void sele()throws IOException
{
System.out.println("CHOSE FORM THE LIST OF OPERATION");
System.out.printf("%s\n %s\n %s\n ","a = Search for a particular Student",
"b=Find the average of all Student",
"c= Find all student with the marks blow 50");
// for(int i=0; i < num;i++)

String in = stdin.readLine();
}
public void search(String[] names,double [] grade,String num,int i) throws IOException
{
if(a==a){
System.out.println("What is the name of the student u are looking for?");

names[i] = stdin.readLine();}
{
int low = 0;
int high = names.length - 1;
int mid;

while( low <= high )
{
mid = ( low + high ) / 2;

if(names[ mid ].compareTo( num)< 0 )
low = mid + 1;
else if( names[ mid ].compareTo( num) > 0 )
high = mid - 1;
else
return mid;
}
return NOT_FOUND; // NOT_FOUND = -1
}

System.out.println(names[i]+""+grade[i]);

}


public void average(String [] names, double [] grade,int num)
{
if (b==B){
double ave=0;
for(int i=0; i < num;i++)
{
ave+=grade[i];
}
System.out.println(ave);

}
}


public void below(String [] names, double [] grade,int num)
{

{
for(int i=0; i < num;i++)
{
if(grade[i] < 50)
{
System.out.println(names[i]+grade[i]);

}
}
}

}
}

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
{
pdemo demo = new pdemo();
demo.inputNames();
demo.inputGrades();
demo.trav();
demo.sele();
demo.search(args, grade, null, n);
demo.average(args, grade, null, n);
demo.below(args, grade, null, n);


pls fix them for me

i want the condition for if a vaule is even dosome thing
example if(ans <=9)
ans/2
thank you
  • 0

#22 atoivan

atoivan

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 73 posts

Posted 06 December 2010 - 02:22 AM

i have created some new methods that will select form a list of options , search ,find average and find all names with grade below 50

1)i am lost with the search algorithm and how to out put the result.
2)how to output the average and the grade below 50

here are the codes the ones in red i am having problems with.
[COLOR="RoyalBlue"]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class pdemo {
private String[] names;
private double[] grade;
public int Nos;
private String num;
public char a=0,b=0,c=0,x=0;
private BufferedReader stdin;
Scanner on = new Scanner(System.in);
public static final int NOT_FOUND = -1;

public pdemo() throws IOException {
stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.printf("%s\n", "Welcome Enter the Number of student taking This Couse");
num = stdin.readLine();
Nos = Integer.parseInt(num);
names = new String[Nos];
grade=new double[Nos];
}

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

public void inputGrades() throws IOException {

for(int i=0; i<grade.length ; i++)
{System.out.println("Enter the grade ");
// grade[i] =Double.parseDouble(num);
grade[i]=on.nextDouble();
}
}
public void trav()throws IOException {
for (int da=0;da<names.length;da++) {
System.out.println( da+ ":"+ names[da]+" :"+grade[da]);

}
}


public void sele()throws IOException
{
System.out.println("CHOSE FORM THE LIST OF OPERATION");
System.out.printf("%s\n %s\n %s\n ","a = Search for a particular Student",
"b=Find the average of all Student",
"c= Find all student with the marks blow 50");
// for(int i=0; i < num;i++)

String in = stdin.readLine();
}[/COLOR]
public void search(String[] names,double [] grade,String num,int i) throws IOException
{
if(a==a){
System.out.println("What is the name of the student u are looking for?");

names[i] = stdin.readLine();}
{
int low = 0;
int high = names.length - 1;
int mid;

while( low <= high )
{
mid = ( low + high ) / 2;

if(names[ mid ].compareTo( num)< 0 )
low = mid + 1;
else if( names[ mid ].compareTo( num) > 0 )
high = mid - 1;
else
return mid;
}
return NOT_FOUND; // NOT_FOUND = -1
}

System.out.println(names[i]+""+grade[i]);

}

public void average(String [] names, double [] grade,int num)
{
if (b==b){
double ave=0;
for(int i=0; i < num;i++)
{
ave+=grade[i];
}
System.out.println(ave);

}
}

public void below(String [] names, double [] grade,int num)
{

{
for(int i=0; i < num;i++)
{
if(grade[i] < 50)
{
System.out.println(names[i]+grade[i]);
}
}
}

}
}


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
{
pdemo demo = new pdemo();
demo.inputNames();
demo.inputGrades();
demo.trav();
demo.sele()
;
[COLOR="Red"]demo.search(args, grade, null, n);
demo.average(args, grade, null, n);
demo.below(args, grade, null, n);[/COLOR]

pls fix them for me

i want the condition for if a vaule is even dosome thing
example if(ans <=9)
ans/2
thank you
  • 0

#23 wim DC

wim DC

    Roar

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

Posted 06 December 2010 - 09:07 AM

What are the methods search and below supposed to be doing?
And what's with the silly if-conditions?
if(a==a) is useless, it's always true.
  • 0

#24 atoivan

atoivan

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 73 posts

Posted 06 December 2010 - 12:01 PM

the method Search is search through the array for a particular names inputed by the user.

and the below method is to output all names with the grade below 50
  • 0





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