Jump to content

Beginner Java Progrmming help..!

Go to solution Solved by rockking1379,

here you go

package tests;import java.util.*;public class koka {	public static void main (String args[])	{		Scanner CZ= new Scanner (System.in);			System.out.println("Name Car 1");		String First = CZ.nextLine();		System.out.println("Horsepower");		int Car1 = CZ.nextInt();				CZ.nextLine();				System.out.println("Name Car 2");		String Second = CZ.nextLine();		System.out.println("Horsepower");		int Car2 = CZ.nextInt();			if (Car1>Car2)		{			System.out.println(First +" Is powerful");		}		else		{			System.out.println(Second +" Is powerful");			}				CZ.close();	}}

change class name to suit your needs,

 

here is an article i used to understand what was happening.

http://stackoverflow.com/questions/8873351/reading-strings-next-and-nextline-java

look specifically at answer 1

 

also, use the code button

Someone tell me where is the problem?

1st it will ask to type the 1st car name the enter the horsepower .....then 2nd car & horsepower.....

The problem is I cannot name car no. 2 .....only Horsepower....

Tell me where is the mistake......or it is a bug?

 

 import java.util.*;public class Cars{public static void main (String args[]){ Scanner CZ= new Scanner (System.in); System.out.println("Name Car 1");String First = CZ.nextLine();System.out.println("Horsepower");int Car1 = CZ.nextInt(); System.out.println("Name Car 2");String Second = CZ.nextLine();System.out.println("Horsepower");int Car2 = CZ.nextInt(); if (Car1>Car2){System.out.println(Car1 +"Is powerful");}else{System.out.println(Car2 +"Is powerful");  } }} 
Edited by alpenwasser
code tags :)
Link to comment
https://linustechtips.com/topic/282551-beginner-java-progrmming-help/
Share on other sites

Link to post
Share on other sites

here you go

package tests;import java.util.*;public class koka {	public static void main (String args[])	{		Scanner CZ= new Scanner (System.in);			System.out.println("Name Car 1");		String First = CZ.nextLine();		System.out.println("Horsepower");		int Car1 = CZ.nextInt();				CZ.nextLine();				System.out.println("Name Car 2");		String Second = CZ.nextLine();		System.out.println("Horsepower");		int Car2 = CZ.nextInt();			if (Car1>Car2)		{			System.out.println(First +" Is powerful");		}		else		{			System.out.println(Second +" Is powerful");			}				CZ.close();	}}

change class name to suit your needs,

 

here is an article i used to understand what was happening.

http://stackoverflow.com/questions/8873351/reading-strings-next-and-nextline-java

look specifically at answer 1

 

also, use the code button

Link to post
Share on other sites

here you go

package tests;import java.util.*;public class koka {	public static void main (String args[])	{		Scanner CZ= new Scanner (System.in);			System.out.println("Name Car 1");		String First = CZ.nextLine();		System.out.println("Horsepower");		int Car1 = CZ.nextInt();				CZ.nextLine();				System.out.println("Name Car 2");		String Second = CZ.nextLine();		System.out.println("Horsepower");		int Car2 = CZ.nextInt();			if (Car1>Car2)		{			System.out.println(First +" Is powerful");		}		else		{			System.out.println(Second +" Is powerful");			}				CZ.close();	}}

change class name to suit your needs,

 

here is an article i used to understand what was happening.

http://stackoverflow.com/questions/8873351/reading-strings-next-and-nextline-java

look specifically at answer 1

 

also, use the code button

Thanks, it worked..!!

So where is the problem in my program?

Link to post
Share on other sites

here you go

package tests;import java.util.*;public class koka {	public static void main (String args[])	{		Scanner CZ= new Scanner (System.in);			System.out.println("Name Car 1");		String First = CZ.nextLine();		System.out.println("Horsepower");		int Car1 = CZ.nextInt();				CZ.nextLine();				System.out.println("Name Car 2");		String Second = CZ.nextLine();		System.out.println("Horsepower");		int Car2 = CZ.nextInt();			if (Car1>Car2)		{			System.out.println(First +" Is powerful");		}		else		{			System.out.println(Second +" Is powerful");			}				CZ.close();	}}

change class name to suit your needs,

 

here is an article i used to understand what was happening.

http://stackoverflow.com/questions/8873351/reading-strings-next-and-nextline-java

look specifically at answer 1

 

also, use the code button

lol, thats some crazy shit xD

Personal Build Project "Rained-On"

helped building up the CPU Overclocking Database and GPU Overclocking Database, check them out ;)

#KilledMyWife #MakeBombs #LinusIsNotFunny || Please, dont use non-default grey font colors. Think about the night-theme users! ;)

Link to post
Share on other sites

Thanks, it worked..!!

So where is the problem in my program?

but to boil it down, think of the input stream the scanner object is using as the line at a grocery store. so you go to get the first car name, and because you are reading a line it will read the entire input stream. including the new line "\n", which is used to denote enter. works great. but then the next value coming in was a number. so we mash in some numbers but still have to add that "\n" to the end of the stream to denote the end of the line. well when you read integers in it only takes the numbers out of the line. leaving the "\n" endline character still there. so then the next time you call "nextLine" well the new line character is actually still in the input stream. so it just processes immediately. so the workaround trick is to just call nextLine between nextInt and when you want to get the name of the next car

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×