Results 1 to 4 of 4
  1. #1
    lowziqian is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default how to input string and integers into 2 diff arrays at a time?

    Below is my code, im trying to change the code such that the array num[] will store integers, and the 2nd input within the code will store Strings to the array num1[].
    Do i have to keep declaring the Scanner and input class?
    eg. Scanner in =new Scanner(System.in);
    [code for integer input]
    Scanner scan=new Scanner(System.scan);
    [code for string put]

    Is this possible using a while loop? Because i do not know how many times i will be looping as well.


    package testing;

    import java.util.Scanner;

    public class NewClass1 {

    public static void main(String[]args)
    {
    Scanner in =new Scanner(System.in);
    int[]num=new int[2];
    int []num1=new int[2];
    for (int i=0;i<2;i++)
    {
    System.out.println("Enter a number");
    num[i]=in.nextInt();

    System.out.println("Enter another number");// I am trying change this part into entering a string
    num1[i]=in.nextInt();

    }


    for (int i=0;i<2;i++)
    { System.out.println("The price of the items is "+num[i]+" and "+num1[i]);

    }
    }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: how to input string and integers into 2 diff arrays at a time?

    No, you will only ever need one Scanner object. How you use it will depend upon how the data is formatted. Is the data in a file with an int and a String (or String and int) on each line. Or int and String each on their own line. Or....

  3. #3
    lowziqian is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: how to input string and integers into 2 diff arrays at a time?

    This is what im trying to achieve, but it appears to only ask me for the "number"//first println
    is there anything wrong with my code?



    Scanner scan =new Scanner(System.in);

    int[]num=new int[2];
    String []num1=new String[2];
    for (int i=0;i<2;i++)
    { System.out.println("Enter a number:");
    num[i]=scan.nextInt();

    System.out.println("Enter name of number:");
    num1[i]=scan.nextLine();

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: how to input string and integers into 2 diff arrays at a time?

    You have fallen for the nextInt trap. In the Scanner class it has several methods (nextInt, nextDouble) that only read the number and do not consume the carriage return. So next time you call nextLine it reads the empty String between the number you entered for nextInt and the carriage return. Kludge to fix: simply make a call to nextLine after nextInt to get rid of the carraige return.

Similar Threads

  1. Replies: 0
    Last Post: 10-14-2011, 12:33 AM
  2. Replies: 9
    Last Post: 05-11-2011, 06:12 PM
  3. Replies: 0
    Last Post: 12-21-2010, 06:09 AM
  4. calculate time diff for particular time period
    By baktha.thalapathy in forum New To Java
    Replies: 2
    Last Post: 05-24-2010, 04:10 PM
  5. file input: array of integers
    By hannes in forum New To Java
    Replies: 8
    Last Post: 01-27-2010, 03:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •