0

so I'm creating a program that will to determine a final semester class grade given assignment and test grades and the percentages that they count for in that grade. The grade and percentage information for a single student will be given on a single line. Soooo, I just one question:

        q1 = input.nextInt();
        q1p = input.nextDouble();
        q2 = input.nextInt();
        q2p = input.nextDouble();
        q3 = input.nextInt();
        q3p = input.nextDouble();

        l1 = input.nextInt();
        l1p = input.nextDouble();
        l2 = input.nextInt();
        l2p = input.nextDouble();
        l3 = input.nextInt();

is there a better way to simplify this mess of input of int & double??

David
  • 125
  • 2
  • 8
  • 3
    Do you know what arrays or collections are? – Tom Oct 17 '15 at 23:52
  • I do know the terminology, just not how to use them since I am new to programming, sorry.... @Tom – David Oct 18 '15 at 00:00
  • 2
    Side note: it is one way to learn programming by trying and asking questions. The other that also works is to pick a good book or tutorial and follow that step by step, chapter by chapter. The first approach has the advantage that you might learn things in an order that make more sense for you; the second makes it easier to not miss important things. – GhostCat Oct 18 '15 at 00:38
  • @TheMarkofDom, did the solution work for you ? – Kevin Avignon Oct 18 '15 at 03:10

2 Answers2

1

From what I can see from your input variable, it looks like you're working with the Scanner API, am I right ? If so, well there's something pretty nice that you could do using a simple look and the condition has next from the Scanner to verify that there's input to read. Finally, to detect if input is a double or an int, we're gonna make a easy check to make sure !

Collection<Integer> integers= new ArrayList<>();
Collection<Double> doubles = new ArrayList<>();

while(input.hasNext())
{

 if (input.hasNextInt()) {
     integers.add(input.nextInt());
}else if(input.hasNextDouble()) {
     doubles.add(input.nextDouble());
}else
     input.next(); // will simply move to next value in the line
}

This way not only you don't have to check everytime like you did before nextInt or NextDouble with a static user input, you won't have to worry. And if the input isn't a double or an int, well, the lists will remain empty !

UPDATE

Change the use of List for the Collections in order to cause less troubles during run time ! The solution should work out great for you at the moment. I also added a clause in the if structure in order to make the loop complete when hasNext == false

Kevin Avignon
  • 2,853
  • 3
  • 19
  • 40
  • 2
    *" And if the input isn't a double or an int, well, the lists will remain empty !"* And you get an infinite loop for free :). – Tom Oct 18 '15 at 00:05
  • 1
    @Tom you're right! I fixed it by using next() method :-) – Kevin Avignon Oct 18 '15 at 00:09
  • Good, then there is only one problem left: [What is a raw type and why shouldn't we use it?](http://stackoverflow.com/q/2770321) – Tom Oct 18 '15 at 00:21
  • I don't get what you mean @Tom. I read it, the post, but I don't really get it. I'm going to look it over again in a few hours and then try to update the answer! – Kevin Avignon Oct 18 '15 at 00:24
  • 1
    Read the section *"What's so special about raw types?"* of the first answer. It explains raw types using a `List`. Since you also used a list in your code here, it might be easier to understand for you. – Tom Oct 18 '15 at 00:39
0

You can use two arrays and loop over them:

int numberOfInputs = XXX;
int[] ints = new int[numberOfInputs];
double[] doubles = new double[numberOfInputs];

for (int i = 0; i < numberOfInputs; i++)
{
    ints[i] = input.nextInt();
    doubles[i] = input.nextDouble();
}

If you need it more flexible and have no problems with (un)boxing you could use a Colletion:

Collection<Integer> ints = new ArrayList<>();
Collection<Double> doubles = new ArrayList<>();

while (moreInput)
{
    ints.add(input.nextInt());
    doubles.add(input.nextDouble());
}

moreInput is an arbitrary condition that you need to adjust to your needs.

You might add conditions in the loops if there are for example 2 ints and 1 double.

Pinkie Swirl
  • 2,375
  • 1
  • 20
  • 25