java scanner

how can i use java scanner to differentiate string and integer when us

Page 1 of 1

5 Replies - 2675 Views - Last Post: 16 October 2008 - 05:59 AM Rate Topic: -----

#1 manggirie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-October 08

java scanner

Post icon  Posted 14 October 2008 - 09:01 PM

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");
}
}

Is This A Good Question/Topic? 0
  • +

Replies To: java scanner

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

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


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
}


Was This Post Helpful? 0
  • +
  • -

#3 manggirie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-October 08

Re: java scanner

Posted 14 October 2008 - 11:18 PM

View Postpbl, 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


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
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: java scanner

Posted 15 October 2008 - 03:02 PM

String str;
str = sc.next(); // will contain "Edward"
str = sc.next(); // will contain "20"
Was This Post Helpful? 0
  • +
  • -

#5 manggirie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-October 08

Re: java scanner

Posted 15 October 2008 - 05:43 PM

View Postpbl, on 15 Oct, 2008 - 03:02 PM, said:

String str;
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)

  • Attached File  3.doc (24K)
    Number of downloads: 61

Was This Post Helpful? 0
  • +
  • -

#6 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2495
  • View blog
  • Posts: 15,451
  • Joined: 24-September 08

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 ^_^
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1