主题:请问如何将含有空格的字符串输入到一个字符串中
daviswplc416
[专家分:0] 发布于 2008-04-12 22:26:00
System.out.print(UNCODED);
uncode = sc.next();
System.out.print(DELIMITERUSE);
delimiter = sc.next();
System.out.print(AMOUNT);
shift =sc.nextInt();
System.out.print(PUNCTUA);
punctuation = sc.nextBoolean();
当程序提示输入uncode字符的时候如果输入含有字符的字符串,“ad kad kkafd dk"程序会用空格分开将四个字符输入到四个变量中,而我只想将他们输入到第一个uncode中。不知道有没有朋友能帮我解决一下,想了一晚上了,也不知道怎么办。用sc.nextline()也不知道该怎么用,上网查了半天也没有头绪。哎
回复列表 (共14个回复)
沙发
飞侠 [专家分:1380] 发布于 2008-04-12 23:00:00
不知道你具体要做什么,下面的代码,可以从命令行接受一个字符串,并把它付给一个变量
import java.io.*;
public class MyInput
{
public static String readString()
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in),1);
String string="";
try
{
string=br.readLine();
}
catch (IOException ex){
System.out.println(ex);
}
return string;
}
}
public clsss test
{
public static void main(String args[])
{
String s="";
System.out.println("Enter a string:");
s=MyInput.readString();
System.out.println("Your string is: "+s);
}
}
板凳
daviswplc416 [专家分:0] 发布于 2008-04-12 23:38:00
谢谢你的回复:)
不过不敢写的这么难,帮一个朋友写作业,刚入门的。要求读出用户的四个输入值并储存,分别是string,char,int和boolean。
刚开始用systemprint函数提示用户输入数据,然后分别储存。第一次运行的时候可以正常运作,经过一个循环进入第二次后,程序总是将第一个和第二个的信息一起输出,然后要求用户同时输入。不知道该怎么解决。这是我后来修改的,就剩下这个问题了。
System.out.print(UNCODED);
uncode = sc.nextLine();
System.out.print(DELIMITERUSE);
delimiter = sc.next();
System.out.print(AMOUNT);
shift =sc.nextInt();
sc.nextLine();
System.out.print(PUNCTUA);
punctuation = sc.nextBoolean();
sc.nextLine();
请帮忙看看,谢谢了
3 楼
飞侠 [专家分:1380] 发布于 2008-04-13 02:04:00
你把你写的完整代码贴出来看看
4 楼
daviswplc416 [专家分:0] 发布于 2008-04-13 11:15:00
/**
* @(#)KXT 101 Assignment 1: Encrption Program
* *
* @File: AssigOne108.java
* @author Wei Feng
* @Studnet NO.
* @This program is used to provide the user with an encrypted (coded) form of a string they enter
*/
//import statement
import java.util.Scanner;
public class AssigOne1082{
public static void main(String[] args){
//imagine user choose to start the game at first and whether to go another test
final String START = "y";
//store some initial values
final int ALPHABET = 26;
final int KNOWLEDGEA = 65;
//use final String to store some initial messages
final String UNCODED = "Please enter a string to be encoded:";
final String DELIMITERUSE = "Please enter the delimiter you wish to use:";
final String AMOUNT = "Please enter the amount you wish to shift by:";
final String PUNCTUA = "True or false -- include pubctuation:";
final String INCLUDE = "will include punctuation, and";
final String UNINCLUDE = "will not include pubctuation, and";
5 楼
daviswplc416 [专家分:0] 发布于 2008-04-13 11:16:00
//the initial coded string is empty
String coded = "";
//a String entered by the user and to uppercase (for their uncoded string)
String uncode;
String uncodeUpper;
//calculate the length of uncoded string
int strLength;
//an integer entered by the user (for the shift)
int shift;
//a character entered by the user (for their delimiter)
String delimiter;
//a boolean to record whether or not punctuation should be included
boolean punctuation;
//the count of the number of coded Strings displayed
int number=0;
//store the answer that user want to do another game or not
String answer,ask;
6 楼
daviswplc416 [专家分:0] 发布于 2008-04-13 11:16:00
System.out.println("This program will encode messages!Press enter to Start!");
//System.out.println();
//creat a Scanner objectb
Scanner sc = new Scanner (System.in);
/**main frame of the program to run the core program
such as store the user's initial input, code the message based on users input
and show the output the program runs*/
do{
//ask users to input some initial variables such as uncoded strings, delimiters
//punctuation, shift number to implement program
sc.nextLine();
System.out.print(UNCODED);
uncode = sc.nextLine();
System.out.print(DELIMITERUSE);
delimiter = sc.next();
System.out.print(AMOUNT);
shift =sc.nextInt();
System.out.print(PUNCTUA);
punctuation = sc.nextBoolean();
sc.nextLine();
//show the initial message based on user's input
System.out.println();
System.out.println("Encoding of \"" + uncode+ "\" ");
System.out.println("will be by shifting " + shift + " letters,");
//accoring to user's choice to show whether the program includes punctuation or not
if (punctuation)
{
System.out.println(INCLUDE);
}
else
{
System.out.println(UNINCLUDE);
}
System.out.println("with words delimited by '" + delimiter + "'." );
uncodeUpper = uncode.toUpperCase();
//calculate uncoded strings' length
strLength = uncodeUpper.length();
7 楼
daviswplc416 [专家分:0] 发布于 2008-04-13 11:16:00
for (int i = 0; i<strLength; i++)
{
if(uncodeUpper.charAt(i)>=0 && uncodeUpper.charAt(i) <= 9)
{
coded = coded + uncodeUpper.charAt(i);
}
else if((uncodeUpper.charAt(i) >= KNOWLEDGEA) && (uncodeUpper.charAt(i) <= KNOWLEDGEA + ALPHABET))
{
coded = coded + (char)((uncodeUpper.charAt(i) + shift)%91+ (uncodeUpper.charAt(i)+shift)/91*65);
}
else if(uncodeUpper.charAt(i) == ' ')
{
coded = coded + delimiter;
}
else if(punctuation)
{
coded = coded + uncodeUpper.charAt(i);
}
else
{
coded = coded;
}
}
System.out.println("The encoded string is:\"" + coded +"\"." );
//ask user whether to go for another test or not
System.out.println();
System.out.print("Try another?(y/n)");
//calculate user's choice to go for another test or not
ask = sc.next();
answer = ask.toLowerCase();
//empty the coded strings for the use nexttime
coded = "";
//store the numbers of strings encoded
number++;
}
while (answer.equals(START));
//tell user how many times strings are encoded
System.out.println(number + " strings encoded");
}
}
8 楼
daviswplc416 [专家分:0] 发布于 2008-04-13 11:17:00
问题1:
运行的时候必须按回车才能运行,因为我加了sc.nextLine()在do循环的第一行里,如果去掉do循环里的第一个sc.nextLine(),当程序进入第二个循环的时候,会将同时输出UNCODED和DELIMITER的信息,让用户同时输入,:(
问题2:
当我输入的第一个(uncoded)的值含有数字,并在第四个(punctuation)的值选择false的时候,输出结果显示不出数字,如果选择true就可以在结果显示出数字。不知道处在哪里的问题。
PS:整个程序要求
要求用户输入一段字符串(可含空格),然后输入界定符,shift的数字,最后是punctuation。程序将用户输入的信息加密并输出加密信息。首先将非加密的信息的字母都变成大写,然后进行
如果用户输入的是数字,直接copy到加密信息中。
否则,如果用户输入的是字母,将字母升shift(如果是A,shift是3,则是D,如果过是Z,shift是1,则结果是A),然后copy到加密信息中。
否则,如果字母是space,将delimiter(界定符)copy到加密信息中
否则,如果punctuation选择true的话,将现有字母copy到加密信息中
否则,什么都不做。
非常感谢!
9 楼
飞侠 [专家分:1380] 发布于 2008-04-13 20:41:00
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Scanner;
public class AssigOne1082{
//这个方法用来判断字符是否为数字
public static boolean isNumber( String source ) {
return source.matches("^[0-9]+$");
}
public static void main(String[] args){
//imagine user choose to start the game at first and whether to go another test
final String START = "y";
//store some initial values
final int ALPHABET = 26;
final int KNOWLEDGEA = 65;
//use final String to store some initial messages
final String UNCODED = "Please enter a string to be encoded:";
final String DELIMITERUSE = "Please enter the delimiter you wish to use:";
final String AMOUNT = "Please enter the amount you wish to shift by:";
final String PUNCTUA = "True or false -- include pubctuation:";
final String INCLUDE = "will include punctuation, and";
final String UNINCLUDE = "will not include pubctuation, and";
//the initial coded string is empty
String coded = "";
//a String entered by the user and to uppercase (for their uncoded string)
String uncode;
String uncodeUpper;
//calculate the length of uncoded string
int strLength;
//an integer entered by the user (for the shift)
int shift;
//a character entered by the user (for their delimiter)
String delimiter;
//a boolean to record whether or not punctuation should be included
boolean punctuation;
//the count of the number of coded Strings displayed
int number=0;
//store the answer that user want to do another game or not
String answer,ask;
System.out.println("This program will encode messages!Press enter to Start!");
//System.out.println();
//creat a Scanner objectb
/**main frame of the program to run the core program
such as store the user's initial input, code the message based on users input
and show the output the program runs*/
10 楼
飞侠 [专家分:1380] 发布于 2008-04-13 20:42:00
do{
//ask users to input some initial variables such as uncoded strings, delimiters
//punctuation, shift number to implement program
//sc.nextLine();
Scanner sc = new Scanner (System.in);
System.out.print(UNCODED);
uncode = sc.nextLine();
System.out.print(DELIMITERUSE);
delimiter = sc.next();
System.out.print(AMOUNT);
shift =sc.nextInt();
System.out.print(PUNCTUA);
punctuation = sc.nextBoolean();
//sc.nextLine();
//show the initial message based on user's input
System.out.println();
System.out.println("Encoding of \"" + uncode+ "\" ");
System.out.println("will be by shifting " + shift + " letters,");
//accoring to user's choice to show whether the program includes punctuation or not
if (punctuation)
{
System.out.println(INCLUDE);
}
else
{
System.out.println(UNINCLUDE);
}
System.out.println("with words delimited by '" + delimiter + "'." );
uncodeUpper = uncode.toUpperCase();
//calculate uncoded strings' length
strLength = uncodeUpper.length();
我来回复