Few problems I'm having with Java at the moment, would be grateful if any of you could give me a hand.

Wrote this piece of code to input 2 numbers from user, square root of 1, and cube root of another. Program works but I want to know while using the Scanner class is there a way so that I can get the user to input multiple data on one line, instead of the program moving to the next line after each input.

package Assignment2;

import java.util.Scanner;
import java.text.DecimalFormat;
public class CubeSquare {

    
    public static void main(String[] args) 
    {
       double number1, number2, min;

       Scanner scan = new Scanner (System.in);
       DecimalFormat fmt1 = new DecimalFormat ("00.###");

        System.out.println("Enter 2 numbers:");
        number1 = scan.nextDouble();
        number2 = scan.nextDouble();

        number1 = Math.cbrt(number1);
        number2 = Math.abs(number2);
        number2 = Math.sqrt(number2);
        min = Math.min(number1,number2);

        System.out.println(""+ fmt1.format(min));





    }

}

Another program i'm having problems with is that I have written this for loop program to input 2 numbers into a system and add up the range inclusively, for example inputting 10 and 14 would give you 10+11+12+13+14 = 60, I need it so that it will work when the user enters a second number that is smaller than the first like 10 and 3. The program doesn't seem to work when that is the outcome at the moment.

package Assignment2;

import java.util.Scanner;

public class Ranger {

    public static void main(String[] args) {
       int number1,number2,count,totalsum =0;

       Scanner scan = new Scanner (System.in);

        System.out.println("Please enter two integers:");
        number1 = scan.nextInt();
        number2 = scan.nextInt();


            
        
        for (count=number1; count<=number2; count++)
        {
            totalsum = totalsum+count;
        }

        System.out.println(""+totalsum);
           


    }
}

Thank you.

Recommended Answers

All 8 Replies

Can anyone help please?

Sorted out the second problem.

I haven't tried that before but there is the next() method:
Call the method twice and give 2 numbers as input:

String a = scan.next();
String b = scan.next();
[B]scan.nextLine();[/B]

double num1 = Double.parseDouble(a);
....

The input would be:
>20 30


A small explanation about the addition of the scan.nextLine() command.

When you call the next method you don't change lines. So you can read the 2 numbers. But any other call of the next.... methods will be done at the same line. It will try to read what is after the "30". Which is an empty String.

If after the second next, you press ENTER and try to read the next line with the nextLine or any other nextDouble method it will not read the next line because the "cursor" is still at the line with the first 2 numbers: "20 30". It will read from the last position it was(after the "30") till the end of the line. Which is, as mentioned, the empty String "".

That is why you call the nextLine in order to change lines. Unless of course you want to keep entering numbers

You can input the two numbers from the user and keep them as a string in your program, after that, you can use the "StringTokenizer" class which can split your string into tokens.
NOTE: I think it's under the directory java.util.
I hope my answer was helpful.

hi can you help me to create loops like this please

Please enter lines of text:
JavaFX Script
This book is a quick and easy read
The quick brown fox
The quick brown fox
Duplicated : The quick brown fox
JavaFX Script
Red sky at night
Red sky at night
Duplicated : Red sky at night
Sink Language project

Bye

Have a while loop that stops when the user enters a specific character.
Then inside the loop read 1 input from the keyboard and compare that with the previous one. If they are the same then you have duplicate. Remember at the end of the loop you will need to replace the value of the old value with the current value, so at the next loop the current value would be treated as the old one to compare.

Also start a new thread, if you have any more questions.

hi i've started doing it but i got stock with this

    public static void main(String[] args) {

 boolean done = false ;
while ( ! done )
{
    if ( userPressedEsc() )
    {
        done = true ;
    }
    else
    {
        System.out.println("not done yet");
    }
}

As explained in my previous post: START a new thread. Have you no interest to my suggestions?

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.