10 Replies - 19762 Views - Last Post: 25 May 2011 - 08:27 AM Rate Topic: -----

#1 demoloution  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 14-November 10

problem with input.nextChar();

Posted 24 May 2011 - 07:06 AM

hello all

import java.util.Scanner;
public class Askisi14{
	public static void main(String args[]){
		int i;
		int voteA = 0;
		int voteB = 0;
		int voteC = 0;
		int white = 0;
		int winnerVotes;
		char winner;
		char vote;
		double percentage;
		Scanner input = new Scanner(System.in);
		for (i=0;i<1500;i++){
			vote = input.nextChar();
			if (vote == 'A')
				voteA++;
			else if (vote == 'B')
				voteB++;
			else if (vote == 'C')
				voteC++;
			else
				white++;
		}
		if (voteA>voteB){
			if (voteA>voteC){
				winner = 'A';
				winnerVotes = voteA;
			}
			else{
				winner = 'C';
				winnerVotes = voteC;
			}
		}
		else{
			if (voteB>voteC){
				winner = 'B';
				winnerVotes = voteB;
			}
			else{
				winner = 'C';
				winnerVotes = voteC;
			}
		}
		percentage = winnerVotes*100/1500;
		System.out.printf("The winner is %s with a percentage of %.2f",winner,percentage);
	}
}	



cannot find symbol
any suggest?

Is This A Good Question/Topic? 0
  • +

Replies To: problem with input.nextChar();

#2 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: problem with input.nextChar();

Posted 24 May 2011 - 07:14 AM

the Scanner class does not have a nextChar() method.
That's it :)
What will it be it's use ?
Was This Post Helpful? 0
  • +
  • -

#3 demoloution  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 14-November 10

Re: problem with input.nextChar();

Posted 24 May 2011 - 07:19 AM

hmmm...i see...
but i cant put input.next() or input.nextString i guess
so how can i read a single letter? :S
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: problem with input.nextChar();

Posted 24 May 2011 - 07:26 AM

Depends what you want to do.
If you want the use first char on each new line he enters:

char c = scanner.netLine().charAt(0);

if you want the all the user entered character on a line

char[] digit = scanner.nextLine().toCharArrray();
Was This Post Helpful? 1
  • +
  • -

#5 demoloution  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 14-November 10

Re: problem with input.nextChar();

Posted 24 May 2011 - 07:31 AM

thanks a lot sir
"If you want the use first char on each new line he enters:"
this line may be helpful on another program where i am stuck too :))
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: problem with input.nextChar();

Posted 24 May 2011 - 07:40 AM

Glad I could help :)
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7964
  • View blog
  • Posts: 13,584
  • Joined: 19-March 11

Re: problem with input.nextChar();

Posted 24 May 2011 - 07:48 AM

Quote

char c = scanner.netLine().charAt(0);



This may be obvious, but bear in mind that if you want to read character-by-character you'll have to capture the String and read it - if you use pbl's method, you've lost everything but the first character. This might not be what you want.

If you want the whole line, something like this might work nicely:
char[] line;
line = scanner.nextLiner().toArray(line);


Then you can either maintain an index manually or write a little method to do it for you
private char nextChar()
{
  return line[lineCounter++];
}

where lineCounter is an int at the class level which starts at 0 (and is reset when you read the next line, please don't forget that!)
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2816
  • View blog
  • Posts: 11,922
  • Joined: 20-September 08

Re: problem with input.nextChar();

Posted 24 May 2011 - 10:17 AM

Quote

for (i=0;i<1500;i++){


I don't know whether you intend the user to enter a 1500-character string or make 1500 entries, but the latter is going to take quite some time, so i would give them the opportunity to quit for sustenance or other calls of nature.
Was This Post Helpful? 1
  • +
  • -

#9 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: problem with input.nextChar();

Posted 25 May 2011 - 12:18 AM

View Postjon.kiparsky, on 24 May 2011 - 10:48 AM, said:

If you want the whole line, something like this might work nicely:
char[] line;
line = scanner.nextLiner().toArray(line);

??? This what I have posted 3 posts ahead

Quote

if you want the all the user entered character on a line

char[] digit = scanner.nextLine().toCharArrray();

And actually have never heard of the toArray() method of the class String :)
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10785
  • View blog
  • Posts: 40,160
  • Joined: 27-December 08

Re: problem with input.nextChar();

Posted 25 May 2011 - 07:18 AM

@jon.kiparsky: There is no Scanner nextLiner() or String toArray() method. It's Scanner nextLine() and String toCharArray(). Also, rather than converting the String to an array, using the String charAt() method would be more memory efficient. :)
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7964
  • View blog
  • Posts: 13,584
  • Joined: 19-March 11

Re: problem with input.nextChar();

Posted 25 May 2011 - 08:27 AM

Yeah, definitely a case of fingers engaged while brain was occupied on that post. I know my brain is around here somewhere, I'll try to get it hooked up again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1