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

Java Question

Joined
Feb 11, 2008
Messages
607 (0.10/day)
Location
Omaha, Nebraska, USA
System Name Built By Me
Processor Intel Core i9 9900K @ 5.1 GHz
Motherboard Gigabyte Z390 Aorus Ultra
Cooling Custom Water Cooling - CPU Only
Memory 32GB (2 x 16) GSkill Ripjaws V DDR4
Video Card(s) RTX 4080 - ASUS ROG Strix 16GB OC - P Mode
Storage 1TB Samsung 970 Evo NVMe
Display(s) Alienware AW2723DF @ 280 Hz @ 1440P
Case Fractal Design Define S2
Audio Device(s) Corsair Virtuoso Pro
Power Supply 850W Seasonic Platinum
Mouse Razer Viper V2 Pro @ 2k Hz
Keyboard Asus ROG Strix Scope II 96 Wireless - ROG NX Snow Switches
Software Windows 11 Pro
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);
}
}
 

Oliver_FF

New Member
Joined
Oct 15, 2006
Messages
544 (0.08/day)
Processor Intel q9400 @ stock
Motherboard Lanparty P45-T2RS
Cooling Zalman CNPS-9500
Memory 8GB OCZ PC2-6400
Video Card(s) BFG Nvidia GTX285 OC
Storage 1TB, 500GB, 500GB
Display(s) 20" Samsung T200HD
Case Antec Mini P180
Audio Device(s) Sound Blaster X-Fi Elite Pro
Power Supply 700w Hiper
Software Ubuntu x64 virtualising Vista
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:
Top