5 Replies - 256 Views - Last Post: 23 June 2013 - 12:27 PM Rate Topic: -----

#1 SomethingAwesome  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 23-June 13

for-loop executes println statements first.

Posted 23 June 2013 - 11:09 AM

My program is being used to enter the names of 12 countries and their corresponding populations with two separate arrays. Then display the contents of the arrays (I know fairly simple). Yet, when the for loop executes after the first iteration, the loop only accepts integer values for the array "population". And it will display both print statements, skipping lines 15 and 16.
It would be great if I could be explained the logic behind the code rather than "spoon feeding" me an answer. Thank you. :bigsmile:/>/>
By the way, you can ignore line 10.

import java.util.Scanner;
/**P. 532*/	
public class Algorithm4
{
	public static void main(String[] args)
	{
	String[] country = new String[12];
	int[] population = new int[12];
	Scanner keyboard = new Scanner(System.in);
	//int count = 0;//Used as a counter when entering population.
	
	for(int i = 0; i < population.length; i++)
	{
		System.out.println("Enter the name of a country: ");
		String text = keyboard.nextLine();
		country[i]= text;
		System.out.println("Enter the population of " + text + ":");
		population[i] = keyboard.nextInt();
	}
	
	}
}



Is This A Good Question/Topic? 1
  • +

Replies To: for-loop executes println statements first.

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,073
  • Joined: 05-April 11

Re: for-loop executes println statements first.

Posted 23 June 2013 - 11:29 AM

It doesn't skip the lines, they are still being executed :D

The problem you are having is a common one when using the Scanner. The Scanner works as expected, but I dislike the use of it because of this one thing....
It is just too confusing to use for beginners

Think about what you are writing to the stream and what is being read
Hitting the return key might be writing a \n or \r\n to the stream depending on your system and/or editor

A call to nextLine() will search for the next line separator and consume it
nextInt() will search for the next integer, but does not consume any line separator

You might input something like the following:
Denmark\n5000000\n


Was This Post Helpful? 2
  • +
  • -

#3 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 136
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: for-loop executes println statements first.

Posted 23 June 2013 - 12:10 PM

View PostCasiOo, on 23 June 2013 - 06:29 PM, said:

It doesn't skip the lines, they are still being executed :D/>

The problem you are having is a common one when using the Scanner. The Scanner works as expected, but I dislike the use of it because of this one thing....
It is just too confusing to use for beginners

Think about what you are writing to the stream and what is being read
Hitting the return key might be writing a \n or \r\n to the stream depending on your system and/or editor

A call to nextLine() will search for the next line separator and consume it
nextInt() will search for the next integer, but does not consume any line separator

You might input something like the following:
Denmark\n5000000\n



This.

A good way to get around this is to continue using Scanner's nextLine(). Pretend that you don't want an integer currently. If you need it in an integer you can parse it from the string.

Just a little example snippet.

for(int i = 0; i < 5; i++) {
    System.out.println("Enter string:");
    string1[i] = scanner.nextLine();
    System.out.println("Enter string2:");
    try {
        string2[i] = scanner.nextLine();
        numberFromString[i] = Integer.parseInt(string2[i]);
    catch (NumberFormatException e) {
        System.out.println("Not a valid number");
    }
}



This of course won't reloop it if it's an invalid number, I was just showing an example of what could be done. :)
Was This Post Helpful? 0
  • +
  • -

#4 SomethingAwesome  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 23-June 13

Re: for-loop executes println statements first.

Posted 23 June 2013 - 12:14 PM

I found out that pressing the Enter key will store a new line character in the keyboard buffer. :detective:

The nextLine method will consume the new line character where as methods with primitive data types won't. So I added keyboard.nextLine(); on line 19 to consume the remaining character. It works. Thank you.
Was This Post Helpful? 0
  • +
  • -

#5 SomethingAwesome  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 23-June 13

Re: for-loop executes println statements first.

Posted 23 June 2013 - 12:24 PM

View Postschutzzz, on 23 June 2013 - 12:10 PM, said:

View PostCasiOo, on 23 June 2013 - 06:29 PM, said:

It doesn't skip the lines, they are still being executed :D/>/>

The problem you are having is a common one when using the Scanner. The Scanner works as expected, but I dislike the use of it because of this one thing....
It is just too confusing to use for beginners

Think about what you are writing to the stream and what is being read
Hitting the return key might be writing a \n or \r\n to the stream depending on your system and/or editor

A call to nextLine() will search for the next line separator and consume it
nextInt() will search for the next integer, but does not consume any line separator

You might input something like the following:
Denmark\n5000000\n



This.

A good way to get around this is to continue using Scanner's nextLine(). Pretend that you don't want an integer currently. If you need it in an integer you can parse it from the string.

Just a little example snippet.

for(int i = 0; i < 5; i++) {
    System.out.println("Enter string:");
    string1[i] = scanner.nextLine();
    System.out.println("Enter string2:");
    try {
        string2[i] = scanner.nextLine();
        numberFromString[i] = Integer.parseInt(string2[i]);
    catch (NumberFormatException e) {
        System.out.println("Not a valid number");
    }
}



This of course won't reloop it if it's an invalid number, I was just showing an example of what could be done. :)/>


I'll try and understand your code, it looks interesting. I haven't learned exception handling yet. Although, this fall semester I will be learning it.
Was This Post Helpful? 0
  • +
  • -

#6 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 136
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: for-loop executes println statements first.

Posted 23 June 2013 - 12:27 PM

Well with your code as an Integer if someone inputs a String in that field accidentally, it's going to create a NumberFormatException. This will try to parse that string to an integer and if it's not a number, then it will ignore it and crash the program.

edit: Fixed typo

and if you do want it to loop, in the catch statement, require the input again.

also, if you are doing population of larger scale countries, use a long, not an integer. Values of Data Types
- Scratch that, I wasn't thinking correctly when I was thinking of population of countries

This post has been edited by schutzzz: 23 June 2013 - 01:00 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1