Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,423 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,344 people online right now. Registration is fast and FREE... Join Now!




java scanner

 
Reply to this topicStart new topic

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

manggirie
14 Oct, 2008 - 08:01 PM
Post #1

New D.I.C Head
*

Joined: 14 Oct, 2008
Posts: 3

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");
}
}
User is offlineProfile CardPM
+Quote Post

pbl
RE: Java Scanner
14 Oct, 2008 - 08:20 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
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

CODE


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
}



User is offlineProfile CardPM
+Quote Post

manggirie
RE: Java Scanner
14 Oct, 2008 - 10:18 PM
Post #3

New D.I.C Head
*

Joined: 14 Oct, 2008
Posts: 3

QUOTE(pbl @ 14 Oct, 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

CODE


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

User is offlineProfile CardPM
+Quote Post

pbl
RE: Java Scanner
15 Oct, 2008 - 02:02 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
String str;
str = sc.next(); // will contain "Edward"
str = sc.next(); // will contain "20"
User is offlineProfile CardPM
+Quote Post

manggirie
RE: Java Scanner
15 Oct, 2008 - 04:43 PM
Post #5

New D.I.C Head
*

Joined: 14 Oct, 2008
Posts: 3

QUOTE(pbl @ 15 Oct, 2008 - 03:02 PM) *

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: 2
User is offlineProfile CardPM
+Quote Post

NeoTifa
RE: Java Scanner
16 Oct, 2008 - 04:59 AM
Post #6

oohhhh, i get one nao!!!
****

Joined: 24 Sep, 2008
Posts: 968



Thanked: 4 times
My Contributions
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. smile.gif hope this helped a bit happy.gif

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 01:13PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month