Hello!

I started this course in Java and we got this assignment to do and it consists of making 2 strings of text and then we are are supposed to write this code that storage the varible "max_length" and then print out "The longest string has the length...". I know how to print it out but I'm really new to programming and my head is soon about to expload of information :eek:

This is how far I've come:

public static void problem3()
{
String indata1 = showInputDialog("Skriv ett ord här!");
String indata2 = showInputDialog("Skriv ett till ord här!");
}

... plz help me (I'm a programming n00b) lol

:o

/anette

Dani AI

Generated

Brief, practical summary: read two strings, use String.length() to get their lengths, store the larger value in an int max_length, and print the required sentence. As pointed out, length() is the correct method. showed the usual if/else approach and suggested Math.max() as a compact alternative; add a null check because showInputDialog can return null (Cancel).

Common syntax mistakes from 's snippets: int = max_length; is invalid — declare int max_length;. Call the method with parentheses: indata1.length() not indata1.length. A missing n in System.out.printl causes a compile error. The condition in if(...) must be inside parentheses and use braces for multi-line blocks. A concise, null-safe pattern that avoids those pitfalls:

String s1 = showInputDialog("First word:");
String s2 = showInputDialog("Second word:");

int len1 = (s1 == null) ? 0 : s1.length();
int len2 = (s2 == null) ? 0 : s2.length();
int max_length = (len1 >= len2) ? len1 : len2;

System.out.println("The longest string has the length " + max_length);

Notes on imports and static vs instance methods: a static import lets the program call dialog methods without qualifying the class; without it the methods must be qualified with the JOptionPane class. main must remain static; to avoid static helper methods follow 's suggestion and instantiate the class inside main and call instance methods. For quick assignments keeping helper methods static is simpler.

Troubleshooting: if a commented call still appears to run, save and rebuild the project (stale .class files or running the wrong class are common causes). Use the IDE's Clean/Build, check for multiple main methods, and fix typos — doing so will fix the typical "commented code still runs" behaviour.

Recommended Answers

All 9 Replies

String has a method length() to determine its length.
Call that on both Strings and compare the results.

can one write like this:

String indata1 = showInputDialog("write a word here!");
String indata2 = showInputDialog("Skriv ett till ord här!");

int = max_length;

if(indata1.length>indata2.length)

System.out.println("Den längsta strängen har längden...");
else
System.out.printl("The longest string has the length...");

Melotron,

Here is an example of the code you need:

import javax.swing.JOptionPane;

public class Test
{
	public static void main(String[] a)
	{
		//get first string from user
		String indata1 = JOptionPane.showInputDialog("Enter text");
		
		//get second string from user
		String indata2 = JOptionPane.showInputDialog("Enter text");
		
		//check length
		if( indata1.length() > indata2.length())
		{
		    System.out.println("The longest is " + indata1);
		}
		else if( indata1.length() < indata2.length())
		{
		    System.out.println("The longest is " + indata2);
		}
		else
		{
		    System.out.println("They are the same length");
		}
	}
}

:?: For more help,

commented: don't do people's homework, let them think for themselves. +0

Melotron,

Here is an example of the code you need:

import javax.swing.JOptionPane;

public class Test
{
	public static void main(String[] a)
	{
		//get first string from user
		String indata1 = JOptionPane.showInputDialog("Enter text");
		
		//get second string from user
		String indata2 = JOptionPane.showInputDialog("Enter text");
		
		//check length
		if( indata1.length() > indata2.length())
		{
		    System.out.println("The longest is " + indata1);
		}
		else if( indata1.length() < indata2.length())
		{
		    System.out.println("The longest is " + indata2);
		}
		else
		{
		    System.out.println("They are the same length");
		}
	}
}

:?: For more help,

Thanks alot but I have some more (stupid basic((java)) questions.

First we need to use the variable "max_length" since it's a part of our assignment. And I think I made thinks right if you look up in the post a bit. But JCreator says that it has 4 errors but it's just that I can't see the structure you need to see to build Java things. (my head hurts now)

Second I wonder if the "//" indicates that it shouldn't be read by Java and in that case why is one "problem" in my code running even if I have:

//problem1();
//problem2();
problem3();

I'm at problem3 but for some reason it runs the second problem and then after executing that one it ends.

Thanks for all the help, btw! :)

(I'm new to this forum but I love it already)

/anette

Here is an example that uses max_length.

import javax.swing.JOptionPane;

public class Test
{
	public static void main(String[] a)
	{
		//get first string from user
		String indata1 = JOptionPane.showInputDialog("Enter text");
		
		//get second string from user
		String indata2 = JOptionPane.showInputDialog("Enter text");
		
		int max_length=0;
		
		//check length
		if( indata1.length() >= indata2.length())
		{
		    max_length = indata1.length();
		}
		else
		{
		    max_length = indata2.length();
		}

		System.out.println("The longest string has length " + max_length);
		
	}
}

To make the code better, you could replace the if statements above with one line:

max_length = Math.max(indata1.length(), indata2.length());

:?: For more help,

Thanks alot!! :)

Moving on to problem4 with a better view of the structure when working in Java. Thanks alot for your help. (headache is disappearing) :P

If one want to change from Static import to non-static import only in the problem modifiers as below, where and what do I need to change?do I need to remove static anywhere else?

import static javax.swing.JOptionPane.*;
public class Lab1_2
{
public static void main (String [] arg)
{
problem1();
//problem2();
//problem3();
//problem4();
}
public static void problem1()
{
String indata;
indata = showInputDialog("Skriv en mening och få reda på om den\när kortfattad eller bluddrig:");
if(indata.length() <15){
showMessageDialog(null, "Kortfattad typ");}
if(indata.length() >10){
showMessageDialog(null, "Bluddrig typ");}

If one want to change from Static import to non-static import only in the problem modifiers as below, where and what do I need to change?do I need to remove static anywhere else?

I'm not sure what you mean by "import", but if you're asking how to get out of making static calls endlessly from one method to the next, just instantiate your class inside the main method and then make method calls on the instance you just created.

For example:

public class MyClass() {[INDENT]//this is a non-static method 
public void doSomething() {[INDENT]System.out.println("Gjøre noe interessant");[/INDENT]}
//main is the special method where all Java programs begin.  It must be declared static.
public static void main(String[] args) {
[INDENT]//Instantiate an instance of a MyClass:
MyClass myClass = new MyClass();
//Now you can make non-static method calls:
myClass.doSomething();[/INDENT]}[/INDENT]}

Håper dette gjelper litt.

And what did you learn from turning in something someone else made for you?
NOTHING.

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.