input
Sample input:
Number of students: 4
Enter students’ name and marks:
Ali 45
Mamat 30
Suzi 56
Suzila 12
output:
Sample output:
Average marks = 35.65
Marks Above Average
===================
Ali (45)
Suzi (56)
Average Marks and Below
=======================
Mamat (30)
Suzila (12)
------------------coding----------------
import java.util.*;
class StudentMarks
{
private static Scanner sc = new Scanner(System.in);
public static void main (String[] args)
{
String[] student;
int[] marks;
int i, count;
int total = 0;
float avg = 0;
System.out.print("Number of Students: ");
count = sc.nextInt();
System.out.println();
System.out.println("Enter Student's name and marks: ");
for (i = 0; i < count; i++){
sc.next();
sc.nextInt();
}
System.out.println("average mark" + avg);
System.out.println();
System.out.println("List students with marks above the average");
System.out.println();
System.out.println("List students with average marks or below");
}
}
java scannerhow can i use java scanner to differentiate string and integer when us
Page 1 of 1
5 Replies - 2437 Views - Last Post: 16 October 2008 - 05:59 AM
Replies To: java scanner
#2
Re: java scanner
Posted 14 October 2008 - 09:20 PM
Question not very clear... if I guess from your topic title:
you can always always read a String from the scanner and then try to convert it to int
you can always always read a String from the scanner and then try to convert it to int
boolean isNumber;
int nb = 0;
// read String
String str = sc.next();
// try to convert it to int
try {
nb = Integer.parseInt(str);
isNumber = true;
}
catch(NumberFormatException e) {
isNumber = false;
}
// test if conversion worked
if(isNumber) {
... it is a number
}
else {
... was other text
}
#3
Re: java scanner
Posted 14 October 2008 - 11:18 PM
pbl, on 14 Oct, 2008 - 09:20 PM, said:
Question not very clear... if I guess from your topic title:
you can always always read a String from the scanner and then try to convert it to int
you can always always read a String from the scanner and then try to convert it to int
boolean isNumber;
int nb = 0;
// read String
String str = sc.next();
// try to convert it to int
try {
nb = Integer.parseInt(str);
isNumber = true;
}
catch(NumberFormatException e) {
isNumber = false;
}
// test if conversion worked
if(isNumber) {
... it is a number
}
else {
... was other text
}
actually my question is.. how can i use 'java scanner' to differentiate integer and string when its all together been insert at one time.. for example..
System.out.println("Enter Student's name and marks: ");
so we must enter " edward 20"-----> 'edward' for name ----> '20' for marks
so how can i use java scanner to differentiate 'edward 20 ' whether 'edward' or '20' are string in order to put 'edward' into 'arrayname' and 20 into 'array marks'?
*arraymarks is integer array
*arrayname is string array
#4
Re: java scanner
Posted 15 October 2008 - 03:02 PM
String str;
str = sc.next(); // will contain "Edward"
str = sc.next(); // will contain "20"
str = sc.next(); // will contain "Edward"
str = sc.next(); // will contain "20"
#5
Re: java scanner
Posted 15 October 2008 - 05:43 PM
pbl, on 15 Oct, 2008 - 03:02 PM, said:
String str;
str = sc.next(); // will contain "Edward"
str = sc.next(); // will contain "20"
str = sc.next(); // will contain "Edward"
str = sc.next(); // will contain "20"
how to pass value 'edward' into arrayname?
how to pass value '20' into arraymarks?
in the end i must print out the list of student with mark above average and below average?
Attached File(s)
-
3.doc (24K)
Number of downloads: 50
#6
Re: java scanner
Posted 16 October 2008 - 05:59 AM
you could make a reference to the name and assign it to the arrayname, and same with the mark. as for the average, so something like
if (mark1 > average)
s.o.println("student is above average");
else if (mark1 < average)
s.o.println("student is below average");
esle if (mark1 == average)
s.o.println("the student has average grade");
or something like it.
hope this helped a bit
if (mark1 > average)
s.o.println("student is above average");
else if (mark1 < average)
s.o.println("student is below average");
esle if (mark1 == average)
s.o.println("the student has average grade");
or something like it.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote







|