HOW TO FOLD THE NUMBER OF LINE JAVA

Good all the time of day. Ask to look at the code and say where I natupil))

import java.util.Scanner;public class Zad3 { 
public static void main(String[] args) {
char c = ' ';
char d = ' ';
int symma=0;
Scanner scan = new Scanner(System.in);
System.out.println("Vvedite 4isla");
String str = scan.next();
for(int i=0;i c = str.charAt(i);
d = str.charAt(i+1);
symma = (int)c+(int)d;
}
System.out.println(symma);
}}

Without going into holivary regarding the quality of the code and to compare c C # (! Sic), I note that the key place where you mistake this line:

c = str.charAt(i);   
d = str.charAt(i+1);
symma = (int)c+(int)d;

With this approach, your code adds the binary values c, and d, and would evidently numerical values.

you must:

  1. Split row of the input stream on line 2 by some delimiter, such as a space - time String[] s=str.split(" ");
  2. The resulting parse string as the number of types: Double.parseDouble(s[i]);

The code does not claim to be perfect, but I deliberately simplified it. Try it for yourself to understand why and how each line, function and character.

Scanner scan = new Scanner(System.in);
System.out.println("Vvedite 4isla 4erez probel");
String str = scan.next();
String[] numbers = str.split(" ");
int res = 0;
try{
for (String s : numbers) {
res += Integer.parseInt(s);
}
System.out.println("Summa: " + res);
} catch(Exception e) {
System.out.println("Vveli ne chisla!");
}

Still looking for an answer? Check out other topics on java label, or ask your question.

JAVATALKS.RU / The sum of two-digit numbers

Firstly, thank you for what you are not going to immediately write system for managing anything, including feature-rich interface, work with the database and all there servlets or other similar vebovskuyu joy!

Secondly, the problem (loops and conditional branches):
1. From the interval between 0 and 100 including the even output and odd number divisible by 7. Do not print the number in the interval [45..55] (includes number 45 and 55). At least once to use the ternary operator.

2. Enter a sequence of numbers. Entering the number 0 indicates the end of the sequence. Print the sum of all the numbers and their quantity. Also withdraw all numbers satisfying Problem 1.

3. Set the string. Take her character at a time from the center: "puerta" => "ertupa", "abogado" => "gaobdoa", "peninsula" => "nsinulepa". Complication: if a vowel character (any of euioa), then skip it ( "abogado" => "gdb", "peninsula" => "nsinulepa", "peninsula" => "nsnpl")

4. Set square matrix (two-dimensional array) of the form:
[1 2 3]
[4 5 6]
[7 8 9], where the numbers designate the position (there may be in any of the figures). The matrix can be not only 3x3 but 4x4, 5x5, etc.
Output matrix elements in sequences 541236987 (unwinding from the center) 123 698 745 (winding center), 123,654,789 (zigzagikom).

5. Select a number. Determine whether it is in the range [55..444) - from 55 to 444 including and not including whether it by 2 and divided by three (simultaneously). If the condition is met, then find out if it is a two-digit or treznachnym. For a two-digit number to bring it to a three digit rearrange the first and third digit and display resulting number. If the conditions are not met, then display an appropriate message.

PS You can still open the book "Philosophy Java" from Eckel - there must be a problem at the end of each chapter. If you make a problem, then you can still throw nemnogo.Software and cathedrals are much the same - first we build them, then we pray.

It should be in the lines to find all the numbers and calculate their sum - COMPUTER ...

It should be in line to find all the numbers and calculate their sum. You need to use parseInt and valueOf?

import java.util.*;
public class sumOfString {
    public static void main (String[] args){
    Scanner in =new Scanner (System.in);
    System.out.print("Введите строку: ");
    String str = in.nextLine();
    if(!str.matches("^\\\\D*$")){
        ???
        //System.out.println("сумма");
        }else{
        System.out.println("нет цифр");
        }
}
}

thank

In my opinion, it is easier to try to convert the string to a number, and if you flew an exception, then no number. The code becomes clearer:

Tell me, please, how to elegant code to do =)

import java.util.*;
public class sum{
    public static void main (String[] args){
    Scanner in =new Scanner (System.in);
    System.out.print("Введите строку: ");
    String str = in.nextLine();
    int a[]= new int[100];
    int number = 0;
    int result=0;
    for (int i = 0; i < str.length(); i++) {
         if (Character.isDigit(str.charAt(i))){
         number = Integer.valueOf(str.substring(i, i+1)).intValue();
         a[i]=number;
         result+=a[i];
        }
       
    }
    System.out.println(result);
}
}

I have yet happened but that's it (

import java.util.Scanner;
public class repeat {
    public static String repeat(int c,int i)
    {
    String tst = "";
    for(int j = 0; j < i; j++)
        {
        tst = tst+c;
    }
    return tst;
}
    public static void main (String[] args){
    Scanner in =new Scanner (System.in);
    System.out.print("Введите строку: ");
    String str = in.nextLine();
    int number = 0;
    for (int i = 0; i < str.length(); i++) {
         if (Character.isDigit(str.charAt(i))){
        number = Integer.valueOf(str.substring(i, i+1)).intValue();
        String strR = str.substring(0,i)+ repeat (i+1,number);
        System.out.println(strR);
         }
    }
   
}
}

and the result is this:
Enter a string: a3d
a333

Why he repeats 3ku, but not in
, and how do I add the rest of the line, after a replay?
those. e.g.
a3dk5rty
adddkrrrrrty

Firstly, called classes in the Java c capitalized (more here: Code Conventions for the Java (TM) Programming Language: Contents)

Second, does not necessarily turn the integral class (Integer) in a primitive (int) explicitly. You can simply write:

Third, the misspelled repeat function.

Fourth, I recommend to use any IDE and debugging (I recommend IntelliJ Idea Community Edition IntelliJ IDEA :: Download Latest Version of IntelliJ IDEA)

I guess I'm confused (((

import java.util.Scanner;
public class RepeatC {
    public static String repeat(String word,  int count) {
              String a = "";
              for(int i = 0; i < count; i++) {
                if(i + 1 == count) {
                  a = a + word;
                }  else {
                    a = a + word ;
                }
              }
              return a;
            }
   
    public static void main (String[] args){
    Scanner in =new Scanner (System.in);
    System.out.print("Введите строку: ");
    String str = in.nextLine();
    String strR = "";
    int number = 0;
    int len=0;
    char[] stringArray;
    for (int i = 0; i < str.length(); i++) {
         if (Character.isDigit(str.charAt(i))){
        number = Integer.valueOf(str.substring(i, i+1)).intValue();
        len=str.length();
        strR = str.substring(0,i)+repeat(str.substring(i+1, i+2),number)+ str.substring(i+2,len);
       
     }
    }
    System.out.println(strR);
}
}

Why such a response?
Enter a string: d6gh7jk9l
d6gh7jklllllllll

I can not figure out how to fix it (((

I corrected a little code

import java.util.Scanner;
public class RepeatC  {
    public static String repeat(String letter,  int count) {
              String a = "";
              for(int i = 0; i < count; i++) {
                      a = a + letter ;
              }
              return a;
    }
   
    public static void main (String[] args){
    Scanner in =new Scanner (System.in);
    System.out.print("Введите строку: ");
    String str = in.nextLine();
    String strR = "";
    int number = 0;
    int len=0;
        for (int i = 0; i < str.length(); i++) {
         if (Character.isDigit(str.charAt(i))){
        number = Integer.valueOf(str.substring(i, i+1)).intValue();
        len=str.length();
        strR =  str.substring(0,i)+repeat(str.substring(i+1, i+2),number)+ str.substring(i+2,len);
       
     }
    }
    System.out.println(strR);
}
}

but the error is equal to (((
Enter string: a3dl2kl
a3dlkkl

strR = str.substring(0, i) + repeat(str.substring(i + 1, i + 2), number) + str.substring(i + 2, len);

Each time you rewrite the value of the variable strR. It is necessary to maintain.

Perhaps, the algorithm may be as follows:
1) if the number is not met, then the current character to be repeated once.
2) If you meet the figure N, the number following the symbol must be repeated N times.

In other words:
if before the character was a figure N, the symbol of the need to write N times, or - once.

Note that for the affected rows is better to use the StringBuilder, it would not stand out each time a new memory. Read more here: skipy.ru: Notes sober practice Technology -> -> Oh, these lines ...

Thus, the desired code may be written something like this:

import java.util.Scanner;

public class RepeatC {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Введите строку: ");
        String str = in.nextLine();

        StringBuilder strR = new StringBuilder();
        int repeatCount = 1;// if last char was not digit => next char should be added once
        for (int i = 0; i < str.length(); i++) {
            final char currentChar = str.charAt(i);
            if (Character.isDigit(currentChar)) {
                repeatCount = Integer.parseInt(str.substring(i, i + 1));
            } else {
                for (int i1 = 0; i1 < repeatCount; i1++) {
                    strR.append(currentChar);
                }
                repeatCount = 1;
            }
        }

        System.out.println(strR.toString());
    }
}

Read in other languages

 BelorussianEnglish Deutsch Spanish French Italian Portuguese Turkish Arab Ukrainian Swedish Hungarian Bulgarian Estonian Chinese (Simplified) Vietnamese Romanian Thai Slovenian Slovak Serbian Malay Norwegian Latvian Lithuanian Korean Japanese Indonesian Hindi Hebrew Finnish Greek Dutch Czech Danish Croatian Chinese (Traditional) Philippine Urdu Azeybardzhansky ArmenianPolish Bengal Georgian Kazakh Catalan Mongolski Russian Tadzhitsky Tamilʹskij telugu Uzbetsky

Read more:   AS WITH WINDOWS 8 enter the BIOS

Add a comment

Your e-mail will not be published. Required fields are marked *