java - writing a program to obtain various data values from user and displaying it in different formats. -


i newbie in java , have written program display flight details

 import java.io.*;  import java.util.scanner;  class main { public static void main(string args[]) {     int ticket;     string name,destination;     float fare;     char tclass;     scanner in = new scanner(system.in);     system.out.println("enter flight name : ");     name=in.nextline();     system.out.println("enter ticket no : ");     ticket=in.nextint();     system.out.println("enter flight fare : ");     fare=in.nextfloat();     system.out.println("enter travelling class : ");     tclass=in.next().charat(0);     system.out.println("enter source : ");      string source=in.nextline();     system.out.println("enter destination : ");     destination=in.nextline();     system.out.println("flight details : ");     system.out.println("flight name : "+name);     system.out.println("ticket no : "+ticket);     system.out.println("flight fare : "+fare);     system.out.println("class : "+tclass);     system.out.println("source : "+source);     system.out.println("destination : "+destination); } 

}

but getting error while printing output. source field empty , destination field has data of source

flight details :  flight name : emirates ticket no : 43190215 flight fare : 19433.94 class : c source :  destination : mumbai 

here mumbai value source getting displayed in destination field

use piece of code in programm,

    system.out.println("enter travelling class : ");     tclass = in.next().charat(0);     in.nextline();     system.out.println("enter source : ");     string source = in.nextline(); 

actually next() method not consuming last newline character, that's why facing problem.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -