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

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, www.NeedProgrammingHelp.com

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, www.NeedProgrammingHelp.com

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, www.NeedProgrammingHelp.com

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.