Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 9th, 2013, 05:52 AM
souvik145 souvik145 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 3 souvik145 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 44 sec
Reputation Power: 0
Angry Whats Wrong With My Code???

Code:
import java.util.Scanner;

public class Main
{
public static void main(String args[])
{
Scanner S=new Scanner(System.in);

System.out.println("Enter the first integer ");

int i=S.nextInt();

System.out.println("Enter the second integer "); 

int j=S.nextInt(); 

System.out.println("Enter the first character ");

char ch1=S.next().charAt(0);

System.out.println("Enter the second character ");

char ch2=S.next().charAt(0); 

System.out.println("Enter the first string "); 

String str1=S.nextLine(); 

System.out.println("Enter the second string"); 

String str2=S.nextLine();

OverloadCompare object=new OverloadCompare(); 

object.Compare(i, j);

object.Compare(ch1, ch2);

object.Compare(str1, str2);

S.close();
}
}



When I execute the program, it asks for first, second integers and characters, but the first string asking is not asked, its just printed but I don't get to input it, only the second string can be inputed. Whats wrong ??

Reply With Quote
  #2  
Old March 9th, 2013, 07:48 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
You are having a problem with the Scanner class. When nextInt() is called and the Enter key is pressed, a newline character is left in the Scanner's buffer. The next call to nextLine() returns that newline character. To see print out what is read by the call to nextLine() following a call to nextInt().
A fix is to call the nextLine() method after calling nextInt() to clear the newline character from the Scanner's buffer

Reply With Quote
  #3  
Old March 14th, 2013, 09:46 PM
souvik145 souvik145 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 3 souvik145 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 44 sec
Reputation Power: 0
Quote:
Originally Posted by NormR
You are having a problem with the Scanner class. When nextInt() is called and the Enter key is pressed, a newline character is left in the Scanner's buffer. The next call to nextLine() returns that newline character. To see print out what is read by the call to nextLine() following a call to nextInt().
A fix is to call the nextLine() method after calling nextInt() to clear the newline character from the Scanner's buffer



thank you so much d00d. cheers

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Whats Wrong With My Code???

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap