Hello,
I am writing a program that will read in a pair of inputs, (P, S), summarizing the results of a code inspection. One input is the integer problem number, P, and the other is the severity level, S, of that problem. Since a typical code inspection will never have as much as 2000 problems, we should plan that 1≤ P ≤ 2000. Severity levels, S, may take on values of 1, 2, 3, 4, or 5, where 1 is the worst case. Use the situation where problem number equals zero, P = 0, as an indicator that there is no more problems to read in. When all problems are read in, the program should summarize the number of problems found in each severity category and print out that summary. As part of the summary, all the problem numbers, Ps, for the highest severity, Severity =1, should also be printed out.

This is what i have coded.

``````import java.util.*;
public class SummarizeResults
{
public static void main(String args[])
{
int p = -1;
int s = -1;
int[] Problem;
Problem = new int;
Arrays.fill(Problem, 0);
/*for ( int i = 1; i < Problem.length; i++ )
{
System.out.println(Problem[ i ]);
}*/

int sev1 = 0;
int sev2 = 0;
int sev3 = 0;
int sev4 = 0;
int sev5 = 0;

/*while ( p !=0 && 1 <= p <= 2000)
{
//should i read in the pinut as a string and parse the information? I would need help doing this because i forgot how to.
Scanner input = new Scanner(System.in);
System.out.print("Enter problem number: ");

p  = input.nextInt();
Scanner 2ndinput = new Scanner(System.in);
System.out.print("Now enter accompanying severity level: ");

s  = 2ndinput.nextInt();
switch (s)
{
case 1 : sev1 = sev1 + 1;
Problem [i] = p;
i = i + 1;
break;
case 2 : sev2 = sev2 + 1;
break;
case 3 : sev3 = sev3 + 1;
break;
case 4 : sev4 = sev4 + 1;
break;
case 5 : sev5 = sev5 + 1;
break;
default: System.out.println ("something is wrong with the severity code for problem = ":, p )
}
Scanner input = new Scanner(System.in);
System.out.print("Enter problem number: ");

p  = input.nextInt();
Scanner 2ndinput = new Scanner(System.in);
System.out.print("Now enter accompanying severity level: ");

s  = 2ndinput.nextInt();
}
if ( 2000  <  p  ||  p < 0 )
{
System.out.print ( “Problem number = ” , p,  “ is invalid and we are terminating with summary”)
}
else
{

System.out.print( “severity 1 =”, sev1, “severity 2 =”, sev2, “severity 3 =”, sev3, “severity 4 =”, sev4, “severity 5 =”, sev5)

System.out.print( “the following list contains all the severity 1 problem numbers.” )

for (int  j = 1 ;   j <= i ;  j++ )
{
System.out.println ( “problem number =”, Problem [j] )
}
}*/

}
}``````

This is very rough and am getting a lot of errors. Can someone help me iron out the wrinkles plz?

First, in your first while() loop while ( p !=0 && 1 <= p <= 2000) You have to restate p every time so it should look like
while(p != 0 && 1 <= p && p <= 2000)

Scanner 2ndinput = new Scanner(System.in); here you can not use a number as the first lette in the variable name. It can be the last though, or in the middle just not the …

Hey,
So you only need to declare Scanner once, and just use that one throughout the rest. So like in the beggining when you have Scanner input = new Scanner(System.in); just use input …

All you need to do is in the last for loop, make it go till j < i instead of j <=i.

## All 15 Replies

First, in your first while() loop while ( p !=0 && 1 <= p <= 2000) You have to restate p every time so it should look like
while(p != 0 && 1 <= p && p <= 2000)

Scanner 2ndinput = new Scanner(System.in); here you can not use a number as the first lette in the variable name. It can be the last though, or in the middle just not the first.

Also, the error with i is because i has not been declared as a variable. It was in the for loop, but that is declared only for the for loop. So you need to decare another variable, like x, to use.

on this line: System.out.println ("something is wrong with the severity code for problem = ":, p )
The problem is you have to use the + symbold to output a variable. So it would be like System.out.println("problem: " + p + "."); And you also need a semicolon after it.

The problem with the last Scanner decleration is that you have already declared an instance of Scanner with the name input. So you need to name it something else.

All of the errors on the bottom are something wrong with your quotes inthe System.out's. Try retyping all of the quotes. That worked for me.

Ok thanks a lot christiangirl. Every1 of those errors i should have been able to fix myself and i just did thnx to you. But am still having issues with it.

When i run it, and enter 0 as my problem number....it should have ended but it didn't. Not only that but am also having issues with it not recognizing my inputs at times........

Here's the update:

``````import java.util.*;
public class SummarizeResults
{
public static void main(String args[])
{
int p;
int s = 0;
int[] Problem;

Scanner input = new Scanner(System.in);
System.out.print("Enter problem number: ");

p  = input.nextInt();
Scanner secondinput = new Scanner(System.in);
System.out.print("Now enter accompanying severity level: ");

Problem = new int;
Arrays.fill(Problem, 0);
/*for ( int i = 1; i < Problem.length; i++ )
{
System.out.println(Problem[ i ]);
}*/
int i = 1;
int sev1 = 0;
int sev2 = 0;
int sev3 = 0;
int sev4 = 0;
int sev5 = 0;

while(p != 0 && 1 <= p && p <= 2000)
{
s  = secondinput.nextInt();
switch (s)
{
case 1 : sev1 = sev1 + 1;
Problem [i] = p;
i = i + 1;
break;
case 2 : sev2 = sev2 + 1;
break;
case 3 : sev3 = sev3 + 1;
break;
case 4 : sev4 = sev4 + 1;
break;
case 5 : sev5 = sev5 + 1;
break;
default: System.out.println ("Something is wrong with the severity code for problem: " + p + ".");
}
Scanner newinput = new Scanner(System.in);
System.out.println("Enter problem number: ");

p  = input.nextInt();
Scanner newsecondinput = new Scanner(System.in);
System.out.print("Now enter accompanying severity level: ");

s  = newsecondinput.nextInt();
}
if ( 2000  <  p  ||  p < 0 )
{
System.out.print ( "Problem number: "+ p + " is invalid and we are terminating with summary");
}
else
{

System.out.println( "Severity 1 = "+ sev1 + " Severity 2 =" + sev2+ " Severity 3 ="+ sev3+ " Severity 4 ="+ sev4+ " Severity 5 ="+ sev5);

System.out.println( "The following list contains all the severity 1 problem numbers." );

for (int  j = 1 ;   j <= i ;  j++ )
{
System.out.println ( "Problem number = "+ Problem [j] );
}
}

}
}``````

I just noticed that the `"s = secondinput.nextInt();"` within the while statement was in the wrong place and thats where the problem was coming from.

The other problem still stands though...refering to the fact that when i run the program, and enter 0 as my problem number instead of it terminating, it doesn't.

And for formatting purposes....how do i get rid of the new line between the print statement and the input field?

Hey,
So you only need to declare Scanner once, and just use that one throughout the rest. So like in the beggining when you have Scanner input = new Scanner(System.in); just use input throughout the program instead of declaring it again.
And for 0, at the end of your while loop put an if statement where if it the problem number doesnt equal 0, then it will ask for the problem severity. Otherwise it doesnt.

As for the line beteen the print statement and the input field, I'm not sure on that one. I'm not sure that there is a way to fix that.

Thanks, that totally worked. And am not bothered by the new line anymore. I added the "if" statement and for some reason the program automatically attaches a program severity number of 1 to it.

I'll go with bad because its not supposed to do that. Me entering "0" should tell the program to just terminate and do nothing else.

you could try subtracting one

where does it add one at?

Here, I have the most updated code attached. You should run it and see what am talking about. You'll understand what am trying to say better.

All you need to do is in the last for loop, make it go till j < i instead of j <=i.

commented: Thanks for your help! +1

Thanks! That solution made sense. I don't know why i didn't think of it. The number 0 ends up being the last entry in the array and instead of including it in the printout all I had to do was stop one short of that entry. This is now an officially solved thread. Am Repping you accordingly. And yea, am terrible at coding. For you see the pseudo code was given to me and am still trying to understand how those switch statements keep track of the severity for each problem. I'll just ask a classmate to explain it to me.

Glad to help! You can message me questions any time you want!

The switch statement pretty much does something based on what is entered, it "switches" betwen different options. If a 1 is entered it goes to option 1, if 2 entered it goes to option 2. then it does the code under that option.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.