1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Java Question

Discussion in 'Programming & Webmastering' started by dcf-joe, Dec 23, 2008.

  1. dcf-joe

    dcf-joe

    Joined:
    Feb 11, 2008
    Messages:
    503 (0.15/day)
    Thanks Received:
    123
    Location:
    Lincoln, Nebraska, USA
    I need help creating a program that takes input from the user, stores it into an array, and then combines everything in the end. Here is the first part of the program, but I do not know what to do next. I may even be going in the wrong direction. But, here it is:
    import java.util.Scanner;

    public class Gauss
    {
    public static void main(String[] args)
    {
    Scanner reader = new Scanner(System.in);
    System.out.print("Enter the number of times the loop should run: ");
    int num3 = reader.nextInt();

    int[] ia = new int[num3];

    for (int i = 0; i < ia.length; i++)
    {
    ia = i;
    }

    int sum = 0;

    for (int i = 0; i < ia.length; i++)
    {
    sum += ia;
    }
    System.out.println(sum);
    }
    }
     
  2. Oliver_FF

    Oliver_FF New Member

    Joined:
    Oct 15, 2006
    Messages:
    544 (0.14/day)
    Thanks Received:
    65
    In Java you shouldn't be using Arrays, you should use a Collection:

    Code:
    import java.util.*;
    
    public class Gauss
    {
        public static void main(String[] args)
        {
            //Create an arraylist of Strings. This is our collection.
            ArrayList<String> inputLines = new ArrayList<String>();
            //Create a scanner to read from the console.
            Scanner reader = new Scanner(System.in);
    
            //Start looping
            do {
                //Get the user input
                String currentLine = reader.nextLine();
                //Add it to the collection
                inputLines.add(currentLine);
            //keep looping until they don't enter anything
            } while (currentLine != null || currentLine.length()!=0); 
            
            //Loop over every String in the collection and print it out...
            for (String s: inputLines) {
                System.out.print(s);
            }
        }
    }
    
     
    Last edited: Dec 24, 2008
    10 Year Member at TPU

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)