1

I am a new learner of Java language and for the past two years I have been working with C++. I am gonna need something equivalent to cin.ignore() in Java.

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
Saim Mehmood
  • 783
  • 8
  • 14
  • 1
    Can you add what it is you need it to do in Java for those not familiar with that method? – Peter Lawrey Nov 16 '14 at 11:16
  • I am working on a Dictionary and there is a check I need to ensure. Well the input gets passed over it every time. No idea why! – Saim Mehmood Nov 16 '14 at 11:34
  • In that case, I suggest you read all the input and debug your program to see why it is reading what it is doing. Trying to hide a problem you don't understand has a habit being a disaster. – Peter Lawrey Nov 16 '14 at 11:38
  • okay thank you Peter! Well I was debugging my program. Its particularly an issue with String input using Scanner class. No idea why it doesn't check "yes" or "no" input? – Saim Mehmood Nov 16 '14 at 11:49
  • Oh! just find out I wasn't using .equals method. :) – Saim Mehmood Nov 16 '14 at 11:52
  • Are you reading "yes" and "no" and using == to compare Strings, because that won't do what you think in Java. You need to use `str.equals("yes")` to find out if two strings have the same contents. – Peter Lawrey Nov 16 '14 at 11:53
  • That's because `String s` is a *reference* in Java. Not the object. When you use `==` to are asking if these are references to the same object, `equals` checks if the contents are equivalent. – Peter Lawrey Nov 16 '14 at 11:54

2 Answers2

4

In java equivalent of cin.ignore() is InputStream.skip(). You can refer to Java Docs

er_suthar
  • 319
  • 1
  • 8
0

Normally in Java you would read the text and remove the bits you don't want. e.g. instead of doing this.

first = std::cin.get();     // get one character
std::cin.ignore(256,' ');   // ignore until space

last = std::cin.get();      // get one character

std::cout << "Your initials are " << first << last << '\n';

you would do

Scanner in = new Scanner(System.in);
String first = in.next(), last = in.next();

System.out.println("Your initials are " + first.charAt(0)+last.charAt(0)+"\n");
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130