Need help with this code. Anyone with knowledge on java please help.
i found some on this forum, but that is also wrong. can anyone give me some help
what is want is to smooth the values in an integer array. If its totally wrong please dont yell

import java.io.*;
import javax.swing.*;

public class arraytest
{	
	public static void main(String args[])
	{	
		String getsize = JOptionPane.showInputDialog(null,"enter a value for the size of the array:","Input",JOptionPane.QUESTION_MESSAGE);
		
		int arraysize = Integer.parseInt(getsize);
		
		
		int[] getsignal = new int [arraysize];
		
		getsignal = getsignal(arraysize);
		
		int[]getsmoothignal = smooth (arraysize.getsignal);
		
		JOptionPane.showMessageDialog(null,showoutput(getsizearray,getsignal,getsmoothsignal)+"complete\n","Output",JOptionPane.INFORMATION_MESSAGE);
		
		System.exit(0);
	}

	
	
	public static int[] getsignal(int s)
	{		
		//declare
		int[] getnumber = new int [s];
		
		
		for(int index = 0; index < s; index++)
		{		
			String getintstring = JOptionPane.showInputDialog(null,"enter value of array:","Input",JOptionPane.QUESTION_MESSAGE);
			
			getnumber[index]=Integer.parseInt(getIntString);
			
			
			while (getnumber[index]<2)
			{		
				JOptionPane.showMessageDialog(null,"Please re-insert your values","Output",JOptionPane.ERROR_MESSAGE);
				
				getIntString = JOptionPane.showInputDialog(null,"Enter values of array:","Input",JOptionPane.QUESTION_MESSAGE);
				
				getnumber[index] = Integer.parseInt(getIntString);
			}
		}

		return getnumber;
	}

	
	public static int[]smooth(int a, int[] signals)
	{
		
		int[]smooth = new int [a];
		
		
		smooth[0]=(signals[0]+signals[1])/2;
		
		
		for (int i = 1; i <= a - 2; i++)
		{
			smooth[i]=(signals[i-1]+signals[i]+signals[i+1])/3;		
		}
		
		int endValue = a - 1;
		smooth[endvalue]=(signals[endvalue-1]+signals[endvalue])/2;
		
		return smooth;
	}
		
	
	public static string showOutput(int a, int[]signal,int[]smooth)
	{
		
		String output = "signal"+" "+smooth ;
		for (int index = 0; index < a; index++)
		{
			output = signal[index]+" "+" " +smooth[index]+"\n";
		}
		return output;
	}
}

Dani AI

Generated

Short, practical checklist to get this compiling and producing the smoothed array. posted a working idea for a 3‑point moving average; was right that the algorithm is fine, and the replies from , and already point to the common causes: case sensitivity, mismatched names, whitespace and incremental debugging. Below are the focused steps to make the program robust and to isolate the remaining errors.

  1. Fix compile-time/name problems first. Java is case-sensitive and identifiers must match exactly (for example String vs string, method names and variable names, endValue vs endvalue). Avoid naming a variable the same as a method. Use the conventional int[] arr spacing so declarations are clear. See Java language basics for reference: Java language basics.

  2. Validate and parse input safely. Wrap Integer.parseInt in a try/catch for NumberFormatException and trim the input before parsing; give a clear re-prompt when parsing fails. See Integer.parseInt and the exceptions guide: Integer.parseInt and Exception handling.

  3. Guard array bounds and edge cases. Decide minimum acceptable array size (at least 2 for end-averages, 3 to use a full 3-point average on interior points) and check it before building arrays. When computing smoothed values, handle first and last elements explicitly to avoid ArrayIndexOutOfBoundsException. Test with small arrays (2–4 elements) to exercise edge logic.

  4. Build output reliably and test on the console before using dialogs. Use a single StringBuilder to assemble lines like "original -> smoothed" and then call JOptionPane.showMessageDialog(...) once. The JOptionPane API is here: JOptionPane.

Practical workflow: compile early and often (as suggested), comment out the smoothing call to get a successful compile, then reintroduce it. Use an IDE (Eclipse/IntelliJ) to see these errors inline. This approach isolates syntax first, then runtime/logic issues.

Recommended Answers

All 5 Replies

What part is "wrong" in your estimation? Wrong is a pretty broad area. Just glancing at the smooth() method it looks like a basic 3 point moving average. Where is the issue?

well ..see i am least gud in java..yet to learn..
there are too many errors which i am nt able to correct...didn understnd hw to bring about the output in JOptionPane..

well ..see i am least gud in java..yet to learn..
there are too many errors which i am nt able to correct...didn understnd hw to bring about the output in JOptionPane..

Get it down to a manageable number of errors. Start commenting things out till everything works. If you get errors before you even get to call the smooth function, comment out the call to the smooth function and the smooth function itself and see if you get fewer. I'd comment out everything but the first line in main first and see if it compiles. If it doesn't, fix it. Then uncomment the second line and compile, see if you get any errors, then the third line, etc. You have to do it in stages. Don't try to debug an entire program full of bugs all at once.

public static string showOutput(int a, int[]signal,int[]smooth)

There is not such thing as a "string". The correct declaration is String with capital S. And most important, java is case sensitive. Meaning that if you want to call the above method you should call it like this: showOutput NOT like this: showoutput. Try to find where you have such a mistake because I think I've seen in other places, and not only for showOutput but other variables as well. You declare them
like: endValue
and I see that you use them
like: smooth[ endvalue ] = ...

Try to correct the above and continue with the others.

And do remember that there is a place for whitespace. int[]variable is NOT good, I think it won't even compile.

Be a part of the DaniWeb community

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